Skip to content

Commit 17051fb

Browse files
authored
Handle Content-Type headers that contain an encoding (#87)
* Handle `Content-Type` headers that contain an encoding * Backfill tests for response `content-type` handling * Format with `black`
1 parent 9002e91 commit 17051fb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/utils/test_requests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,22 @@ def test_request_includes_base_headers(self, capture_and_mock_request):
109109
headers = set(request_kwargs["headers"].items())
110110

111111
assert base_headers.issubset(headers)
112+
113+
def test_request_parses_json_when_content_type_present(self, mock_request_method):
114+
mock_request_method(
115+
"get", {"foo": "bar"}, 200, headers={"content-type": "application/json"}
116+
)
117+
118+
assert RequestHelper().request("ok_place") == {"foo": "bar"}
119+
120+
def test_request_parses_json_when_encoding_in_content_type(
121+
self, mock_request_method
122+
):
123+
mock_request_method(
124+
"get",
125+
{"foo": "bar"},
126+
200,
127+
headers={"content-type": "application/json; charset=utf8"},
128+
)
129+
130+
assert RequestHelper().request("ok_place") == {"foo": "bar"}

workos/utils/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def request(
7272
if response.headers is not None
7373
else None
7474
)
75-
if content_type == "application/json":
75+
if content_type is not None and "application/json" in content_type:
7676
try:
7777
response_json = response.json()
7878
except ValueError:

0 commit comments

Comments
 (0)