follow redirects
parent
79b3984b6d
commit
226908b5ba
|
@ -0,0 +1,17 @@
|
|||
require "./spec_helper"
|
||||
|
||||
WebMock.stub(:get, "http://example.com/redirect").to_return(body: "success")
|
||||
|
||||
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"})
|
||||
|
||||
describe "Mechanize redirect test" do
|
||||
it "redirect" do
|
||||
agent = Mechanize.new
|
||||
query = {"email" => "foobar"}
|
||||
page = agent.post("http://example.com/post", query: query)
|
||||
page.body.should eq("success")
|
||||
page.code.should eq(200)
|
||||
end
|
||||
end
|
|
@ -30,9 +30,26 @@ module MechanizeCr
|
|||
page = response_parse(response, body, uri)
|
||||
# save cookies
|
||||
save_response_cookies(response, uri, page)
|
||||
|
||||
if response && response.status.redirection?
|
||||
return follow_redirect(response, headers)
|
||||
end
|
||||
|
||||
page
|
||||
end
|
||||
|
||||
private def follow_redirect(response, headers)
|
||||
redirect_url = response.headers["location"]
|
||||
uri = URI.parse(redirect_url)
|
||||
|
||||
# Make sure we are not copying over the POST headers from the original request
|
||||
headers.delete("Content-MD5")
|
||||
headers.delete("Content-Type")
|
||||
headers.delete("Content-Length")
|
||||
|
||||
@context.not_nil!.get(uri)
|
||||
end
|
||||
|
||||
def http_request(uri, method, params)
|
||||
case uri.scheme.not_nil!.downcase
|
||||
when "http", "https" then
|
||||
|
|
Loading…
Reference in New Issue