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

73 lines
3.4 KiB
Ruby
Raw Normal View History

2009-01-24 17:55:18 -05:00
require File.dirname(__FILE__) + '/base'
describe RestClient::Response do
2009-12-29 12:27:39 -05:00
before do
@net_http_res = mock('net http response', :to_hash => {"Status" => ["200 OK"]})
@response = RestClient::Response.new('abc', @net_http_res)
end
2009-01-24 17:55:18 -05:00
2009-12-29 12:27:39 -05:00
it "behaves like string" do
@response.should == 'abc'
end
2009-01-24 17:55:18 -05:00
2009-12-29 12:27:39 -05:00
it "accepts nil strings and sets it to empty for the case of HEAD" do
RestClient::Response.new(nil, @net_http_res).should == ""
end
it "test headers and raw headers" do
@response.raw_headers["Status"][0].should == "200 OK"
@response.headers[:status].should == "200 OK"
end
describe "cookie processing" do
it "should correctly deal with one Set-Cookie header with one cookie inside" do
net_http_res = mock('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.new('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.cookies.should == { "main_page" => "main_page_no_rewrite" }
end
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
net_http_res = mock('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.new('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.cookies.should == {
"main_page" => "main_page_no_rewrite",
"remember_me" => "",
"user" => "somebody"
}
end
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
net_http_res = mock('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.new('abc', net_http_res)
response.cookies.should == {
"main_page" => "main_page_no_rewrite",
"remember_me" => "",
"user" => "somebody"
}
end
end
describe "exceptions processing" do
it "should return itself for normal codes" do
(200..206).each do |code|
net_http_res = mock('net http response', :code => '200')
response = RestClient::Response.new('abc', net_http_res)
response.return!
end
end
it "should throw an exception for other codes" do
RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
net_http_res = mock('net http response', :code => code.to_i)
response = RestClient::Response.new('abc', net_http_res)
lambda { response.return!}.should raise_error
end
end
end
2009-01-24 17:55:18 -05:00
end