1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

convert == to eq to avoid warnings, (all were done for concistency)

(cherry picked from commit 0b6e74593a)
Signed-off-by: Larry Gilbert <larry@l2g.to>

Conflicts:
	spec/request_spec.rb
This commit is contained in:
Jon Rowe 2013-08-11 21:16:02 +01:00 committed by Larry Gilbert
parent 3e6504be32
commit c6a791f752
9 changed files with 133 additions and 133 deletions

View file

@ -22,7 +22,7 @@ describe RestClient::AbstractResponse do
it "fetches the numeric response code" do it "fetches the numeric response code" do
@net_http_res.should_receive(:code).and_return('200') @net_http_res.should_receive(:code).and_return('200')
@response.code.should == 200 @response.code.should eq 200
end end
it "has a nice description" do it "has a nice description" do
@ -33,36 +33,36 @@ describe RestClient::AbstractResponse do
it "beautifies the headers by turning the keys to symbols" do it "beautifies the headers by turning the keys to symbols" do
h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ]) h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ])
h.keys.first.should == :content_type h.keys.first.should eq :content_type
end end
it "beautifies the headers by turning the values to strings instead of one-element arrays" do it "beautifies the headers by turning the values to strings instead of one-element arrays" do
h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] ) h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] )
h.values.first.should == 'text/html' h.values.first.should eq 'text/html'
end end
it "fetches the headers" do it "fetches the headers" do
@net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ]) @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
@response.headers.should == { :content_type => 'text/html' } @response.headers.should eq({ :content_type => 'text/html' })
end end
it "extracts cookies from response headers" do it "extracts cookies from response headers" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
@response.cookies.should == { 'session_id' => '1' } @response.cookies.should eq({ 'session_id' => '1' })
end end
it "extract strange cookies" do it "extract strange cookies" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']) @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
@response.cookies.should == { 'session_id' => 'ZJ%2FHQVH6YE+rVkTpn0zvTQ%3D%3D' } @response.cookies.should eq({ 'session_id' => 'ZJ%2FHQVH6YE+rVkTpn0zvTQ%3D%3D' })
end end
it "doesn't escape cookies" do it "doesn't escape cookies" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/']) @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/'])
@response.cookies.should == { 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' } @response.cookies.should eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' })
end end
it "can access the net http result directly" do it "can access the net http result directly" do
@response.net_http_res.should == @net_http_res @response.net_http_res.should eq @net_http_res
end end
describe "#return!" do describe "#return!" do

View file

