Skip to content

Commit 5c1e671

Browse files
authored
Only attempt to parse the JSON for JSON responses (#84)
1 parent f4bc729 commit 5c1e671

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

tests/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ def __init__(self, response_dict, status_code, headers=None):
1010
self.status_code = status_code
1111
self.headers = {} if headers is None else headers
1212

13+
if "content-type" not in self.headers:
14+
self.headers["content-type"] = "application/json"
15+
1316
def json(self):
1417
return self.response_dict
1518

1619

20+
class MockRawResponse(object):
21+
def __init__(self, content, status_code, headers=None):
22+
self.content = content
23+
self.status_code = status_code
24+
self.headers = {} if headers is None else headers
25+
26+
1727
@pytest.fixture
1828
def set_api_key(monkeypatch):
1929
monkeypatch.setattr(workos, "api_key", "sk_abdsomecharactersm284")
@@ -40,6 +50,17 @@ def mock(*args, **kwargs):
4050
return inner
4151

4252

53+
@pytest.fixture
54+
def mock_raw_request_method(monkeypatch):
55+
def inner(method, content, status_code, headers=None):
56+
def mock(*args, **kwargs):
57+
return MockRawResponse(content, status_code, headers=headers)
58+
59+
monkeypatch.setattr(requests, method, mock)
60+
61+
return inner
62+
63+
4364
@pytest.fixture
4465
def capture_and_mock_request(monkeypatch):
4566
def inner(method, response_dict, status_code):

tests/test_directory_sync.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,13 @@ def test_list_directories(self, mock_directories, mock_request_method):
161161

162162
assert directories == mock_directories
163163

164-
def test_delete_directory(self, mock_directories, mock_request_method):
165-
mock_request_method("delete", None, 202)
164+
def test_delete_directory(self, mock_directories, mock_raw_request_method):
165+
mock_raw_request_method(
166+
"delete",
167+
"Accepted",
168+
202,
169+
headers={"content-type": "text/plain; charset=utf-8"},
170+
)
166171

167172
response = self.directory_sync.delete_directory(directory="directory_id")
168173

tests/test_organizations.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@ def test_update_organization(self, mock_organization_updated, mock_request_metho
119119
}
120120
]
121121

122-
def test_delete_organization(self, setup, mock_request_method):
123-
mock_request_method("delete", None, 202)
122+
def test_delete_organization(self, setup, mock_raw_request_method):
123+
mock_raw_request_method(
124+
"delete",
125+
"Accepted",
126+
202,
127+
headers={"content-type": "text/plain; charset=utf-8"},
128+
)
124129

125130
response = self.organizations.delete_organization(organization="connection_id")
126131

tests/test_sso.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,13 @@ def test_list_connections(
232232

233233
assert connections_response["data"] == mock_connections["data"]
234234

235-
def test_delete_connection(self, setup_with_client_id, mock_request_method):
236-
mock_request_method("delete", None, 204)
235+
def test_delete_connection(self, setup_with_client_id, mock_raw_request_method):
236+
mock_raw_request_method(
237+
"delete",
238+
"No Content",
239+
204,
240+
headers={"content-type": "text/plain; charset=utf-8"},
241+
)
237242

238243
response = self.sso.delete_connection(connection="connection_id")
239244

workos/utils/request.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ def request(
6767
response = request_fn(url, headers=headers, json=params)
6868

6969
response_json = None
70-
try:
71-
response_json = response.json()
72-
except ValueError:
73-
raise ServerException(response)
70+
content_type = (
71+
response.headers.get("content-type")
72+
if response.headers is not None
73+
else None
74+
)
75+
if content_type == "application/json":
76+
try:
77+
response_json = response.json()
78+
except ValueError:
79+
raise ServerException(response)
7480

7581
status_code = response.status_code
7682
if status_code >= 400 and status_code < 500:

0 commit comments

Comments
 (0)