Permalink
Browse files

Follow redirections for head and options requests

  • Loading branch information...
Peter Williams authored and sferik committed Oct 22, 2012
1 parent 6179871 commit dc91c09c63e257be47a0f5121048b5c0c09a11ff
Showing with 16 additions and 7 deletions.
  1. +4 −1 lib/faraday_middleware/response/follow_redirects.rb
  2. +12 −6 spec/follow_redirects_spec.rb
@@ -34,7 +34,7 @@ def initialize(response)
# words, it doesn't support parallelism.
class FollowRedirects < Faraday::Middleware
# HTTP methods for which 30x redirects can be followed
- ALLOWED_METHODS = Set.new [:get, :post, :put, :patch, :delete]
+ ALLOWED_METHODS = Set.new [:head, :options, :get, :post, :put, :patch, :delete]
# HTTP redirect status codes that this middleware implements
REDIRECT_CODES = Set.new [301, 302, 303, 307]
# Keys in env hash which will get cleared between requests
@@ -68,6 +68,9 @@ def call(env)
private
def transform_into_get?(response)
+ return false if [:head, :options].include? response.env[:method]
+ # Never convert head or options to a get. That would just be silly.
+
!@replay_request_codes.include? response.status
end
@@ -16,12 +16,18 @@
end.get('/permanent').body).to eq 'fin'
end
- [:head, :options].each do |method|
- it "returning the response headers for a #{method.to_s.upcase} request" do
- expect(connection do |stub|
- stub.new_stub(method, '/permanent') { [status_code, {'Location' => '/found'}, ''] }
- end.run_request(method, '/permanent', nil, nil).headers['Location']).to eq('/found')
- end
+ it "follows the redirection for a HEAD request" do
+ expect(connection do |stub|
+ stub.head('/permanent') { [status_code, {'Location' => '/found'}, ''] }
+ stub.head('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
+ end.head('/permanent').status).to eq 200
+ end
+
+ it "follows the redirection for a OPTIONS request" do
+ expect(connection do |stub|
+ stub.new_stub(:options, '/permanent') { [status_code, {'Location' => '/found'}, ''] }
+ stub.new_stub(:options, '/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
+ end.run_request(:options, '/permanent', nil, nil).status).to eq 200
end
end

0 comments on commit dc91c09

Please sign in to comment.