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

added 307

This commit is contained in:
Julien Kirch 2010-05-25 18:55:57 +02:00
parent 0b4da8c71b
commit d970eb85d7
5 changed files with 21 additions and 6 deletions

View file

@ -78,7 +78,7 @@ See RestClient::Resource docs for details.
== Exceptions (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
* for results code between 200 and 207 a RestClient::Response will be returned
* for results code 301 and 302 the redirection will be followed if the request is a get or a head
* for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head
* for result code 303 the redirection will be followed and the request transformed into a get
* for other cases a RestClient::Exception holding the Response will be raised, a specific exception class will be thrown for know error codes
@ -115,11 +115,11 @@ Response.return! can be called to invoke the default response's behavior.
}
# Follow redirections for all request types and not only for get and head
# RFC : "If the 301 (or 302) status code is received in response to a request other than GET or HEAD,
# RFC : "If the 301, 302 or 307 status code is received in response to a request other than GET or HEAD,
# the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user,
# since this might change the conditions under which the request was issued."
RestClient.get('http://my-rest-service.com/resource'){ |response, request, &block|
if [301, 302].include? response.code
if [301, 302, 307].include? response.code
response.follow_redirection(request, &block)
else
response.return!(request, &block)

View file

@ -2,6 +2,7 @@
- forgot to include rest-client.rb in the gem
- user, password and user-defined headers should survive a redirect
- added 207 and 307 status code
# 1.5.1

View file

@ -35,11 +35,11 @@ module RestClient
end
# Return the default behavior corresponding to the response code:
# the response itself for code in 200..206, redirection for 301 and 302 in get and head cases, redirection for 303 and an exception in other cases
# the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
def return! request = nil, &block
if (200..207).include? code
self
elsif [301, 302].include? code
elsif [301, 302, 307].include? code
unless [:get, :head].include? args[:method]
raise Exceptions::EXCEPTIONS_MAP[code], self
else

View file

@ -16,6 +16,8 @@ module RestClient
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',

View file

@ -90,12 +90,24 @@ describe RestClient::Response do
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should == 'Foo'
end
it "doesn't follow a redirection when the request is a post" do
it "doesn't follow a 301 when the request is a post" do
net_http_res = mock('net http response', :code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
end
it "doesn't follow a 302 when the request is a post" do
net_http_res = mock('net http response', :code => 302)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::Found)
end
it "doesn't follow a 307 when the request is a post" do
net_http_res = mock('net http response', :code => 307)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
end
it "doesn't follow a redirection when the request is a put" do
net_http_res = mock('net http response', :code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :put})