From 3d4dfc4568b9871dea814ec1704936cef1067640 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Wed, 4 Aug 2021 14:37:56 +0900 Subject: [PATCH] add user agent test --- spec/http_spec.cr | 8 ++++++++ src/mechanize.cr | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 284e4fd..88e98b2 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -47,4 +47,12 @@ describe "Mechanize HTTP test" do page.body.should eq "success" page.code.should eq 200 end + + it "can set user agent" do + agent = Mechanize.new + mac_chrome_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" + agent.user_agent = mac_chrome_agent + page = agent.get("http://example.com/") + agent.request_headers["User-Agent"].should eq mac_chrome_agent + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index b888b4d..bebf532 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -65,6 +65,14 @@ class Mechanize @agent.request_headers = request_headers end + def user_agent + @agent.user_agent + end + + def user_agent=(user_agent) + @agent.user_agent = user_agent + end + def current_page @agent.current_page end