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}"