spec/flavicon/finder_spec.rb in flavicon-0.1.0 vs spec/flavicon/finder_spec.rb in flavicon-0.1.1

- old
+ new

@@ -1,67 +1,93 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'flavicon/finder' describe Flavicon::Finder do + def html(file) + File.read(File.expand_path(File.dirname(__FILE__) + "/../fixtures/#{file}")) + end + subject { Flavicon::Finder.new('http://www.ex.com') } describe '#find' do - it '' do + it 'should delegate to #request' do + response = double('response', body: html('absolute.html')) + subject.should_receive(:request).with('http://www.ex.com').and_return([response, 'http://www.other.com']) + subject.should_receive(:verify_favicon_url).and_return('http://sub.example.com/absolute.ico') + subject.find + end + it 'should extract favicon from body' do + response = double('response', body: html('absolute.html')) + subject.should_receive(:request).with('http://www.ex.com').and_return([response, 'http://www.other.com']) + subject.should_receive(:verify_favicon_url).with('http://sub.example.com/absolute.ico') + .and_return('http://sub.example.com/absolute.ico') + subject.find.should == 'http://sub.example.com/absolute.ico' end + + it 'should fallback to default when response is empty' do + response = double('response', body: '') + subject.should_receive(:request).with('http://www.ex.com').and_return([response, 'http://www.other.com']) + subject.should_receive(:verify_favicon_url).with('http://www.other.com/favicon.ico') + .and_return('http://www.other.com/favicon.ico') + subject.find.should == 'http://www.other.com/favicon.ico' + end + + it 'should return nil when not valid' do + response = double('response', body: html('absolute.html')) + subject.should_receive(:request).with('http://www.ex.com').and_return([response, 'http://www.other.com']) + subject.should_receive(:verify_favicon_url).and_return(nil) + subject.find.should be_nil + end end - describe '#valid_favicon_url?' do + describe '#verify_favicon_url' do let(:favicon_url) { 'http://www.ex.com/favicon.ico' } let(:response) do { status: 200, body: 'blah', headers: { 'Content-Type' => 'image/x-icon'} } end before do stub_request(:get, favicon_url).to_return(response) end - it 'should be true by default' do - subject.valid_favicon_url?(favicon_url).should be_true + it 'should return favicon url' do + subject.verify_favicon_url(favicon_url).should == favicon_url end context 'http failure' do let(:response) do { status: 400, body: 'blah', headers: { 'Content-Type' => 'image/x-icon'} } end - it 'should be false' do - subject.valid_favicon_url?(favicon_url).should be_false + it 'should return nil' do + subject.verify_favicon_url(favicon_url).should be_nil end end context 'empty body' do let(:response) do { status: 200, body: '', headers: { 'Content-Type' => 'image/x-icon'} } end - it 'should be false' do - subject.valid_favicon_url?(favicon_url).should be_false + it 'should return nil' do + subject.verify_favicon_url(favicon_url).should be_nil end end context 'invalid content-type' do let(:response) do { status: 200, body: 'blah', headers: { 'Content-Type' => 'text/javascript'} } end - it 'should be false' do - subject.valid_favicon_url?(favicon_url).should be_false + it 'should return nil' do + subject.verify_favicon_url(favicon_url).should be_nil end end end describe '#extract_from_html' do - def html(file) - File.read(File.expand_path(File.dirname(__FILE__) + "/../fixtures/#{file}")) - end - it 'should return nil for empty body' do subject.extract_from_html('', 'http://www.ex.com').should be_nil end it 'should return nil if no favicon found' do @@ -82,10 +108,28 @@ subject.extract_from_html(html('multiple.html'), 'http://www.ex.com') .should == 'http://www.ex.com/first.ico' end end + describe '#default_path' do + it 'should return default favicon url' do + subject.default_path('http://www.basic.com').should == 'http://www.basic.com/favicon.ico' + end + + it 'ignores query and fragment' do + subject.default_path('http://www.basic.com?query=a#fragment').should == 'http://www.basic.com/favicon.ico' + end + + it 'ignores path with no trailing slash' do + subject.default_path('http://www.basic.com/basic').should == 'http://www.basic.com/favicon.ico' + end + + it 'ignores path with trailing slash' do + subject.default_path('http://www.basic.com/basic/').should == 'http://www.basic.com/favicon.ico' + end + end + describe '#request' do before do stub_request(:get, 'http://www.ex.com/abc/') .to_return(status: 200, body: '', headers: {}) end @@ -98,9 +142,24 @@ it 'should return response and url when successful' do response, url = subject.request('http://www.ex.com/abc/') response.should be_a Net::HTTPSuccess url.should == 'http://www.ex.com/abc/' + end + + context 'ssl' do + before do + stub_request(:get, 'http://secure.ex.com:443/abc/') + .to_return(status: 200, body: '', headers: {}) + end + + it 'should set http secure state' do + Net::HTTP.any_instance.should_receive(:use_ssl=).with(true) + Net::HTTP.any_instance.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) + response, url = subject.request('https://secure.ex.com/abc/') + response.should be_a Net::HTTPSuccess + url.should == 'https://secure.ex.com/abc/' + end end context 'redirect' do before do stub_request(:get, 'http://www.ex.com/abc')