@ -6,18 +6,18 @@ include WebMock::API
describe RestClient::Exception do describe RestClient::Exception do
it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
e = RestClient::Exception.new e = RestClient::Exception.new
e.message.should == "RestClient::Exception" e.message.should eq "RestClient::Exception"
end end
it "returns the 'message' that was set" do it "returns the 'message' that was set" do
e = RestClient::Exception.new e = RestClient::Exception.new
message = "An explicitly set message" message = "An explicitly set message"
e.message = message e.message = message
e.message.should == message e.message.should eq message
end end
it "sets the exception message to ErrorMessage" do it "sets the exception message to ErrorMessage" do
RestClient::ResourceNotFound.new.message.should == 'Resource Not Found' RestClient::ResourceNotFound.new.message.should eq 'Resource Not Found'
end end
it "contains exceptions in RestClient" do it "contains exceptions in RestClient" do
@ -29,7 +29,7 @@ end
describe RestClient::ServerBrokeConnection do describe RestClient::ServerBrokeConnection do
it "should have a default message of 'Server broke connection'" do it "should have a default message of 'Server broke connection'" do
e = RestClient::ServerBrokeConnection.new e = RestClient::ServerBrokeConnection.new
e.message.should == 'Server broke connection' e.message.should eq 'Server broke connection'
end end
end end
@ -43,17 +43,17 @@ describe RestClient::RequestFailed do
begin begin
raise RestClient::RequestFailed, response raise RestClient::RequestFailed, response
rescue RestClient::RequestFailed => e rescue RestClient::RequestFailed => e
e.response.should == response e.response.should eq response
end end
end end
it "http_code convenience method for fetching the code as an integer" do it "http_code convenience method for fetching the code as an integer" do
RestClient::RequestFailed.new(@response).http_code.should == 502 RestClient::RequestFailed.new(@response).http_code.should eq 502
end end
it "http_body convenience method for fetching the body (decoding when necessary)" do it "http_body convenience method for fetching the body (decoding when necessary)" do
RestClient::RequestFailed.new(@response).http_code.should == 502 RestClient::RequestFailed.new(@response).http_code.should eq 502
RestClient::RequestFailed.new(@response).message.should == 'HTTP status code 502' RestClient::RequestFailed.new(@response).message.should eq 'HTTP status code 502'
end end
it "shows the status code in the message" do it "shows the status code in the message" do
@ -67,22 +67,22 @@ describe RestClient::ResourceNotFound do
begin begin
raise RestClient::ResourceNotFound, response raise RestClient::ResourceNotFound, response
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.response.should == response e.response.should eq response
end end
end end
end end
describe "backwards compatibility" do describe "backwards compatibility" do
it "alias RestClient::Request::Redirect to RestClient::Redirect" do it "alias RestClient::Request::Redirect to RestClient::Redirect" do
RestClient::Request::Redirect.should == RestClient::Redirect RestClient::Request::Redirect.should eq RestClient::Redirect
end end
it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
RestClient::Request::Unauthorized.should == RestClient::Unauthorized RestClient::Request::Unauthorized.should eq RestClient::Unauthorized
end end
it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
RestClient::Request::RequestFailed.should == RestClient::RequestFailed RestClient::Request::RequestFailed.should eq RestClient::RequestFailed
end end
it "make the exception's response act like an Net::HTTPResponse" do it "make the exception's response act like an Net::HTTPResponse" do
@ -92,7 +92,7 @@ describe "backwards compatibility" do
RestClient.get "www.example.com" RestClient.get "www.example.com"
raise raise
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.response.body.should == body e.response.body.should eq body
end end
end end
end end

View file

@ -9,15 +9,15 @@ describe RestClient do
body = 'abc' body = 'abc'
stub_request(:get, "www.example.com").to_return(:body => body, :status => 200) stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.code.should == 200 response.code.should eq 200
response.body.should == body response.body.should eq body
end end
it "a simple request with gzipped content" do it "a simple request with gzipped content" do
stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } ) stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } )
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.code.should == 200 response.code.should eq 200
response.body.should == "i'm gziped\n" response.body.should eq "i'm gziped\n"
end end
it "a 404" do it "a 404" do
@ -27,10 +27,10 @@ describe RestClient do
RestClient.get "www.example.com" RestClient.get "www.example.com"
raise raise
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.http_code.should == 404 e.http_code.should eq 404
e.response.code.should == 404 e.response.code.should eq 404
e.response.body.should == body e.response.body.should eq body
e.http_body.should == body e.http_body.should eq body
end end
end end

View file

