complete url if location header is relative path

master
Kanezoh 2021-08-17 14:30:08 +09:00
parent 226908b5ba
commit 810f9ae430
2 changed files with 15 additions and 3 deletions

View File

@ -6,6 +6,10 @@ WebMock.stub(:post, "http://example.com/post")
.with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
.to_return(status: 302, body: "redirect", headers: {"Location" => "http://example.com/redirect"})
WebMock.stub(:post, "http://example.com/post2")
.with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
.to_return(status: 302, body: "redirect", headers: {"Location" => "/redirect"})
describe "Mechanize redirect test" do
it "redirect" do
agent = Mechanize.new
@ -14,4 +18,12 @@ describe "Mechanize redirect test" do
page.body.should eq("success")
page.code.should eq(200)
end
it "redirect with relative path" do
agent = Mechanize.new
query = {"email" => "foobar"}
page = agent.post("http://example.com/post2", query: query)
page.body.should eq("success")
page.code.should eq(200)
end
end

View File

@ -32,15 +32,15 @@ module MechanizeCr
save_response_cookies(response, uri, page)
if response && response.status.redirection?
return follow_redirect(response, headers)
return follow_redirect(response, headers, page)
end
page
end
private def follow_redirect(response, headers)
private def follow_redirect(response, headers, referer)
redirect_url = response.headers["location"]
uri = URI.parse(redirect_url)
uri = resolve_url(redirect_url, referer)
# Make sure we are not copying over the POST headers from the original request
headers.delete("Content-MD5")