-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathtest_request.py
More file actions
315 lines (257 loc) · 11.1 KB
/
Copy pathtest_request.py
File metadata and controls
315 lines (257 loc) · 11.1 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
from __future__ import annotations
import asyncio
import io
import json
from collections.abc import Iterator
from http import HTTPStatus
from typing import TYPE_CHECKING, Any
from unittest import mock
from unittest.mock import AsyncMock
import aiohttp
import pytest
from aioresponses import aioresponses
from ai.backend.client.config import API_VERSION, get_config
from ai.backend.client.exceptions import BackendAPIError, BackendClientError
from ai.backend.client.request import AttachedFile, Request, Response
from ai.backend.client.session import AsyncSession, Session
if TYPE_CHECKING:
from ai.backend.client.config import APIConfig
@pytest.fixture(scope="module", autouse=True)
def api_version() -> Iterator[None]:
mock_nego_func = AsyncMock()
mock_nego_func.return_value = API_VERSION
with mock.patch("ai.backend.client.session._negotiate_api_version", mock_nego_func):
yield
@pytest.fixture
def session(defconfig: APIConfig) -> Iterator[Session]:
with Session(config=defconfig) as session:
yield session
@pytest.fixture
def mock_request_params(session: Session) -> dict[str, Any]:
return {
"method": "GET",
"path": "/function/item/",
"params": {"app": "999"},
"content": b'{"test1": 1}',
"content_type": "application/json",
}
def test_request_initialization(mock_request_params: dict[str, Any]) -> None:
rqst = Request(**mock_request_params)
assert rqst.method == mock_request_params["method"]
assert rqst.params == mock_request_params["params"]
assert rqst.path == mock_request_params["path"].lstrip("/")
assert rqst.content == mock_request_params["content"]
assert "X-BackendAI-Version" in rqst.headers
def test_request_set_content_none(mock_request_params: dict[str, Any]) -> None:
mock_request_params = mock_request_params.copy()
mock_request_params["content"] = None
rqst = Request(**mock_request_params)
assert rqst.content == b""
assert rqst._pack_content() is rqst.content
def test_request_set_content(mock_request_params: dict[str, Any]) -> None:
rqst = Request(**mock_request_params)
assert rqst.content == mock_request_params["content"]
assert rqst.content_type == "application/json"
assert rqst._pack_content() is rqst.content
mock_request_params["content"] = "hello"
mock_request_params["content_type"] = None
rqst = Request(**mock_request_params)
assert rqst.content == b"hello"
assert rqst.content_type == "text/plain"
assert rqst._pack_content() is rqst.content
mock_request_params["content"] = b"\x00\x01\xfe\xff"
mock_request_params["content_type"] = None
rqst = Request(**mock_request_params)
assert rqst.content == b"\x00\x01\xfe\xff"
assert rqst.content_type == "application/octet-stream"
assert rqst._pack_content() is rqst.content
def test_request_attach_files(mock_request_params: dict[str, Any]) -> None:
files = [
AttachedFile("test1.txt", io.BytesIO(), "application/octet-stream"),
AttachedFile("test2.txt", io.BytesIO(), "application/octet-stream"),
]
mock_request_params["content"] = b"something"
rqst = Request(**mock_request_params)
with pytest.raises(ValueError):
rqst.attach_files(files)
mock_request_params["content"] = b""
rqst = Request(**mock_request_params)
rqst.attach_files(files)
assert rqst.content_type == "multipart/form-data"
assert rqst.content == b""
packed_content = rqst._pack_content()
assert isinstance(packed_content, aiohttp.FormData)
assert packed_content.is_multipart
def test_build_correct_url(mock_request_params: dict[str, Any]) -> None:
config = get_config()
canonical_url = str(config.endpoint).rstrip("/") + "/function?app=999"
mock_request_params["path"] = "/function"
rqst = Request(**mock_request_params)
assert str(rqst._build_url()) == canonical_url
mock_request_params["path"] = "function"
rqst = Request(**mock_request_params)
assert str(rqst._build_url()) == canonical_url
async def test_fetch_invalid_method(mock_request_params: dict[str, Any]) -> None:
mock_request_params["method"] = "STRANGE"
rqst = Request(**mock_request_params)
with pytest.raises(ValueError):
async with rqst.fetch():
pass
async def test_fetch(dummy_endpoint: str) -> None:
with aioresponses() as m, Session():
body = b"hello world"
m.post(
dummy_endpoint + "function",
status=HTTPStatus.OK,
body=body,
headers={"Content-Type": "text/plain; charset=utf-8", "Content-Length": str(len(body))},
)
rqst = Request("POST", "function")
async with rqst.fetch() as resp:
assert isinstance(resp, Response)
assert resp.status == HTTPStatus.OK
assert resp.content_type == "text/plain"
assert await resp.text() == body.decode()
assert resp.content_length == len(body)
with aioresponses() as m, Session():
body = b'{"a": 1234, "b": null}'
m.post(
dummy_endpoint + "function",
status=HTTPStatus.OK,
body=body,
headers={
"Content-Type": "application/json; charset=utf-8",
"Content-Length": str(len(body)),
},
)
rqst = Request("POST", "function")
async with rqst.fetch() as resp:
assert isinstance(resp, Response)
assert resp.status == HTTPStatus.OK
assert resp.content_type == "application/json"
assert await resp.text() == body.decode()
assert await resp.json() == {"a": 1234, "b": None}
assert resp.content_length == len(body)
async def test_streaming_fetch(dummy_endpoint: str) -> None:
# Read content by chunks.
with aioresponses() as m, Session():
body = b"hello world"
m.post(
dummy_endpoint + "function",
status=HTTPStatus.OK,
body=body,
headers={"Content-Type": "text/plain; charset=utf-8", "Content-Length": str(len(body))},
)
rqst = Request("POST", "function")
async with rqst.fetch() as resp:
assert resp.status == HTTPStatus.OK
assert resp.content_type == "text/plain"
assert await resp.read(3) == b"hel"
assert await resp.read(2) == b"lo"
await resp.read()
with pytest.raises(AssertionError):
assert await resp.text()
async def test_invalid_requests(dummy_endpoint: str) -> None:
with aioresponses() as m, Session():
body = json.dumps({
"type": "https://api.backend.ai/probs/kernel-not-found",
"title": "Kernel Not Found",
}).encode("utf8")
m.post(
dummy_endpoint,
status=HTTPStatus.NOT_FOUND,
body=body,
headers={
"Content-Type": "application/problem+json; charset=utf-8",
"Content-Length": str(len(body)),
},
)
rqst = Request("POST", "/")
with pytest.raises(BackendAPIError) as e:
async with rqst.fetch():
pass
assert e.status == HTTPStatus.NOT_FOUND
assert e.data["type"] == "https://api.backend.ai/probs/kernel-not-found"
assert e.data["title"] == "Kernel Not Found"
async def test_fetch_invalid_method_async() -> None:
async with AsyncSession():
rqst = Request("STRANGE", "/")
with pytest.raises(ValueError):
async with rqst.fetch():
pass
async def test_fetch_client_error_async(dummy_endpoint: str) -> None:
with aioresponses() as m:
async with AsyncSession():
m.post(dummy_endpoint, exception=aiohttp.ClientConnectionError())
rqst = Request("POST", "/")
with pytest.raises(BackendClientError):
async with rqst.fetch():
pass
@pytest.mark.xfail
async def test_fetch_cancellation_async(dummy_endpoint: str) -> None:
# It seems that aiohttp swallows asyncio.CancelledError
with aioresponses() as m:
async with AsyncSession():
m.post(dummy_endpoint, exception=asyncio.CancelledError())
rqst = Request("POST", "/")
with pytest.raises(asyncio.CancelledError):
async with rqst.fetch():
pass
async def test_fetch_timeout_async(dummy_endpoint: str) -> None:
with aioresponses() as m:
async with AsyncSession():
m.post(dummy_endpoint, exception=TimeoutError())
rqst = Request("POST", "/")
with pytest.raises(asyncio.TimeoutError):
async with rqst.fetch():
pass
async def test_response_async(defconfig: APIConfig, dummy_endpoint: str) -> None:
body = b'{"test": 5678}'
with aioresponses() as m:
m.post(
dummy_endpoint + "function",
status=HTTPStatus.OK,
body=body,
headers={"Content-Type": "application/json", "Content-Length": str(len(body))},
)
async with AsyncSession(config=defconfig):
rqst = Request("POST", "/function")
async with rqst.fetch() as resp:
assert await resp.text() == '{"test": 5678}'
assert await resp.json() == {"test": 5678}
async def test_fetch_preserves_date_header_override(dummy_endpoint: str) -> None:
fixed_date = "Tue, 02 Sep 2025 08:00:00 GMT"
with aioresponses() as m:
m.post(dummy_endpoint + "function", status=HTTPStatus.OK, body=b"")
async with AsyncSession():
rqst = Request("POST", "function")
async with rqst.fetch(headers={"Date": fixed_date}):
pass
sent_kwargs = next(iter(m.requests.values()))[0].kwargs
assert sent_kwargs["headers"]["Date"] == fixed_date
# The internal datetime must also reflect the override so that signing
# done with `self.date` matches the Date header sent on the wire.
assert rqst.date is not None
assert rqst.date.year == 2025
assert rqst.date.month == 9
assert rqst.date.day == 2
async def test_fetch_default_date_header_when_no_override(dummy_endpoint: str) -> None:
with aioresponses() as m:
m.post(dummy_endpoint + "function", status=HTTPStatus.OK, body=b"")
async with AsyncSession():
rqst = Request("POST", "function")
async with rqst.fetch():
pass
sent_kwargs = next(iter(m.requests.values()))[0].kwargs
# Without override, fetch() auto-populates Date with self.date.isoformat().
assert rqst.date is not None
assert sent_kwargs["headers"]["Date"] == rqst.date.isoformat()
async def test_fetch_passes_through_arbitrary_header_overrides(dummy_endpoint: str) -> None:
with aioresponses() as m:
m.post(dummy_endpoint + "function", status=HTTPStatus.OK, body=b"")
async with AsyncSession():
rqst = Request("POST", "function")
async with rqst.fetch(headers={"X-Custom-Header": "custom-value"}):
pass
sent_kwargs = next(iter(m.requests.values()))[0].kwargs
assert sent_kwargs["headers"]["X-Custom-Header"] == "custom-value"