@ -5,26 +5,26 @@ describe RestClient::Payload do
context "A regular Payload" do context "A regular Payload" do
it "should use standard enctype as default content-type" do it "should use standard enctype as default content-type" do
RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']. RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
should == 'application/x-www-form-urlencoded' should eq 'application/x-www-form-urlencoded'
end end
it "should form properly encoded params" do it "should form properly encoded params" do
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s. RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
should == "foo=bar" should eq "foo=bar"
["foo=bar&baz=qux", "baz=qux&foo=bar"].should include( ["foo=bar&baz=qux", "baz=qux&foo=bar"].should include(
RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s) RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
end end
it "should escape parameters" do it "should escape parameters" do
RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'}).to_s. RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'}).to_s.
should == "foo%20=bar" should eq "foo%20=bar"
end end
it "should properly handle hashes as parameter" do it "should properly handle hashes as parameter" do
RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s. RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s.
should == "foo[bar]=baz" should eq "foo[bar]=baz"
RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s. RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s.
should == "foo[bar][baz]=qux" should eq "foo[bar][baz]=qux"
end end
it "should handle many attributes inside a hash" do it "should handle many attributes inside a hash" do
@ -44,16 +44,16 @@ describe RestClient::Payload do
it "should form properly use symbols as parameters" do it "should form properly use symbols as parameters" do
RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s. RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s.
should == "foo=bar" should eq "foo=bar"
RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s. RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s.
should == "foo[bar]=baz" should eq "foo[bar]=baz"
end end
it "should properly handle arrays as repeated parameters" do it "should properly handle arrays as repeated parameters" do
RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s. RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s.
should == "foo[]=bar" should eq "foo[]=bar"
RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s. RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s.
should == "foo[]=bar&foo[]=baz" should eq "foo[]=bar&foo[]=baz"
end end
it 'should not close if stream already closed' do it 'should not close if stream already closed' do
@ -67,7 +67,7 @@ describe RestClient::Payload do
it "should use standard enctype as default content-type" do it "should use standard enctype as default content-type" do
m = RestClient::Payload::Multipart.new({}) m = RestClient::Payload::Multipart.new({})
m.stub(:boundary).and_return(123) m.stub(:boundary).and_return(123)
m.headers['Content-Type'].should == 'multipart/form-data; boundary=123' m.headers['Content-Type'].should eq 'multipart/form-data; boundary=123'
end end
it 'should not error on close if stream already closed' do it 'should not error on close if stream already closed' do
@ -77,7 +77,7 @@ describe RestClient::Payload do
it "should form properly separated multipart data" do it "should form properly separated multipart data" do
m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]]) m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar"\r Content-Disposition: form-data; name="bar"\r
\r \r
@ -92,7 +92,7 @@ bar\r
it "should not escape parameters names" do it "should not escape parameters names" do
m = RestClient::Payload::Multipart.new([["bar ", "baz"]]) m = RestClient::Payload::Multipart.new([["bar ", "baz"]])
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar "\r Content-Disposition: form-data; name="bar "\r
\r \r
@ -104,7 +104,7 @@ baz\r
it "should form properly separated multipart data" do it "should form properly separated multipart data" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
@ -117,7 +117,7 @@ Content-Type: image/jpeg\r
it "should ignore the name attribute when it's not set" do it "should ignore the name attribute when it's not set" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
m = RestClient::Payload::Multipart.new({nil => f}) m = RestClient::Payload::Multipart.new({nil => f})
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; filename="master_shake.jpg"\r Content-Disposition: form-data; filename="master_shake.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
@ -132,7 +132,7 @@ Content-Type: image/jpeg\r
f.instance_eval "def content_type; 'text/plain'; end" f.instance_eval "def content_type; 'text/plain'; end"
f.instance_eval "def original_filename; 'foo.txt'; end" f.instance_eval "def original_filename; 'foo.txt'; end"
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo"; filename="foo.txt"\r Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
Content-Type: text/plain\r Content-Type: text/plain\r
@ -144,7 +144,7 @@ Content-Type: text/plain\r
it "should handle hash in hash parameters" do it "should handle hash in hash parameters" do
m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}}) m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar[baz]"\r Content-Disposition: form-data; name="bar[baz]"\r
\r \r
@ -156,7 +156,7 @@ foo\r
f.instance_eval "def content_type; 'text/plain'; end" f.instance_eval "def content_type; 'text/plain'; end"
f.instance_eval "def original_filename; 'foo.txt'; end" f.instance_eval "def original_filename; 'foo.txt'; end"
m = RestClient::Payload::Multipart.new({:foo => {:bar => f}}) m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
m.to_s.should == <<-EOS m.to_s.should eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
Content-Type: text/plain\r Content-Type: text/plain\r
@ -172,23 +172,23 @@ Content-Type: text/plain\r
it "should properly determine the size of file payloads" do it "should properly determine the size of file payloads" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
payload.size.should == 76_988 payload.size.should eq 76_988
payload.length.should == 76_988 payload.length.should eq 76_988
end end
it "should properly determine the size of other kinds of streaming payloads" do it "should properly determine the size of other kinds of streaming payloads" do
s = StringIO.new 'foo' s = StringIO.new 'foo'
payload = RestClient::Payload.generate(s) payload = RestClient::Payload.generate(s)
payload.size.should == 3 payload.size.should eq 3
payload.length.should == 3 payload.length.should eq 3
begin begin
f = Tempfile.new "rest-client" f = Tempfile.new "rest-client"
f.write 'foo bar' f.write 'foo bar'
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
payload.size.should == 7 payload.size.should eq 7
payload.length.should == 7 payload.length.should eq 7
ensure ensure
f.close f.close
end end

