Skip to content
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

Support customized HTTP Request Headers #337

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ __pycache__/

# JS files
*/node_modules/
*package-lock.json
*package-lock.json

# Build files
build/
4 changes: 2 additions & 2 deletions okta/request_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ async def create_request(self, method: str, url: str, body: dict = None,

# Build request
# Get predetermined headers and build URL
headers.update(self._custom_headers)
headers.update(self._default_headers)
headers = {**self._custom_headers, **headers}
headers = {**self._default_headers, **headers}
Comment on lines +121 to +122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The **syntax destructures the dictionaries and prioritizes entries in the headers parameter set by the user

if self._config["client"]["orgUrl"] not in url:
url = self._config["client"]["orgUrl"] + url

Expand Down
50 changes: 41 additions & 9 deletions tests/unit/test_request_executor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import aiohttp
import asyncio
import datetime
import pytest
import time

from http import HTTPStatus
Expand Down Expand Up @@ -29,11 +30,13 @@ class MockHTTPRequest():

def __call__(self, **params):
self.request_info = params
self.headers = MultiDict({'Date': datetime.datetime.now(tz=datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S %Z'),
'Content-Type': 'application/json',
'X-Rate-Limit-Limit': 600,
'X-Rate-Limit-Remaining': 599,
'X-Rate-Limit-Reset': str(time.time())})
self.headers = MultiDict(
{'Date': datetime.datetime.now(tz=datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S %Z'),
'Content-Type': 'application/json',
'X-Rate-Limit-Limit': 600,
'X-Rate-Limit-Remaining': 599,
'X-Rate-Limit-Reset': str(time.time())}
)
self.url = params['url']
self.content_type = 'application/json'
self.links = ''
Expand Down Expand Up @@ -95,17 +98,46 @@ def test_clear_empty_params():
body = {'int_value': 0,
'str_value': '0',
'empty_str_value': '',
'list_value': [1,2,3],
'list_value': [1, 2, 3],
'empty_list_value': [],
'dict_value': {'int_value': 0},
'empty_dict_value': {},
'nested_empty_dict_value': {'list_value': [1,2,3], 'empty_list_value': []},
'nested_empty_dict_value': {'list_value': [1, 2, 3], 'empty_list_value': []},
'nested_empty_list_value': {'empty_list_value': []}}

cleared_body = {'int_value': 0,
'str_value': '0',
'list_value': [1,2,3],
'list_value': [1, 2, 3],
'dict_value': {'int_value': 0},
'nested_empty_dict_value': {'list_value': [1,2,3]}}
'nested_empty_dict_value': {'list_value': [1, 2, 3]}}

assert req_exec.clear_empty_params(body) == cleared_body


@pytest.mark.parametrize(
"accept_header",
["", "application/xml", "application/json",
"text/html", "application/xhtml+xml", "image/jpeg"]
)
@pytest.mark.asyncio
async def test_overwrite_default_request_executor_headers(accept_header):
Comment on lines +117 to +123
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parametrized test added to test out various Accept headers

org_url = "https://test.okta.com"
token = "TOKEN"
config = {'client': {'orgUrl': org_url,
'token': token,
'rateLimit': {},
'authorizationMode': None}}
req_exec = RequestExecutor(config=config, cache=None)

# overwrite headers if parameter present
header_overwrite = {'Accept': accept_header} if accept_header else {}
request, error = await req_exec.create_request(
'GET',
f'{org_url}/api/v1/users',
{},
header_overwrite,
{}
)
assert request is not None
assert request["headers"]["Accept"] ==\
accept_header if accept_header else "application/json"