Skip to content

refactor: define UA in requests is sufficisnet instead #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,6 @@ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
Style/RedundantInitialize: # new in 1.27
Enabled: true
Style/RedundantStringEscape: # new in 1.37
Enabled: true
Enabled: true
Style/RegexpLiteral:
Enabled: false
38 changes: 7 additions & 31 deletions lib/appium_lib_core/common/base/http_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ module RequestHeaders
end

class Default < Selenium::WebDriver::Remote::Http::Default
DEFAULT_HEADERS = {
'Accept' => CONTENT_TYPE,
'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8",
'User-Agent' =>
"appium/ruby_lib_core/#{VERSION} (#{::Selenium::WebDriver::Remote::Http::Common::DEFAULT_HEADERS['User-Agent']})"
}.freeze

attr_reader :additional_headers

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

def request(verb, url, headers, payload, redirects = 0)
headers['User-Agent'] = "appium/ruby_lib_core/#{VERSION} (#{headers['User-Agent']})"
headers = headers.merge @additional_headers unless @additional_headers.empty?

super(verb, url, headers, payload, redirects)
end

private

def validate_url_param(scheme, host, port, path)
Expand All @@ -73,30 +73,6 @@ def validate_url_param(scheme, host, port, path)
::Appium::Logger.debug(message)
false
end

public

# override to use default header
# https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/http/common.rb#L46
def call(verb, url, command_hash)
url = server_url.merge(url) unless url.is_a?(URI)
headers = DEFAULT_HEADERS.dup
headers = headers.merge @additional_headers unless @additional_headers.empty?
headers['Cache-Control'] = 'no-cache' if verb == :get

if command_hash
payload = JSON.generate(command_hash)
headers['Content-Length'] = payload.bytesize.to_s if [:post, :put].include?(verb)
elsif verb == :post
payload = '{}'
headers['Content-Length'] = '2'
end

::Appium::Logger.info(" >>> #{url} | #{payload}")
::Appium::Logger.info(" > #{headers.inspect}")

request verb, url, headers, payload
end
end
end
end
Expand Down
106 changes: 96 additions & 10 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ def android_mock_create_session_w3c
}.to_json

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

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

assert_equal({}, driver.send(:bridge).http.additional_headers)
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(:post, "#{SESSION}/timeouts", body: { implicit: 5_000 }.to_json, times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)

assert_requested(
:post,
"#{SESSION}/timeouts",
headers: {
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
body: { implicit: 5_000 }.to_json,
times: 1
)
assert_not_requested(
:post,
"#{SESSION}/timeouts",
headers: { 'X-Idempotency-Key' => /.+/ },
body: { implicit: 5_000 }.to_json,
times: 1
)
driver
end

Expand All @@ -473,7 +498,6 @@ def android_chrome_mock_create_session_w3c
}.to_json

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

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

assert_equal({}, driver.send(:bridge).http.additional_headers)
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(:post, "#{SESSION}/timeouts", body: { implicit: 5_000 }.to_json, times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)

assert_requested(
:post,
"#{SESSION}/timeouts",
headers: {
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
body: { implicit: 5_000 }.to_json,
times: 1
)
assert_not_requested(
:post,
"#{SESSION}/timeouts",
headers: { 'X-Idempotency-Key' => /.+/ },
body: { implicit: 5_000 }.to_json,
times: 1
)
driver
end

Expand All @@ -510,7 +560,16 @@ def ios_mock_create_session_w3c

driver = @core.start_driver

assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)
driver
end

Expand All @@ -532,7 +591,16 @@ def windows_mock_create_session

driver = @core.start_driver

assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)
driver
end

Expand All @@ -554,7 +622,16 @@ def windows_mock_create_session_w3c

driver = @core.start_driver

assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)
driver
end

Expand All @@ -574,7 +651,16 @@ def mac2_mock_create_session_w3c

driver = @core.start_driver

assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
assert_requested(
:post,
'http://127.0.0.1:4723/wd/hub/session',
headers: {
'X-Idempotency-Key' => /.+/,
'Content-Type' => 'application/json; charset=UTF-8',
'User-Agent' => /appium\/ruby_lib_core\/.+/
},
times: 1
)
driver
end
end
Expand Down