require 'spec_helper' describe SpiderMech do before(:all) do @example_dataset = [{:url=>"http://localhost:8321", :assets=>{:scripts=>["https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", "http://getbootstrap.com/dist/js/bootstrap.min.js"], :images=>[], :css=>["http://getbootstrap.com/dist/css/bootstrap.min.css", "http://getbootstrap.com/examples/starter-template/starter-template.css"]}, :links=>["/", "/about.html", "/contact.html"]}, {:url=>"/", :assets=>{:scripts=>["https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", "http://getbootstrap.com/dist/js/bootstrap.min.js"], :images=>[], :css=>["http://getbootstrap.com/dist/css/bootstrap.min.css", "http://getbootstrap.com/examples/starter-template/starter-template.css"]}, :links=>["/", "/about.html", "/contact.html"]}, {:url=>"/about.html", :assets=>{:scripts=>["https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", "http://getbootstrap.com/dist/js/bootstrap.min.js"], :images=>[], :css=>["http://getbootstrap.com/dist/css/bootstrap.min.css", "http://getbootstrap.com/examples/starter-template/starter-template.css"]}, :links=>["/", "/about.html", "/contact.html"]}, {:url=>"/contact.html", :assets=>{:scripts=>["https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", "http://getbootstrap.com/dist/js/bootstrap.min.js"], :images=>[], :css=>["http://getbootstrap.com/dist/css/bootstrap.min.css", "http://getbootstrap.com/examples/starter-template/starter-template.css"]}, :links=>["/", "/about.html", "/contact.html"]} ] @empty_dataset = [{:url=>"http://localhost:8321/empty.html", :assets=>{:scripts=>[], :images=>[], :css=>[]}, :links=>[]}] end after(:all) do #cleanup File.delete 'spidermech.log' File.delete 'localhost.json' end it 'should crawl a site' do spider = SpiderMech.new 'http://localhost:8321' data = spider.run data.should eq(@example_dataset) end it 'should save a json file' do spider = SpiderMech.new 'http://localhost:8321' spider.run spider.save_json File.exist?('localhost.json').should eq(true) file = File.open 'localhost.json', 'r' data = JSON.parse file.read data.should eq(JSON.parse(@example_dataset.to_json)) # quick way to change symbols to quotes to test equality end it 'should return a hash with empty assets for empty page' do spider = SpiderMech.new 'http://localhost:8321/empty.html' data = spider.run data.length.should eq(1) # should only have on result as it contains links to no other pages data.should eq(@empty_dataset) end it 'should only have one image' do spider = SpiderMech.new 'http://localhost:8321/image_only.html' data = spider.run data.length.should eq(1) data.first[:assets][:images].first.should eq('test.png') end it 'should only have one script' do spider = SpiderMech.new 'http://localhost:8321/script_only.html' data = spider.run data.length.should eq(1) data.first[:assets][:scripts].first.should eq('stuffs.js') end it 'should only have one css file' do spider = SpiderMech.new 'http://localhost:8321/css_only.html' data = spider.run data.length.should eq(1) data.first[:assets][:css].first.should eq('style.css') end end