Skip to content

A platform for community discussion. Free, open, simple.

FIX: Cookies header didn't have the right format

Gerhard Schlagerb6277e208b11
diff --git a/lib/final_destination.rb b/lib/final_destination.rb
index c6c989de9a9..a1180b4618a 100644
--- a/lib/final_destination.rb
+++ b/lib/final_destination.rb
@@ -73,7 +73,7 @@ class FinalDestination
"Host" => @uri.hostname
}
- result['cookie'] = @cookie if @cookie
+ result['Cookie'] = @cookie if @cookie
result
end
@@ -164,7 +164,7 @@ class FinalDestination
)
location = nil
- headers = nil
+ response_headers = nil
response_status = response.status.to_i
@@ -181,31 +181,29 @@ class FinalDestination
return @uri
end
- headers = {}
+ response_headers = {}
if cookie_val = get_response.get_fields('set-cookie')
- headers['set-cookie'] = cookie_val.join
+ response_headers[:cookies] = cookie_val
end
- # TODO this is confusing why grab location for anything not
- # between 300-400 ?
if location_val = get_response.get_fields('location')
- headers['location'] = location_val.join
+ response_headers[:location] = location_val.join
end
end
- unless headers
- headers = {}
- response.headers.each do |k, v|
- headers[k.to_s.downcase] = v
- end
+ unless response_headers
+ response_headers = {
+ cookies: response.data[:cookies] || response.headers[:"set-cookie"],
+ location: response.headers[:location]
+ }
end
if (300..399).include?(response_status)
- location = headers["location"]
+ location = response_headers[:location]
end
- if set_cookie = headers["set-cookie"]
- @cookie = set_cookie
+ if cookies = response_headers[:cookies]
+ @cookie = Array.wrap(cookies).map { |c| c.split(';').first.strip }.join('; ')
end
if location
diff --git a/spec/components/final_destination_spec.rb b/spec/components/final_destination_spec.rb
index 38cd37e2bfa..aa5364c5b10 100644
--- a/spec/components/final_destination_spec.rb
+++ b/spec/components/final_destination_spec.rb
@@ -204,6 +204,63 @@ describe FinalDestination do
expect(final.cookie).to eq('evil=trout')
end
end
+
+ it "correctly extracts cookies during GET" do
+ stub_request(:head, "https://eviltrout.com").to_return(status: 405)
+
+ stub_request(:get, "https://eviltrout.com")
+ .to_return(status: 302, body: "" , headers: {
+ "Location" => "https://eviltrout.com",
+ "Set-Cookie" => ["foo=219ffwef9w0f; expires=Mon, 19-Feb-2018 10:44:24 GMT; path=/; domain=eviltrout.com",
+ "bar=1",
+ "baz=2; expires=Tue, 19-Feb-2019 10:14:24 GMT; path=/; domain=eviltrout.com"]
+ })
+
+ stub_request(:head, "https://eviltrout.com")
+ .with(headers: { "Cookie" => "bar=1; baz=2; foo=219ffwef9w0f" })
+ .to_return(status: 200, body: "")
+
+ final = FinalDestination.new("https://eviltrout.com", opts)
+ expect(final.resolve.to_s).to eq("https://eviltrout.com")
+ expect(final.status).to eq(:resolved)
+ expect(final.cookie).to eq("bar=1; baz=2; foo=219ffwef9w0f")
+ end
+ end
+
+ it "should use the correct format for cookies when there is only one cookie" do
+ stub_request(:head, "https://eviltrout.com")
+ .to_return(status: 302, body: "" , headers: {
+ "Location" => "https://eviltrout.com",
+ "Set-Cookie" => "foo=219ffwef9w0f; expires=Mon, 19-Feb-2018 10:44:24 GMT; path=/; domain=eviltrout.com"
+ })
+
+ stub_request(:head, "https://eviltrout.com")
+ .with(headers: { "Cookie" => "foo=219ffwef9w0f" })
+ .to_return(status: 200, body: "")
+
+ final = FinalDestination.new("https://eviltrout.com", opts)
+ expect(final.resolve.to_s).to eq("https://eviltrout.com")
+ expect(final.status).to eq(:resolved)
+ expect(final.cookie).to eq("foo=219ffwef9w0f")
+ end
+
+ it "should use the correct format for cookies when there are multiple cookies" do
+ stub_request(:head, "https://eviltrout.com")
+ .to_return(status: 302, body: "" , headers: {
+ "Location" => "https://eviltrout.com",
+ "Set-Cookie" => ["foo=219ffwef9w0f; expires=Mon, 19-Feb-2018 10:44:24 GMT; path=/; domain=eviltrout.com",
+ "bar=1",
+ "baz=2; expires=Tue, 19-Feb-2019 10:14:24 GMT; path=/; domain=eviltrout.com"]
+ })
+
+ stub_request(:head, "https://eviltrout.com")
+ .with(headers: { "Cookie" => "bar=1; baz=2; foo=219ffwef9w0f" })
+ .to_return(status: 200, body: "")
+
+ final = FinalDestination.new("https://eviltrout.com", opts)
+ expect(final.resolve.to_s).to eq("https://eviltrout.com")
+ expect(final.status).to eq(:resolved)
+ expect(final.cookie).to eq("bar=1; baz=2; foo=219ffwef9w0f")
end
end