Skip to content

Commit

Permalink
[rb] Support registering extra headers in HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed May 15, 2024
1 parent 6978ea8 commit 4f72e3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 5 additions & 1 deletion rb/lib/selenium/webdriver/remote/http/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Common
'User-Agent' => "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"
}.freeze

class << self
attr_accessor :extra_headers
end

attr_writer :server_url

def quit_errors
Expand All @@ -42,7 +46,7 @@ def close

def call(verb, url, command_hash)
url = server_url.merge(url) unless url.is_a?(URI)
headers = DEFAULT_HEADERS.dup
headers = DEFAULT_HEADERS.merge(Common.extra_headers || {}).dup
headers['Cache-Control'] = 'no-cache' if verb == :get

if command_hash
Expand Down
23 changes: 19 additions & 4 deletions rb/spec/unit/selenium/webdriver/remote/http/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ module WebDriver
module Remote
module Http
describe Common do
it 'sends non-empty body header for POST requests without command data' do
subject(:common) do
common = described_class.new
common.server_url = URI.parse('http://server')
allow(common).to receive(:request)

common
end

after do
described_class.extra_headers = {}
end

it 'sends non-empty body header for POST requests without command data' do
common.call(:post, 'clear', nil)

expect(common).to have_received(:request)
Expand All @@ -37,17 +45,24 @@ module Http
end

it 'sends a standard User-Agent by default' do
common = described_class.new
common.server_url = URI.parse('http://server')
user_agent_regexp = %r{\Aselenium/#{WebDriver::VERSION} \(ruby #{Platform.os}\)\z}
allow(common).to receive(:request)

common.call(:post, 'session', nil)

expect(common).to have_received(:request)
.with(:post, URI.parse('http://server/session'),
hash_including('User-Agent' => a_string_matching(user_agent_regexp)), '{}')
end

it 'allows registering extra headers' do
described_class.extra_headers = {'Foo' => 'bar'}

common.call(:post, 'session', nil)

expect(common).to have_received(:request)
.with(:post, URI.parse('http://server/session'),
hash_including('Foo' => 'bar'), '{}')
end
end
end # Http
end # Remote
Expand Down

0 comments on commit 4f72e3f

Please sign in to comment.