From 0baedaca6e0541273594b42aa57fbf2943e307bd Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sat, 9 Oct 2021 20:26:33 +0900 Subject: [PATCH] complete uri scheme from referer_uri --- spec/http_spec.cr | 9 +++++++++ src/mechanize/http/agent.cr | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index d9a1dcd..f1157e3 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -4,6 +4,8 @@ WebMock.stub(:post, "http://example.com/post") .with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) .to_return(body: "success") WebMock.stub(:get, "example.com/%E3%81%82%E3%81%82%E3%81%82") +WebMock.stub(:get, "https://example.com/") +WebMock.stub(:get, "https://example.com/post") describe "Mechanize HTTP test" do it "simple GET" do @@ -62,4 +64,11 @@ describe "Mechanize HTTP test" do page = agent.get("http://example.com/") agent.request_headers["User-Agent"].should eq mac_chrome_agent end + + it "can complete uri when uri is relative" do + agent = Mechanize.new + agent.get("https://example.com/") + page = agent.get("/post") + page.uri.to_s.should eq "https://example.com/post" + end end diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 431b8b6..b8edaf5 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -151,10 +151,16 @@ module MechanizeCr if target_url.host.nil? && referer && referer_uri.try &.host target_url.host = referer_uri.not_nil!.host end + # fill scheme if scheme isn't set if target_url.relative? - target_url.scheme = "http" + if referer && referer_uri.try &.scheme + target_url.scheme = referer_uri.not_nil!.scheme + else + target_url.scheme = "http" + end end + # fill path's slash if there's no slash. if target_url.path && (target_url.path.empty? || target_url.path[0] != '/') target_url.path = "/#{target_url.path}"