Skip to content

Commit 393e3e4

Browse files
authored
refactor: define UA in requests is sufficisnet instead (#473)
* refactor: define UA in requests is sufficisnet instead * fix rubocop
1 parent 0a5dc26 commit 393e3e4

File tree

3 files changed

+106
-42
lines changed

3 files changed

+106
-42
lines changed

.rubocop.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,6 @@ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
223223
Style/RedundantInitialize: # new in 1.27
224224
Enabled: true
225225
Style/RedundantStringEscape: # new in 1.37
226-
Enabled: true
226+
Enabled: true
227+
Style/RegexpLiteral:
228+
Enabled: false

lib/appium_lib_core/common/base/http_default.rb

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ module RequestHeaders
2727
end
2828

2929
class Default < Selenium::WebDriver::Remote::Http::Default
30-
DEFAULT_HEADERS = {
31-
'Accept' => CONTENT_TYPE,
32-
'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8",
33-
'User-Agent' =>
34-
"appium/ruby_lib_core/#{VERSION} (#{::Selenium::WebDriver::Remote::Http::Common::DEFAULT_HEADERS['User-Agent']})"
35-
}.freeze
36-
3730
attr_reader :additional_headers
3831

3932
# override
@@ -64,6 +57,13 @@ def update_sending_request_to(scheme:, host:, port:, path:)
6457
@server_url = URI.parse "#{scheme}://#{host}:#{port}#{path}"
6558
end
6659

60+
def request(verb, url, headers, payload, redirects = 0)
61+
headers['User-Agent'] = "appium/ruby_lib_core/#{VERSION} (#{headers['User-Agent']})"
62+
headers = headers.merge @additional_headers unless @additional_headers.empty?
63+
64+
super(verb, url, headers, payload, redirects)
65+
end
66+
6767
private
6868

6969
def validate_url_param(scheme, host, port, path)
@@ -73,30 +73,6 @@ def validate_url_param(scheme, host, port, path)
7373
::Appium::Logger.debug(message)
7474
false
7575
end
76-
77-
public
78-
79-
# override to use default header
80-
# https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/http/common.rb#L46
81-
def call(verb, url, command_hash)
82-
url = server_url.merge(url) unless url.is_a?(URI)
83-
headers = DEFAULT_HEADERS.dup
84-
headers = headers.merge @additional_headers unless @additional_headers.empty?
85-
headers['Cache-Control'] = 'no-cache' if verb == :get
86-
87-
if command_hash
88-
payload = JSON.generate(command_hash)
89-
headers['Content-Length'] = payload.bytesize.to_s if [:post, :put].include?(verb)
90-
elsif verb == :post
91-
payload = '{}'
92-
headers['Content-Length'] = '2'
93-
end
94-
95-
::Appium::Logger.info(" >>> #{url} | #{payload}")
96-
::Appium::Logger.info(" > #{headers.inspect}")
97-
98-
request verb, url, headers, payload
99-
end
10076
end
10177
end
10278
end

test/test_helper.rb

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ def android_mock_create_session_w3c
443443
}.to_json
444444

445445
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
446-
.with(headers: { 'X-Idempotency-Key' => /.+/ })
447446
.to_return(headers: HEADER, status: 200, body: response)
448447

449448
stub_request(:post, "#{SESSION}/timeouts")
@@ -453,8 +452,34 @@ def android_mock_create_session_w3c
453452
driver = @core.start_driver
454453

455454
assert_equal({}, driver.send(:bridge).http.additional_headers)
456-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
457-
assert_requested(:post, "#{SESSION}/timeouts", body: { implicit: 5_000 }.to_json, times: 1)
455+
assert_requested(
456+
:post,
457+
'http://127.0.0.1:4723/wd/hub/session',
458+
headers: {
459+
'X-Idempotency-Key' => /.+/,
460+
'Content-Type' => 'application/json; charset=UTF-8',
461+
'User-Agent' => /appium\/ruby_lib_core\/.+/
462+
},
463+
times: 1
464+
)
465+
466+
assert_requested(
467+
:post,
468+
"#{SESSION}/timeouts",
469+
headers: {
470+
'Content-Type' => 'application/json; charset=UTF-8',
471+
'User-Agent' => /appium\/ruby_lib_core\/.+/
472+
},
473+
body: { implicit: 5_000 }.to_json,
474+
times: 1
475+
)
476+
assert_not_requested(
477+
:post,
478+
"#{SESSION}/timeouts",
479+
headers: { 'X-Idempotency-Key' => /.+/ },
480+
body: { implicit: 5_000 }.to_json,
481+
times: 1
482+
)
458483
driver
459484
end
460485

