spec/session_spec.rb in patron-0.4.20 vs spec/session_spec.rb in patron-0.5.0

- old
+ new

@@ -32,248 +32,259 @@ before(:each) do @session = Patron::Session.new @session.base_url = "http://localhost:9001" end + it "should work when forcing ipv4" do + @session.force_ipv4 = true + expect { @session.get("/test") }.to_not raise_error + end + it "should escape and unescape strings symetrically" do string = "foo~bar baz/" escaped = @session.escape(string) unescaped = @session.unescape(escaped) - unescaped.should == string + expect(unescaped).to be == string end it "should raise an error when passed an invalid action" do - lambda { @session.request(:bogus, "/test", {}) }.should raise_error(ArgumentError) + expect { @session.request(:bogus, "/test", {}) }.to raise_error(ArgumentError) end it "should raise an error when no URL is provided" do @session.base_url = nil - lambda {@session.get(nil)}.should raise_error(ArgumentError) + expect {@session.get(nil)}.to raise_error(ArgumentError) end it "should retrieve a url with :get" do response = @session.get("/test") body = YAML::load(response.body) - body.request_method.should == "GET" + expect(body.request_method).to be == "GET" end + it "should use full base url" do + @session.base_url = "http://localhost:9001/api/v1" + response = @session.get("/test") + expect(response.url).to be == "http://localhost:9001/api/v1/test" + end + it 'should ignore #base_url when a full URL is provided' do @session.base_url = "http://example.com:123" - lambda { @session.get("http://localhost:9001/test") }.should_not raise_error(URI::InvalidURIError) + expect { @session.get("http://localhost:9001/test") }.to_not raise_error(URI::InvalidURIError) end it "should download content with :get and a file path" do tmpfile = "/tmp/patron_test.yaml" response = @session.get_file "/test", tmpfile - response.body.should be_nil + expect(response.body).to be_nil body = YAML::load_file(tmpfile) - body.request_method.should == "GET" + expect(body.request_method).to be == "GET" FileUtils.rm tmpfile end it "should download correctly(md5 ok) with get_file" do tmpfile = "/tmp/picture" response = @session.get_file "/picture", tmpfile - response.body.should be_nil - File.size(File.join(File.dirname(__FILE__),"../pic.png")).should == File.size(tmpfile) + expect(response.body).to be_nil + expect(File.size(File.join(File.dirname(__FILE__),"../pic.png"))).to be == File.size(tmpfile) FileUtils.rm tmpfile end it "should include custom headers in a request" do response = @session.get("/test", {"User-Agent" => "PatronTest"}) body = YAML::load(response.body) - body.header["user-agent"].should == ["PatronTest"] + expect(body.header["user-agent"]).to be == ["PatronTest"] end it "should include default headers in a request, if they were defined" do @session.headers = {"User-Agent" => "PatronTest"} response = @session.get("/test") body = YAML::load(response.body) - body.header["user-agent"].should == ["PatronTest"] + expect(body.header["user-agent"]).to be == ["PatronTest"] end it "should merge custom headers with session headers" do @session.headers["X-Test"] = "Testing" response = @session.get("/test", {"User-Agent" => "PatronTest"}) body = YAML::load(response.body) - body.header["user-agent"].should == ["PatronTest"] - body.header["x-test"].should == ["Testing"] + expect(body.header["user-agent"]).to be == ["PatronTest"] + expect(body.header["x-test"]).to be == ["Testing"] end it "should raise an exception on timeout" do @session.timeout = 1 - lambda {@session.get("/timeout")}.should raise_error(Patron::TimeoutError) + expect {@session.get("/timeout")}.to raise_error(Patron::TimeoutError) end it "should follow redirects by default" do @session.max_redirects = 1 response = @session.get("/redirect") body = YAML::load(response.body) - response.status.should == 200 - body.path.should == "/test" + expect(response.status).to be == 200 + expect(body.path).to be == "/test" end it "should include redirect count in response" do @session.max_redirects = 1 response = @session.get("/redirect") - response.redirect_count.should == 1 + expect(response.redirect_count).to be == 1 end it "should not follow redirects when configured to do so" do @session.max_redirects = 0 response = @session.get("/redirect") - response.status.should == 301 - response.body.should be_empty + expect(response.status).to be == 301 + expect(response.body).to be_empty end it "should retrieve URL metadata with :head" do response = @session.head("/test") - response.status.should == 200 - response.body.should be_empty - response.headers.should_not be_empty + expect(response.status).to be == 200 + expect(response.body).to be_empty + expect(response.headers).to_not be_empty end it "should send a delete request with :delete" do response = @session.delete("/test") body = YAML::load(response.body) - body.request_method.should == "DELETE" + expect(body.request_method).to be == "DELETE" end it "should send a COPY request with :copy" do response = @session.copy("/test", "/test2") body = YAML::load(response.body) - body.request_method.should == "COPY" + expect(body.request_method).to be == "COPY" end it "should include a Destination header in COPY requests" do response = @session.copy("/test", "/test2") body = YAML::load(response.body) - body.header['destination'].first.should == "/test2" + expect(body.header['destination'].first).to be == "/test2" end it "should upload data with :get" do data = "upload data" response = @session.request(:get, "/test", {}, :data => data) body = YAML::load(response.body) - body.request_method.should == "GET" - body.header['content-length'].should == [data.size.to_s] + expect(body.request_method).to be == "GET" + expect(body.header['content-length']).to be == [data.size.to_s] end it "should upload data with :put" do data = "upload data" response = @session.put("/test", data) body = YAML::load(response.body) - body.request_method.should == "PUT" - body.header['content-length'].should == [data.size.to_s] + expect(body.request_method).to be == "PUT" + expect(body.header['content-length']).to be == [data.size.to_s] end it "should upload data with :delete" do data = "upload data" response = @session.request(:delete, "/test", {}, :data => data) body = YAML::load(response.body) - body.request_method.should == "DELETE" - body.header['content-length'].should == [data.size.to_s] + expect(body.request_method).to be == "DELETE" + expect(body.header['content-length']).to be == [data.size.to_s] end it "should raise when no data is provided to :put" do - lambda { @session.put("/test", nil) }.should raise_error(ArgumentError) + expect { @session.put("/test", nil) }.to raise_error(ArgumentError) end it "should upload a file with :put" do response = @session.put_file("/test", "LICENSE") body = YAML::load(response.body) - body.request_method.should == "PUT" + expect(body.request_method).to be == "PUT" end it "should raise when no file is provided to :put" do - lambda { @session.put_file("/test", nil) }.should raise_error(ArgumentError) + expect { @session.put_file("/test", nil) }.to raise_error(ArgumentError) end it "should use chunked encoding when uploading a file with :put" do response = @session.put_file("/test", "LICENSE") body = YAML::load(response.body) - body.header['transfer-encoding'].first.should == "chunked" + expect(body.header['transfer-encoding'].first).to be == "chunked" end it "should upload data with :post" do data = "upload data" response = @session.post("/test", data) body = YAML::load(response.body) - body.request_method.should == "POST" - body.header['content-length'].should == [data.size.to_s] + expect(body.request_method).to be == "POST" + expect(body.header['content-length']).to be == [data.size.to_s] end it "should post a hash of arguments as a urlencoded form" do data = {:foo => 123, 'baz' => '++hello world++'} response = @session.post("/testpost", data) body = YAML::load(response.body) - body['content_type'].should == "application/x-www-form-urlencoded" - body['body'].should match(/baz=%2B%2Bhello%20world%2B%2B/) - body['body'].should match(/foo=123/) + expect(body['content_type']).to be == "application/x-www-form-urlencoded" + expect(body['body']).to match(/baz=%2B%2Bhello%20world%2B%2B/) + expect(body['body']).to match(/foo=123/) end it "should raise when no data is provided to :post" do - lambda { @session.post("/test", nil) }.should raise_error(ArgumentError) + expect { @session.post("/test", nil) }.to raise_error(ArgumentError) end it "should upload a file with :post" do response = @session.post_file("/test", "LICENSE") body = YAML::load(response.body) - body.request_method.should == "POST" + expect(body.request_method).to be == "POST" end it "should upload a multipart with :post" do response = @session.post_multipart("/test", { :test_data => "123" }, { :test_file => "LICENSE" } ) body = YAML::load(response.body) - body.request_method.should == "POST" + expect(body.request_method).to be == "POST" end it "should raise when no file is provided to :post" do - lambda { @session.post_file("/test", nil) }.should raise_error(ArgumentError) + expect { @session.post_file("/test", nil) }.to raise_error(ArgumentError) end it "should use chunked encoding when uploading a file with :post" do response = @session.post_file("/test", "LICENSE") body = YAML::load(response.body) - body.header['transfer-encoding'].first.should == "chunked" + expect(body.header['transfer-encoding'].first).to be == "chunked" end it "should pass credentials as http basic auth" do @session.username = "foo" @session.password = "bar" response = @session.get("/test") body = YAML::load(response.body) - body.header['authorization'].should == [encode_authz("foo", "bar")] + expect(body.header['authorization']).to be == [encode_authz("foo", "bar")] end it "should handle cookies if set" do @session.handle_cookies response = @session.get("/setcookie").body - YAML::load(response).header['cookie'].first.should == "session_id=foo123" + expect(YAML::load(response).header['cookie'].first).to be == "session_id=foo123" end it "should not handle cookies by default" do response = @session.get("/setcookie").body - YAML::load(response).header.should_not include('cookie') + expect(YAML::load(response).header).to_not include('cookie') end it "should ignore a wrong Content-Length when asked to" do - lambda { + expect { @session.ignore_content_length = true @session.get("/wrongcontentlength") - }.should_not raise_error + }.to_not raise_error end it "should fail by default with a Content-Length too high" do - lambda { + expect { @session.ignore_content_length = nil @session.get("/wrongcontentlength") - }.should raise_error(Patron::PartialFileError) + }.to raise_error(Patron::PartialFileError) end it "should raise exception if cookie store is not writable or readable" do - lambda { @session.handle_cookies("/trash/clash/foo") }.should raise_error(ArgumentError) + expect { @session.handle_cookies("/trash/clash/foo") }.to raise_error(ArgumentError) end it "should work with multiple threads" do threads = [] 3.times do @@ -294,45 +305,112 @@ # to have a more robust test here. @session.buffer_size = 1 body = nil - lambda { + expect { response = @session.get("/test") body = YAML::load(response.body) - }.should_not raise_error + }.to_not raise_error - body.request_method.should == "GET" + expect(body.request_method).to be == "GET" end it "should serialize query params and append them to the url" do response = @session.request(:get, "/test", {}, :query => {:foo => "bar"}) request = YAML::load(response.body) request.parse - (request.path + '?' + request.query_string).should == "/test?foo=bar" + expect(request.path + '?' + request.query_string).to be == "/test?foo=bar" end it "should merge parameters in the :query option with pre-existing query parameters" do response = @session.request(:get, "/test?foo=bar", {}, :query => {:baz => "quux"}) request = YAML::load(response.body) request.parse - (request.path + '?' + request.query_string).should == "/test?foo=bar&baz=quux" + expect(request.path + '?' + request.query_string).to be == "/test?foo=bar&baz=quux" end def encode_authz(user, passwd) "Basic " + Base64.encode64("#{user}:#{passwd}").strip end + describe 'when instantiating with hash arguments' do + + let(:args) { { + :timeout => 10, + :base_url => 'http://localhost:9001', + :headers => {'User-Agent' => 'myapp/1.0'} + } } + + let(:session) { Patron::Session.new(args) } + + it 'sets the base_url' do + expect(session.base_url).to be == args[:base_url] + end + + it 'sets timeout' do + expect(session.timeout).to be == args[:timeout] + end + + it 'sets headers' do + expect(session.headers).to be == args[:headers] + end + + context 'when given an incorrect accessor name' do + let(:args) { { :not_a_real_accessor => 'http://localhost:9001' }} + it 'raises no method error' do + expect { session }.to raise_error NoMethodError + end + end + + end + + describe 'when instantiating with a block' do + args = { + :timeout => 10, + :base_url => 'http://localhost:9001', + :headers => {'User-Agent' => 'myapp/1.0'} + } + + session = Patron::Session.new do |patron| + patron.timeout = args[:timeout] + patron.base_url = args[:base_url] + patron.headers = args[:headers] + end + + it 'sets the base_url' do + expect(session.base_url).to be == args[:base_url] + end + + it 'sets timeout' do + expect(session.timeout).to be == args[:timeout] + end + + it 'sets headers' do + expect(session.headers).to be == args[:headers] + end + + context 'when given an incorrect accessor name' do + it 'raises no method error' do + expect { + Patron::Session.new do |patron| + patron.timeoutttt = args[:timeout] + end + }.to raise_error NoMethodError + end + end + end + # ------------------------------------------------------------------------ describe 'when debug is enabled' do it 'it should not clobber stderr' do rdev = STDERR.stat.rdev @session.enable_debug - STDERR.stat.rdev.should be == rdev + expect(STDERR.stat.rdev).to be == rdev @session.enable_debug - STDERR.stat.rdev.should be == rdev + expect(STDERR.stat.rdev).to be == rdev end end end