View file

@ -8,10 +8,10 @@ describe RestClient::RawResponse do
end end
it "behaves like string" do it "behaves like string" do
@response.to_s.should == 'the answer is 42' @response.to_s.should eq 'the answer is 42'
end end
it "exposes a Tempfile" do it "exposes a Tempfile" do
@response.file.should == @tf @response.file.should eq @tf
end end
end end

View file

@ -7,10 +7,10 @@ describe RestClient::Request do
it "manage params for get requests" do it "manage params for get requests" do
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should == 'foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should eq 'foo'
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar', 'params' => 'a'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar', 'params' => 'a'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should == 'foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should eq 'foo'
end end
it "can use a block to process response" do it "can use a block to process response" do
@ -20,7 +20,7 @@ describe RestClient::Request do
end end
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block) RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
response_value.should == "foo" response_value.should eq "foo"
end end
it 'closes payload if not nil' do it 'closes payload if not nil' do

View file

@ -22,13 +22,13 @@ describe RestClient::Request do
end end
it "accept */* mimetype, preferring xml" do it "accept */* mimetype, preferring xml" do
@request.default_headers[:accept].should == '*/*; q=0.5, application/xml' @request.default_headers[:accept].should eq '*/*; q=0.5, application/xml'
end end
describe "compression" do describe "compression" do
it "decodes an uncompressed result body by passing it straight through" do it "decodes an uncompressed result body by passing it straight through" do
RestClient::Request.decode(nil, 'xyz').should == 'xyz' RestClient::Request.decode(nil, 'xyz').should eq 'xyz'
end end
it "doesn't fail for nil bodies" do it "doesn't fail for nil bodies" do
@ -37,7 +37,7 @@ describe RestClient::Request do
it "decodes a gzip body" do it "decodes a gzip body" do
RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n" RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should eq "i'm gziped\n"
end end
it "ingores gzip for empty bodies" do it "ingores gzip for empty bodies" do
@ -45,7 +45,7 @@ describe RestClient::Request do
end end
it "decodes a deflated body" do it "decodes a deflated body" do
RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text" RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should eq "some deflated text"
end end
end end
@ -54,8 +54,8 @@ describe RestClient::Request do
res.stub(:code).and_return("200") res.stub(:code).and_return("200")
res.stub(:body).and_return('body') res.stub(:body).and_return('body')
res.stub(:[]).with('content-encoding').and_return(nil) res.stub(:[]).with('content-encoding').and_return(nil)
@request.process_result(res).body.should == 'body' @request.process_result(res).body.should eq 'body'
@request.process_result(res).to_s.should == 'body' @request.process_result(res).to_s.should eq 'body'
end end
it "doesn't classify successful requests as failed" do it "doesn't classify successful requests as failed" do
@ -82,23 +82,23 @@ describe RestClient::Request do
it "extracts the username and password when parsing http://user:password@example.com/" do it "extracts the username and password when parsing http://user:password@example.com/" do
URI.stub(:parse).and_return(double('uri', :user => 'joe', :password => 'pass1')) URI.stub(:parse).and_return(double('uri', :user => 'joe', :password => 'pass1'))
@request.parse_url_with_auth('http://joe:pass1@example.com/resource') @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
@request.user.should == 'joe' @request.user.should eq 'joe'
@request.password.should == 'pass1' @request.password.should eq 'pass1'
end end
it "extracts with escaping the username and password when parsing http://user:password@example.com/" do it "extracts with escaping the username and password when parsing http://user:password@example.com/" do
URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1')) URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1'))
@request.parse_url_with_auth('http://joe%20:pass1@example.com/resource') @request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
@request.user.should == 'joe ' @request.user.should eq 'joe '
@request.password.should == 'pass1' @request.password.should eq 'pass1'
end end
it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil)) URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil))
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2') @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
@request.parse_url_with_auth('http://example.com/resource') @request.parse_url_with_auth('http://example.com/resource')
@request.user.should == 'beth' @request.user.should eq 'beth'
@request.password.should == 'pass2' @request.password.should eq 'pass2'
end end
end end
@ -106,11 +106,11 @@ describe RestClient::Request do
URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil)) URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil))
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" }) @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
@request.should_receive(:default_headers).and_return({'Foo' => 'bar'}) @request.should_receive(:default_headers).and_return({'Foo' => 'bar'})
@request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'} @request.make_headers({}).should eq({ 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'})
end end
it "determines the Net::HTTP class to instantiate by the method name" do it "determines the Net::HTTP class to instantiate by the method name" do
@request.net_http_request_class(:put).should == Net::HTTP::Put @request.net_http_request_class(:put).should eq Net::HTTP::Put
end end
describe "user headers" do describe "user headers" do
@ -118,16 +118,16 @@ describe RestClient::Request do
@request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' }) @request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' })
headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip') headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
headers.should have_key "Accept-Encoding" headers.should have_key "Accept-Encoding"
headers["Accept-Encoding"].should == "gzip" headers["Accept-Encoding"].should eq "gzip"
headers.should have_key "Accept" headers.should have_key "Accept"
headers["Accept"].should == "application/json" headers["Accept"].should eq "application/json"
end end
it "prefers the user header when the same header exists in the defaults" do it "prefers the user header when the same header exists in the defaults" do
@request.should_receive(:default_headers).and_return({ '1' => '2' }) @request.should_receive(:default_headers).and_return({ '1' => '2' })
headers = @request.make_headers('1' => '3') headers = @request.make_headers('1' => '3')
headers.should have_key('1') headers.should have_key('1')
headers['1'].should == '3' headers['1'].should eq '3'
end end
end end
@ -137,37 +137,37 @@ describe RestClient::Request do
@request.should_receive(:default_headers).and_return({}) @request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:content_type => 'abc') headers = @request.make_headers(:content_type => 'abc')
headers.should have_key('Content-Type') headers.should have_key('Content-Type')
headers['Content-Type'].should == 'abc' headers['Content-Type'].should eq 'abc'
end end
it "converts content-type from extension to real content-type" do it "converts content-type from extension to real content-type" do
@request.should_receive(:default_headers).and_return({}) @request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:content_type => 'json') headers = @request.make_headers(:content_type => 'json')
headers.should have_key('Content-Type') headers.should have_key('Content-Type')
headers['Content-Type'].should == 'application/json' headers['Content-Type'].should eq 'application/json'
end end
it "converts accept from extension(s) to real content-type(s)" do it "converts accept from extension(s) to real content-type(s)" do
@request.should_receive(:default_headers).and_return({}) @request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:accept => 'json, mp3') headers = @request.make_headers(:accept => 'json, mp3')
headers.should have_key('Accept') headers.should have_key('Accept')
headers['Accept'].should == 'application/json, audio/mpeg' headers['Accept'].should eq 'application/json, audio/mpeg'
@request.should_receive(:default_headers).and_return({}) @request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:accept => :json) headers = @request.make_headers(:accept => :json)
headers.should have_key('Accept') headers.should have_key('Accept')
headers['Accept'].should == 'application/json' headers['Accept'].should eq 'application/json'
end end
it "only convert symbols in header" do it "only convert symbols in header" do
@request.should_receive(:default_headers).and_return({}) @request.should_receive(:default_headers).and_return({})
headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'}) headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'})
headers['Foo-Bar'].should == 'value' headers['Foo-Bar'].should eq 'value'
headers['bar_bar'].should == 'value' headers['bar_bar'].should eq 'value'
end end
it "converts header values to strings" do it "converts header values to strings" do
@request.make_headers('A' => 1)['A'].should == '1' @request.make_headers('A' => 1)['A'].should eq '1'
end end
end end
@ -195,11 +195,11 @@ describe RestClient::Request do
end end
it "passes non-hash payloads straight through" do it "passes non-hash payloads straight through" do
@request.process_payload("x").should == "x" @request.process_payload("x").should eq "x"
end end
it "converts a hash payload to urlencoded data" do it "converts a hash payload to urlencoded data" do
@request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd" @request.process_payload(:a => 'b c+d').should eq "a=b%20c%2Bd"
end end
it "accepts nested hashes in payload" do it "accepts nested hashes in payload" do
@ -212,7 +212,7 @@ describe RestClient::Request do
it "set urlencoded content_type header on hash payloads" do it "set urlencoded content_type header on hash payloads" do
@request.process_payload(:a => 1) @request.process_payload(:a => 1)
@request.headers[:content_type].should == 'application/x-www-form-urlencoded' @request.headers[:content_type].should eq 'application/x-www-form-urlencoded'
end end
describe "credentials" do describe "credentials" do
@ -276,7 +276,7 @@ describe RestClient::Request do
describe "block usage" do describe "block usage" do
it "returns what asked to" do it "returns what asked to" do
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' ) res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
@request.process_result(res){|response, request| "foo"}.should == "foo" @request.process_result(res){|response, request| "foo"}.should eq "foo"
end end
end end
@ -296,25 +296,25 @@ describe RestClient::Request do
it "logs a get request" do it "logs a get request" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url').log_request RestClient::Request.new(:method => :get, :url => 'http://url').log_request
log[0].should == %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n} log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n}
end end
it "logs a post request with a small payload" do it "logs a post request with a small payload" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
log[0].should == %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n} log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n}
end end
it "logs a post request with a large payload" do it "logs a post request with a large payload" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
log[0].should == %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n} log[0].should eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n}
end end
it "logs input headers as a hash" do it "logs input headers as a hash" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
log[0].should == %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n} log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n}
end end
it "logs a response including the status code, content type, and result body size in bytes" do it "logs a response including the status code, content type, and result body size in bytes" do
@ -322,7 +322,7 @@ describe RestClient::Request do
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub(:[]).with('Content-type').and_return('text/html') res.stub(:[]).with('Content-type').and_return('text/html')
@request.log_response res @request.log_response res
log[0].should == "# => 200 OK | text/html 4 bytes\n" log[0].should eq "# => 200 OK | text/html 4 bytes\n"
end end
it "logs a response with a nil Content-type" do it "logs a response with a nil Content-type" do
@ -330,7 +330,7 @@ describe RestClient::Request do
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub(:[]).with('Content-type').and_return(nil) res.stub(:[]).with('Content-type').and_return(nil)
@request.log_response res @request.log_response res
log[0].should == "# => 200 OK | 4 bytes\n" log[0].should eq "# => 200 OK | 4 bytes\n"
end end
it "logs a response with a nil body" do it "logs a response with a nil body" do
@ -338,7 +338,7 @@ describe RestClient::Request do
res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil) res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil)
res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8') res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
@request.log_response res @request.log_response res
log[0].should == "# => 200 OK | text/html 0 bytes\n" log[0].should eq "# => 200 OK | text/html 0 bytes\n"
end end
end end
@ -347,7 +347,7 @@ describe RestClient::Request do
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8') res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
@request.log_response res @request.log_response res
log[0].should == "# => 200 OK | text/html 4 bytes\n" log[0].should eq "# => 200 OK | text/html 4 bytes\n"
end end
describe "timeout" do describe "timeout" do
@ -385,7 +385,7 @@ describe RestClient::Request do
end end
it "should default to not verifying ssl certificates" do it "should default to not verifying ssl certificates" do
@request.verify_ssl.should == false @request.verify_ssl.should eq false
end end
it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
@ -523,6 +523,6 @@ describe RestClient::Request do
@http.should_receive(:request).and_return(@request.fetch_body(net_http_res)) @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
response = @request.transmit(@uri, 'req', 'payload') response = @request.transmit(@uri, 'req', 'payload')
response.should_not be_nil response.should_not be_nil
response.code.should == 204 response.code.should eq 204
end end
end end

