-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_async_http_client.py
More file actions
365 lines (310 loc) · 12.3 KB
/
test_async_http_client.py
File metadata and controls
365 lines (310 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from platform import python_version
import httpx
import pytest
from unittest.mock import AsyncMock
from tests.test_sync_http_client import STATUS_CODE_TO_EXCEPTION_MAPPING
from workos.exceptions import (
BadRequestException,
BaseRequestException,
ConflictException,
ServerException,
)
from workos.utils.http_client import AsyncHTTPClient
@pytest.mark.asyncio
class TestAsyncHTTPClient(object):
@pytest.fixture(autouse=True)
def setup(self):
response = httpx.Response(200, json={"message": "Success!"})
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"message": "Success!"})
self.http_client = AsyncHTTPClient(
api_key="sk_test",
base_url="https://api.workos.test/",
client_id="client_b27needthisforssotemxo",
version="test",
transport=httpx.MockTransport(handler),
)
self.http_client._client.request = AsyncMock(
return_value=response,
)
@pytest.mark.parametrize(
"method,status_code,expected_response",
[
("GET", 200, {"message": "Success!"}),
("DELETE", 204, None),
("DELETE", 202, None),
],
)
async def test_request_without_body(
self, method: str, status_code: int, expected_response: dict
):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=status_code, json=expected_response
),
)
response = await self.http_client.request(
"events", method=method, params={"test_param": "test_value"}
)
self.http_client._client.request.assert_called_with(
method=method,
url="https://api.workos.test/events",
headers=httpx.Headers(
{
"accept": "application/json",
"content-type": "application/json",
"user-agent": f"WorkOS Python/{python_version()} Python SDK/test",
"authorization": "Bearer sk_test",
}
),
params={"test_param": "test_value"},
timeout=25,
)
assert response == expected_response
@pytest.mark.parametrize(
"method,status_code,expected_response",
[
("POST", 201, {"message": "Success!"}),
("PUT", 200, {"message": "Success!"}),
("PATCH", 200, {"message": "Success!"}),
],
)
async def test_request_with_body(
self, method: str, status_code: int, expected_response: dict
):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=status_code, json=expected_response
),
)
response = await self.http_client.request(
"events", method=method, json={"test_param": "test_value"}
)
self.http_client._client.request.assert_called_with(
method=method,
url="https://api.workos.test/events",
headers=httpx.Headers(
{
"accept": "application/json",
"content-type": "application/json",
"user-agent": f"WorkOS Python/{python_version()} Python SDK/test",
"authorization": "Bearer sk_test",
}
),
params=None,
json={"test_param": "test_value"},
timeout=25,
)
assert response == expected_response
@pytest.mark.parametrize(
"method,status_code,expected_response",
[
("POST", 201, {"message": "Success!"}),
("PUT", 200, {"message": "Success!"}),
("PATCH", 200, {"message": "Success!"}),
],
)
async def test_request_with_body_and_query_parameters(
self, method: str, status_code: int, expected_response: dict
):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=status_code, json=expected_response
),
)
response = await self.http_client.request(
"events",
method=method,
params={"test_param": "test_param_value"},
json={"test_json": "test_json_value"},
)
self.http_client._client.request.assert_called_with(
method=method,
url="https://api.workos.test/events",
headers=httpx.Headers(
{
"accept": "application/json",
"content-type": "application/json",
"user-agent": f"WorkOS Python/{python_version()} Python SDK/test",
"authorization": "Bearer sk_test",
}
),
params={"test_param": "test_param_value"},
json={"test_json": "test_json_value"},
timeout=25,
)
assert response == expected_response
@pytest.mark.parametrize(
"status_code,expected_exception",
STATUS_CODE_TO_EXCEPTION_MAPPING,
)
async def test_request_raises_expected_exception_for_status_code(
self, status_code: int, expected_exception: BaseRequestException
):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(status_code=status_code),
)
with pytest.raises(expected_exception): # type: ignore
await self.http_client.request("bad_place")
@pytest.mark.parametrize(
"status_code,expected_exception",
STATUS_CODE_TO_EXCEPTION_MAPPING,
)
async def test_request_exceptions_include_expected_request_data(
self, status_code: int, expected_exception: BaseRequestException
):
request_id = "request-123"
response_message = "stuff happened"
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=status_code,
json={"message": response_message},
headers={"X-Request-ID": request_id},
),
)
try:
await self.http_client.request("bad_place")
except expected_exception as ex: # type: ignore
assert ex.message == response_message
assert ex.request_id == request_id
assert ex.__class__ == expected_exception
async def test_bad_request_exceptions_include_expected_request_data(self):
request_id = "request-123"
error = "example_error"
error_description = "Example error description"
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=400,
json={"error": error, "error_description": error_description},
headers={"X-Request-ID": request_id},
),
)
try:
await self.http_client.request("bad_place")
except BadRequestException as ex:
assert (
str(ex)
== "(message=No message, request_id=request-123, error=example_error, error_description=Example error description)"
)
assert ex.__class__ == BadRequestException
async def test_bad_request_exceptions_exclude_expected_request_data(self):
request_id = "request-123"
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=400,
json={"foo": "bar"},
headers={"X-Request-ID": request_id},
),
)
try:
await self.http_client.request("bad_place")
except BadRequestException as ex:
assert str(ex) == "(message=No message, request_id=request-123, foo=bar)"
assert ex.__class__ == BadRequestException
async def test_request_bad_body_raises_expected_exception_with_request_data(self):
request_id = "request-123"
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=200,
content="this_isnt_json",
headers={"X-Request-ID": request_id},
),
)
try:
await self.http_client.request("bad_place")
except ServerException as ex:
assert ex.message is None
assert ex.request_id == request_id
assert ex.__class__ == ServerException
async def test_conflict_exception(self):
request_id = "request-123"
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=409,
headers={"X-Request-ID": request_id},
),
)
try:
await self.http_client.request("bad_place")
except ConflictException as ex:
assert str(ex) == "(message=No message, request_id=request-123)"
assert ex.__class__ == ConflictException
async def test_request_includes_base_headers(
self, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
await self.http_client.request("ok_place")
default_headers = set(
(header[0].lower(), header[1])
for header in self.http_client.default_headers.items()
)
headers = set(request_kwargs["headers"].items())
assert default_headers.issubset(headers)
async def test_request_parses_json_when_content_type_present(self):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=200,
json={"foo": "bar"},
headers={"content-type": "application/json"},
),
)
assert await self.http_client.request("ok_place") == {"foo": "bar"}
async def test_request_parses_json_when_encoding_in_content_type(self):
self.http_client._client.request = AsyncMock(
return_value=httpx.Response(
status_code=200,
json={"foo": "bar"},
headers={"content-type": "application/json; charset=utf8"},
),
)
assert await self.http_client.request("ok_place") == {"foo": "bar"}
async def test_request_removes_none_parameter_values(
self, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
await self.http_client.request(
path="/test",
method="get",
params={"organization_id": None, "test": "value"},
)
assert request_kwargs["params"] == {"test": "value"}
async def test_request_removes_none_json_values(
self, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
await self.http_client.request(
path="/test",
method="post",
json={"organization_id": None, "test": "value"},
)
assert request_kwargs["json"] == {"test": "value"}
async def test_delete_with_body_sends_json(
self, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
await self.http_client.delete_with_body(
path="/test",
json={"resource_id": "res_01ABC"},
)
assert request_kwargs["method"] == "delete"
assert request_kwargs["json"] == {"resource_id": "res_01ABC"}
async def test_delete_with_body_sends_params(
self, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
await self.http_client.delete_with_body(
path="/test",
json={"resource_id": "res_01ABC"},
params={"org_id": "org_01ABC"},
)
assert request_kwargs["params"] == {"org_id": "org_01ABC"}
assert request_kwargs["json"] == {"resource_id": "res_01ABC"}
async def test_delete_without_body_raises_value_error(self):
with pytest.raises(
ValueError, match="Cannot send a body with a delete request"
):
await self.http_client.request(
path="/test",
method="delete",
json={"should": "fail"},
)