require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "GoGetter" do describe "#parse_url" do it "returns a URI instance, given a URL string" do uri = GoGetter.parse_url('http://www.google.com/') uri.scheme.should == 'http' uri.host.should == 'www.google.com' uri.port.should == 80 uri.path.should == '/' end context "when the URL has no scheme" do it "prepends http" do uri = GoGetter.parse_url('www.google.com') uri.scheme.should == 'http' end end context "when the URL has no path" do it "appends '/'" do uri = GoGetter.parse_url('www.google.com') uri.path.should == '/' end end context "when the URL has a path" do it "retains the path" do uri = GoGetter.parse_url('www.google.com/search') uri.path.should == '/search' end end context "when the URL has a query part" do it "retains the query" do uri = GoGetter.parse_url('www.google.com/search?q=gogetter&hl=en') uri.query.should == 'q=gogetter&hl=en' end end end describe "#handle_redirection" do context "when the max_redirects param is 0" do it "should not send another request" do GoGetter.should_not_receive(:get) response = Net::HTTPRedirection.new('1.1', '302', 'Found') response['Location'] = 'http://www.google.co.il/' GoGetter.handle_redirection(URI.parse('http://www.google.com/'), response, {}, {max_redirects: 0}).should == response end end context "when the max_redirects param > 0" do it "should send another get request to the new location" do response = Net::HTTPRedirection.new('1.1', '302', 'Found') response['Location'] = 'http://www.google.co.il/' GoGetter.should_receive(:get).with(URI.parse('http://www.google.co.il'), {}, hash_including(:max_redirects=>0)) GoGetter.handle_redirection(URI.parse('http://www.google.com/'), response, {}, {max_redirects: 1}) end end context "when the response location is only a path (no host)" do it "uses the host from the original URI" do response = Net::HTTPRedirection.new('1.1', '302', 'Found') response['Location'] = '/new_target' exp_uri = URI.parse('/new_target') exp_uri.host = 'www.google.com'; exp_uri.port = 80; exp_uri.scheme = 'http' GoGetter.should_receive(:get).with(exp_uri, {}, hash_including(:max_redirects=>0)) GoGetter.handle_redirection(URI.parse('http://www.google.com/'), response, {}, {max_redirects: 1}) end end context "when the response redirects to a location that had already been got in the recursive chain" do it "should not send another request" do GoGetter.should_not_receive(:get) response = Net::HTTPRedirection.new('1.1', '302', 'Found') response['Location'] = 'http://www.google.co.il/' GoGetter.handle_redirection( URI.parse('http://www.google.com/'), response, {}, {:max_redirects=>4, :uris_seen => Set.new([URI.parse('http://www.google.co.il/'), URI.parse('http://www.yahoo.com/')])} ).should == response end end end describe "#get" do context "when given a URL" do it "should get it" do url = "http://google.html/" body = file_fixture('google.html') FakeWeb.register_uri(:get, url, :status => ['200', 'OK'], :body => body) response = GoGetter.get url response.should be_a(Net::HTTPOK) response.body.should == body end end context "when given a URI" do it "should get that too" do url = "http://google.html/" body = file_fixture('google.html') FakeWeb.register_uri(:get, url, :status => ['200', 'OK'], :body => body) response = GoGetter.get URI.parse(url) response.should be_a(Net::HTTPOK) response.body.should == body end end context "when given basic auth params" do it "should do basic authentication" do url = 'http://example.com/secret' url_auth = 'http://user:pass@example.com/secret' FakeWeb.register_uri(:get, url, :body => "Unauthorized", :status => ["401", "Unauthorized"]) FakeWeb.register_uri(:get, url_auth, :status => ['200', 'OK'], :body => "Authorized") GoGetter.get(url).should be_a(Net::HTTPUnauthorized) GoGetter.get(url, {}, {:auth_user => 'user', :auth_pass => 'pass'}).should be_a(Net::HTTPOK) end end context "when given proxy params" do it "should use a proxy" do url = "http://google.html/" #proxy: host = 'proxy.example.com'; port = '8080' user = 'user'; pass = 'pass' #proxy_class = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass) FakeWeb.register_uri(:get, url, :status => ['200', 'OK'], :body => file_fixture('google.html')) Net::HTTP.should_receive(:Proxy).with(host, port, user, pass) # I was unable to mock proxy behavior properly and #get keeps raising errors # However, this code still tests that a proxy class is created, which is the whole point expect { GoGetter.get(url, {}, {proxy_host: host,proxy_port: port,proxy_user: user,proxy_pass: pass}) }.to raise_error end end context "when the response is a redirect" do it "does redirection" do url1 = "http://www.google.com/" url2 = "http://www.google.co.il/" body = file_fixture('google.redirect.html') FakeWeb.register_uri(:get, url1, :status => ['302','Found'], :headers => {'Location'=>url2},:body => body) params = {max_redirects: 1} GoGetter.should_receive(:handle_redirection).with(URI.parse(url1), an_instance_of(Net::HTTPFound), {}, params) GoGetter.get(url1, {}, params) end end end end