Skip to content
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
9 changes: 6 additions & 3 deletions appium/webdriver/appium_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ def get_remote_connection_headers(cls, parsed_url: 'ParseResult', keep_alive: bo
This method will be used in sending a request method in this class.
"""

extra_headers = cls.extra_headers.copy()
if parsed_url.path.endswith('/session'):
# https://github.com/appium/appium-base-driver/pull/400
cls.extra_headers[HEADER_IDEMOTENCY_KEY] = str(uuid.uuid4())
# Keep the per-request idempotency key local: sessions may be created concurrently.
extra_headers[HEADER_IDEMOTENCY_KEY] = str(uuid.uuid4())
else:
cls.extra_headers = _get_new_headers(HEADER_IDEMOTENCY_KEY, cls.extra_headers)
extra_headers = _get_new_headers(HEADER_IDEMOTENCY_KEY, extra_headers)
cls.extra_headers = extra_headers

return {**super().get_remote_connection_headers(parsed_url, keep_alive=keep_alive), **cls.extra_headers}
return {**super().get_remote_connection_headers(parsed_url, keep_alive=keep_alive), **extra_headers}
31 changes: 31 additions & 0 deletions test/unit/webdriver/appium_connection_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import threading
import unittest
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import patch
from urllib import parse

from appium.webdriver import appium_connection
Expand Down Expand Up @@ -30,6 +33,34 @@ def test_get_remote_connection_headers(self):
self.assertIsNone(headers.get('X-Idempotency-Key'))
self.assertEqual(headers.get('custom'), 'header')

def test_concurrent_new_sessions_get_distinct_idempotency_keys(self):
barrier = threading.Barrier(2)

def base_headers(cls, parsed_url, keep_alive=True):
barrier.wait(timeout=2)
return {}

appium_connection.AppiumConnection.extra_headers = {}
with (
patch.object(
appium_connection.RemoteConnection,
'get_remote_connection_headers',
new=classmethod(base_headers),
),
ThreadPoolExecutor(max_workers=2) as pool,
):
results = list(
pool.map(
lambda _: appium_connection.AppiumConnection.get_remote_connection_headers(
parse.urlparse('http://127.0.0.1:4723/session')
),
range(2),
)
)

keys = [headers['X-Idempotency-Key'] for headers in results]
self.assertEqual(len(set(keys)), 2)

def test_remove_headers_case_insensitive(self):
for h in ['X-Idempotency-Key', 'X-idempotency-Key', 'x-idempotency-key']:
appium_connection.AppiumConnection.extra_headers = {h: 'value'}
Expand Down
Loading