View file

@ -51,56 +51,56 @@ describe RestClient::Resource do
it "is backwards compatible with previous constructor" do it "is backwards compatible with previous constructor" do
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass') @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
@resource.user.should == 'user' @resource.user.should eq 'user'
@resource.password.should == 'pass' @resource.password.should eq 'pass'
end end
it "concatenates urls, inserting a slash when it needs one" do it "concatenates urls, inserting a slash when it needs one" do
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource' @resource.concat_urls('http://example.com', 'resource').should eq 'http://example.com/resource'
end end
it "concatenates urls, using no slash if the first url ends with a slash" do it "concatenates urls, using no slash if the first url ends with a slash" do
@resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource' @resource.concat_urls('http://example.com/', 'resource').should eq 'http://example.com/resource'
end end
it "concatenates urls, using no slash if the second url starts with a slash" do it "concatenates urls, using no slash if the second url starts with a slash" do
@resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource' @resource.concat_urls('http://example.com', '/resource').should eq 'http://example.com/resource'
end end
it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
@resource.concat_urls(:posts, 1).should == 'posts/1' @resource.concat_urls(:posts, 1).should eq 'posts/1'
end end
it "offers subresources via []" do it "offers subresources via []" do
parent = RestClient::Resource.new('http://example.com') parent = RestClient::Resource.new('http://example.com')
parent['posts'].url.should == 'http://example.com/posts' parent['posts'].url.should eq 'http://example.com/posts'
end end
it "transports options to subresources" do it "transports options to subresources" do
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password') parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
parent['posts'].user.should == 'user' parent['posts'].user.should eq 'user'
parent['posts'].password.should == 'password' parent['posts'].password.should eq 'password'
end end
it "passes a given block to subresources" do it "passes a given block to subresources" do
block = Proc.new{|r| r} block = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block) parent = RestClient::Resource.new('http://example.com', &block)
parent['posts'].block.should == block parent['posts'].block.should eq block
end end
it "the block should be overrideable" do it "the block should be overrideable" do
block1 = Proc.new{|r| r} block1 = Proc.new{|r| r}
block2 = Proc.new{|r| r} block2 = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block1) parent = RestClient::Resource.new('http://example.com', &block1)
# parent['posts', &block2].block.should == block2 # ruby 1.9 syntax # parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax
parent.send(:[], 'posts', &block2).block.should == block2 parent.send(:[], 'posts', &block2).block.should eq block2
end end
it "the block should be overrideable in ruby 1.9 syntax" do it "the block should be overrideable in ruby 1.9 syntax" do
block = Proc.new{|r| r} block = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block) parent = RestClient::Resource.new('http://example.com', &block)
r19_syntax = %q{ r19_syntax = %q{
parent['posts', &->(r){r}].block.should_not == block parent['posts', &->(r){r}].block.should_not eq block
} }
if is_ruby_19? if is_ruby_19?
eval(r19_syntax) eval(r19_syntax)
@ -108,26 +108,26 @@ describe RestClient::Resource do
end end
it "prints its url with to_s" do it "prints its url with to_s" do
RestClient::Resource.new('x').to_s.should == 'x' RestClient::Resource.new('x').to_s.should eq 'x'
end end
describe 'block' do describe 'block' do
it 'can use block when creating the resource' do it 'can use block when creating the resource' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' } resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
resource.get.should == 'foo' resource.get.should eq 'foo'
end end
it 'can use block when executing the resource' do it 'can use block when executing the resource' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') resource = RestClient::Resource.new('www.example.com')
resource.get { |response, request| 'foo' }.should == 'foo' resource.get { |response, request| 'foo' }.should eq 'foo'
end end
it 'execution block override resource block' do it 'execution block override resource block' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' } resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
resource.get { |response, request| 'bar' }.should == 'bar' resource.get { |response, request| 'bar' }.should eq 'bar'
end end
end end

