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
|
2010-01-22 15:04:49 -05:00
|
|
|
|
2010-01-20 11:37:54 -05:00
|
|
|
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"]
|
2010-01-22 15:04:49 -05:00
|
|
|
response.cookies.should == { "main_page" => "main_page_no_rewrite" }
|
2010-01-20 11:37:54 -05:00
|
|
|
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 == {
|
2010-01-22 15:04:49 -05:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2010-01-20 11:37:54 -05:00
|
|
|
}
|
|
|
|
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 == {
|
2010-01-22 15:04:49 -05:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2010-01-20 11:37:54 -05:00
|
|
|
}
|
|
|
|
end
|
2010-01-20 10:54:54 -05:00
|
|
|
end
|
2010-01-20 11:37:54 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
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
|