Sets a Referer header

master
Anton Maminov 2021-11-16 10:27:04 +02:00
parent f175c42097
commit e5373f4cb8
2 changed files with 24 additions and 2 deletions

View File

@ -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

View File

@ -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