From e5373f4cb8626de9edf2d733588afec9d57adc3d Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Tue, 16 Nov 2021 10:27:04 +0200 Subject: [PATCH] Sets a Referer header --- spec/agent_spec.cr | 14 ++++++++++++++ src/mechanize/http/agent.cr | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index 542e15e..661f53c 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -65,4 +65,18 @@ describe "Mechanize Agent test" do File.exists?("mechanizecr_example.html").should eq true File.delete("mechanizecr_example.html") end + + it "should set referer header" do + page_url = "http://example.com/form" + + agent = Mechanize.new + page = agent.get(page_url) + form = page.forms[0] + form.field_with("name").value = "foo" + form.field_with("email").value = "bar" + page = agent.submit(form) + + page.not_nil!.code.should eq 200 + agent.request_headers["Referer"].should eq(page_url) + end end diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index b8edaf5..d27acdf 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -19,11 +19,12 @@ module MechanizeCr @user_agent = "" end - def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String, String).new, + def fetch(uri, method = :get, headers = ::HTTP::Headers.new, params = Hash(String, String).new, referer = (current_page unless history.empty?)) uri = resolve_url(uri, referer) set_request_headers(uri, headers) set_user_agent + set_request_referer(referer) uri, params = resolve_parameters(uri, method, params) response = http_request(uri, method, params) body = response.not_nil!.body @@ -47,7 +48,7 @@ module MechanizeCr headers.delete("Content-Type") headers.delete("Content-Length") - @context.not_nil!.get(uri) + fetch(uri) end def http_request(uri, method, params) @@ -94,6 +95,13 @@ module MechanizeCr end end + # Sets a Referer header. + def set_request_referer(referer : MechanizeCr::Page?) + return unless referer + + request_headers["Referer"] = referer.uri.to_s + end + private def resolve_parameters(uri, method, params) case method when :get