Skip to content

Commit 4f72e3f

Browse files
committed
[rb] Support registering extra headers in HTTP client
1 parent 6978ea8 commit 4f72e3f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

rb/lib/selenium/webdriver/remote/http/common.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Common
3030
'User-Agent' => "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"
3131
}.freeze
3232

33+
class << self
34+
attr_accessor :extra_headers
35+
end
36+
3337
attr_writer :server_url
3438

3539
def quit_errors
@@ -42,7 +46,7 @@ def close
4246

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

4852
if command_hash

rb/spec/unit/selenium/webdriver/remote/http/common_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ module WebDriver
2424
module Remote
2525
module Http
2626
describe Common do
27-
it 'sends non-empty body header for POST requests without command data' do
27+
subject(:common) do
2828
common = described_class.new
2929
common.server_url = URI.parse('http://server')
3030
allow(common).to receive(:request)
3131

32+
common
33+
end
34+
35+
after do
36+
described_class.extra_headers = {}
37+
end
38+
39+
it 'sends non-empty body header for POST requests without command data' do
3240
common.call(:post, 'clear', nil)
3341

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

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

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

4752
expect(common).to have_received(:request)
4853
.with(:post, URI.parse('http://server/session'),
4954
hash_including('User-Agent' => a_string_matching(user_agent_regexp)), '{}')
5055
end
56+
57+
it 'allows registering extra headers' do
58+
described_class.extra_headers = {'Foo' => 'bar'}
59+
60+
common.call(:post, 'session', nil)
61+
62+
expect(common).to have_received(:request)
63+
.with(:post, URI.parse('http://server/session'),
64+
hash_including('Foo' => 'bar'), '{}')
65+
end
5166
end
5267
end # Http
5368
end # Remote

0 commit comments

Comments
 (0)