Skip to content

Commit ff83457

Browse files
fix: incomplete/incorrect headers sent when establishing event hub connection (#13)
Co-authored-by: Octavian Ionescu <[email protected]> Co-authored-by: erhe <[email protected]>
1 parent 165fb73 commit ff83457

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

source/ftrack_api/event/hub.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,16 @@ def __init__(self, server_url, api_user, api_key, headers=None, cookies=None):
126126
url_parse_result.hostname,
127127
url_parse_result.port
128128
)
129-
130-
self._cookies = cookies or {}
131-
self._headers = headers or {}
129+
130+
def _validate_mapping(mapping):
131+
'''Validate mapping is a mapping type and return as dict.'''
132+
if not isinstance(mapping, collections_abc.Mapping):
133+
raise TypeError('Expected mapping, got {0!r}.'.format(mapping))
134+
135+
return dict(mapping)
136+
137+
self._cookies = _validate_mapping(cookies or {})
138+
self._headers = _validate_mapping(headers or {})
132139

133140
def get_server_url(self):
134141
'''Return URL to server.'''

source/ftrack_api/session.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,22 +250,28 @@ def __init__(
250250
self._managed_request = None
251251
self._request = requests.Session()
252252

253-
if cookies is not None:
253+
if cookies:
254254
if not isinstance(cookies, collections_abc.Mapping):
255255
raise TypeError('The cookies argument is required to be a mapping.')
256256
self._request.cookies.update(cookies)
257-
258-
if headers is not None:
257+
258+
if headers:
259259
if not isinstance(headers, collections_abc.Mapping):
260260
raise TypeError('The headers argument is required to be a mapping.')
261-
self._request.headers.update(headers)
261+
262+
headers = dict(headers)
263+
264+
else:
265+
headers = {}
262266

263267
if not isinstance(strict_api, bool):
264268
raise TypeError('The strict_api argument is required to be a boolean.')
265-
self._request.headers.update(
269+
270+
headers.update(
266271
{'ftrack-strict-api': 'true' if strict_api is True else 'false'}
267272
)
268-
273+
274+
self._request.headers.update(headers)
269275
self._request.auth = SessionAuthentication(
270276
self._api_key, self._api_user
271277
)
@@ -287,7 +293,7 @@ def __init__(
287293
self._server_url,
288294
self._api_user,
289295
self._api_key,
290-
headers=self._request.headers,
296+
headers=headers,
291297
cookies=requests.utils.dict_from_cookiejar(self._request.cookies)
292298
)
293299

test/unit/event/test_hub.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import subprocess
99
import sys
10+
import requests
1011
import logging
1112

1213
import pytest
@@ -181,15 +182,28 @@ def test_connect_custom_headers(session):
181182
event_hub.disconnect()
182183

183184

184-
def test_connect_strict_api_header(session):
185+
@pytest.mark.parametrize(
186+
'headers', [
187+
(
188+
requests.structures.CaseInsensitiveDict(
189+
{'ftrack-strict-api': 'true'}
190+
)
191+
),
192+
(
193+
{'ftrack-strict-api': 'true'}
194+
)
195+
]
196+
)
197+
def test_connect_strict_api_header(session, headers):
185198
'''Connect with ftrack-strict-api = True header passed in.'''
186199
event_hub = ftrack_api.event.hub.EventHub(
187-
session.server_url, session.api_user, session.api_key, headers={'ftrack-strict-api': 'true'}
200+
session.server_url, session.api_user, session.api_key, headers=headers
188201
)
189202
event_hub.connect()
190203

191204
assert (
192205
'ftrack-strict-api' in event_hub._headers.keys(),
206+
isinstance(event_hub._headers, dict),
193207
event_hub._headers['ftrack-strict-api'] is True,
194208
event_hub.connected is True
195209
)

0 commit comments

Comments
 (0)