Permalink
Browse files

fix redirect with cookie support

When a response does not include cookies, the middleware sets the
"Cookies" header to nil which raises an exception in Net::HTTP.
  • Loading branch information...
1 parent 87c05b7 commit 1fcb779fbc06f67aa04220edbc6518619ae1a204 Marty Zalega committed with mislav Jul 22, 2012
Showing with 11 additions and 1 deletion.
  1. +4 −1 lib/faraday_middleware/response/follow_redirects.rb
  2. +7 −0 spec/follow_redirects_spec.rb
@@ -87,7 +87,10 @@ def perform_with_redirection(env, follows)
def update_env(env, request_body, response)
env[:url] += response['location']
- env[:request_headers][:cookies] = keep_cookies(env) if @options[:cookies]
+ if @options[:cookies]
+ cookies = keep_cookies(env)
+ env[:request_headers][:cookies] = cookies unless cookies.nil?
+ end
if transform_into_get?(response)
env[:method] = :get
@@ -141,6 +141,13 @@
stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
end.get('/').env[:request_headers][:cookies].should == cookies
end
+
+ it "not set cookies header on request when response has no cookies" do
+ conn = connection(:cookies => :all) do |stub|
+ stub.get('/') { [301, {'Location' => '/found'}, ''] }
+ stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
+ end.get('/').env[:request_headers].has_key?('Cookies').should == false
+ end
end
context "is an array of cookie names" do

0 comments on commit 1fcb779

Please sign in to comment.