@@ -473,7 +498,6 @@ def android_chrome_mock_create_session_w3c
473498
}.to_json
474499

475500
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
476-
.with(headers: { 'X-Idempotency-Key' => /.+/ })
477501
.to_return(headers: HEADER, status: 200, body: response)
478502

479503
stub_request(:post, "#{SESSION}/timeouts")
@@ -483,8 +507,34 @@ def android_chrome_mock_create_session_w3c
483507
driver = @core.start_driver
484508

485509
assert_equal({}, driver.send(:bridge).http.additional_headers)
486-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
487-
assert_requested(:post, "#{SESSION}/timeouts", body: { implicit: 5_000 }.to_json, times: 1)
510+
assert_requested(
511+
:post,
512+
'http://127.0.0.1:4723/wd/hub/session',
513+
headers: {
514+
'X-Idempotency-Key' => /.+/,
515+
'Content-Type' => 'application/json; charset=UTF-8',
516+
'User-Agent' => /appium\/ruby_lib_core\/.+/
517+
},
518+
times: 1
519+
)
520+
521+
assert_requested(
522+
:post,
523+
"#{SESSION}/timeouts",
524+
headers: {
525+
'Content-Type' => 'application/json; charset=UTF-8',
526+
'User-Agent' => /appium\/ruby_lib_core\/.+/
527+
},
528+
body: { implicit: 5_000 }.to_json,
529+
times: 1
530+
)
531+
assert_not_requested(
532+
:post,
533+
"#{SESSION}/timeouts",
534+
headers: { 'X-Idempotency-Key' => /.+/ },
535+
body: { implicit: 5_000 }.to_json,
536+
times: 1
537+
)
488538
driver
489539
end
490540

@@ -510,7 +560,16 @@ def ios_mock_create_session_w3c
510560

511561
driver = @core.start_driver
512562

513-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
563+
assert_requested(
564+
:post,
565+
'http://127.0.0.1:4723/wd/hub/session',
566+
headers: {
567+
'X-Idempotency-Key' => /.+/,
568+
'Content-Type' => 'application/json; charset=UTF-8',
569+
'User-Agent' => /appium\/ruby_lib_core\/.+/
570+
},
571+
times: 1
572+
)
514573
driver
515574
end
516575

@@ -532,7 +591,16 @@ def windows_mock_create_session
532591

533592
driver = @core.start_driver
534593

535-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
594+
assert_requested(
595+
:post,
596+
'http://127.0.0.1:4723/wd/hub/session',
597+
headers: {
598+
'X-Idempotency-Key' => /.+/,
599+
'Content-Type' => 'application/json; charset=UTF-8',
600+
'User-Agent' => /appium\/ruby_lib_core\/.+/
601+
},
602+
times: 1
603+
)
536604
driver
537605
end
538606

@@ -554,7 +622,16 @@ def windows_mock_create_session_w3c
554622

555623
driver = @core.start_driver
556624

557-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
625+
assert_requested(
626+
:post,
627+
'http://127.0.0.1:4723/wd/hub/session',
628+
headers: {
629+
'X-Idempotency-Key' => /.+/,
630+
'Content-Type' => 'application/json; charset=UTF-8',
631+
'User-Agent' => /appium\/ruby_lib_core\/.+/
632+
},
633+
times: 1
634+
)
558635
driver
559636
end
560637

@@ -574,7 +651,16 @@ def mac2_mock_create_session_w3c
574651

575652
driver = @core.start_driver
576653

577-
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
654+
assert_requested(
655+
:post,
656+
'http://127.0.0.1:4723/wd/hub/session',
657+
headers: {
658+
'X-Idempotency-Key' => /.+/,
659+
'Content-Type' => 'application/json; charset=UTF-8',
660+
'User-Agent' => /appium\/ruby_lib_core\/.+/
661+
},
662+
times: 1
663+
)
578664
driver
579665
end
580666
end

0 commit comments

Comments
 (0)