View file

@ -11,47 +11,47 @@ describe RestClient::Response do
end end
it "behaves like string" do it "behaves like string" do
@response.should.to_s == 'abc' @response.to_s.should eq 'abc'
@response.to_str.should == 'abc' @response.to_str.should eq 'abc'
@response.to_i.should == 200 @response.to_i.should eq 200
end end
it "accepts nil strings and sets it to empty for the case of HEAD" do it "accepts nil strings and sets it to empty for the case of HEAD" do
RestClient::Response.create(nil, @net_http_res, {}).should.to_s == "" RestClient::Response.create(nil, @net_http_res, {}).to_s.should eq ""
end end
it "test headers and raw headers" do it "test headers and raw headers" do
@response.raw_headers["Status"][0].should == "200 OK" @response.raw_headers["Status"][0].should eq "200 OK"
@response.headers[:status].should == "200 OK" @response.headers[:status].should eq "200 OK"
end end
describe "cookie processing" do describe "cookie processing" do
it "should correctly deal with one Set-Cookie header with one cookie inside" do it "should correctly deal with one Set-Cookie header with one cookie inside" do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {}) response = RestClient::Response.create('abc', net_http_res, {})
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"] response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
response.cookies.should == { "main_page" => "main_page_no_rewrite" } response.cookies.should eq({ "main_page" => "main_page_no_rewrite" })
end end
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {}) response = RestClient::Response.create('abc', net_http_res, {})
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"] response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
response.cookies.should == { response.cookies.should eq({
"main_page" => "main_page_no_rewrite", "main_page" => "main_page_no_rewrite",
"remember_me" => "", "remember_me" => "",
"user" => "somebody" "user" => "somebody"
} })
end end
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {}) response = RestClient::Response.create('abc', net_http_res, {})
response.cookies.should == { response.cookies.should eq({
"main_page" => "main_page_no_rewrite", "main_page" => "main_page_no_rewrite",
"remember_me" => "", "remember_me" => "",
"user" => "somebody" "user" => "somebody"
} })
end end
end end
@ -81,19 +81,19 @@ describe RestClient::Response do
it "follows a redirection when the request is a get" do it "follows a redirection when the request is a get" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
end end
it "follows a redirection and keep the parameters" do it "follows a redirection and keep the parameters" do
stub_request(:get, 'http://foo:bar@some/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://foo:bar@some/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://foo:bar@new/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => 'Foo') stub_request(:get, 'http://foo:bar@new/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should eq 'Foo'
end end
it "follows a redirection and keep the cookies" do it "follows a redirection and keep the cookies" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', }) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', })
stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux') stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Qux' RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
end end
it "doesn't follow a 301 when the request is a post" do it "doesn't follow a 301 when the request is a post" do
@ -123,31 +123,31 @@ describe RestClient::Response do
it "follows a redirection when the request is a post and result is a 303" do it "follows a redirection when the request is a post and result is a 303" do
stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'}) stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should eq 'Foo'
end end
it "follows a redirection when the request is a head" do it "follows a redirection when the request is a head" do
stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:head, 'http://new/resource').to_return(:body => 'Foo') stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should eq 'Foo'
end end
it "handles redirects with relative paths" do it "handles redirects with relative paths" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
stub_request(:get, 'http://some/index').to_return(:body => 'Foo') stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
end end
it "handles redirects with relative path and query string" do it "handles redirects with relative path and query string" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo') stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
end end
it "follow a redirection when the request is a get and the response is in the 30x range" do it "follow a redirection when the request is a get and the response is in the 30x range" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo' RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
end end
it "follows no more than 10 redirections before raising error" do it "follows no more than 10 redirections before raising error" do