-
Notifications
You must be signed in to change notification settings - Fork 18
/
testing.py
450 lines (369 loc) · 14 KB
/
testing.py
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import typing
from functools import partial
from ipaddress import IPv6Address, ip_address
from json import JSONDecodeError
from socket import AF_INET6, SOCK_STREAM, socket
from string import ascii_lowercase
from types import SimpleNamespace
import httpx
from sanic import Sanic # type: ignore
from sanic.asgi import ASGIApp # type: ignore
from sanic.exceptions import MethodNotSupported # type: ignore
from sanic.log import logger # type: ignore
from sanic.request import Request # type: ignore
from sanic.response import text # type: ignore
from websockets.legacy.client import connect
ASGI_HOST = "mockserver"
ASGI_PORT = 1234
ASGI_BASE_URL = f"http://{ASGI_HOST}:{ASGI_PORT}"
HOST = "127.0.0.1"
PORT = None
Sanic.test_mode = True
httpx_version = tuple(
map(int, httpx.__version__.strip(ascii_lowercase).split("."))
)
class TestingResponse(httpx.Response):
@property
def status(self):
return self.status_code
@property
def body(self):
return self.content
@property
def content_type(self):
return self.headers.get("content-type")
@property
def json(self):
if getattr(self, "_json", None):
return self._json
try:
self._json = super().json()
except (JSONDecodeError, UnicodeDecodeError):
self._json = None
return self._json
def _blank(*_, **__):
...
class SanicTestClient:
def __init__(
self, app: Sanic, port: typing.Optional[int] = PORT, host: str = HOST
) -> None:
"""Use port=None to bind to a random port"""
Sanic.test_mode = True
self.app = app
self.port = port
self.host = host
self._do_request = _blank
app.after_server_start(self._run_request)
def _run_request(self, *args, **kwargs):
return self._do_request(*args, **kwargs)
@classmethod
def _start_test_mode(cls, sanic, *args, **kwargs):
Sanic.test_mode = True
@classmethod
def _end_test_mode(cls, sanic, *args, **kwargs):
Sanic.test_mode = False
def get_new_session(self, **kwargs) -> httpx.AsyncClient:
return httpx.AsyncClient(verify=False, **kwargs)
async def _local_request(self, method: str, url: str, *args, **kwargs):
logger.info(url)
raw_cookies = kwargs.pop("raw_cookies", None)
session_kwargs = kwargs.pop("session_kwargs", {})
if httpx_version >= (0, 20) and method != "websocket":
kwargs["follow_redirects"] = True
allow_redirects = kwargs.pop("allow_redirects", None)
if allow_redirects is not None:
kwargs["follow_redirects"] = allow_redirects
if method == "websocket":
ws_proxy = SimpleNamespace()
async with connect(url, *args, **kwargs) as websocket:
ws_proxy.ws = websocket
ws_proxy.opened = True
return ws_proxy
else:
async with self.get_new_session(**session_kwargs) as session:
try:
if method == "request":
args = tuple([url] + list(args))
url = kwargs.pop("http_method", "GET").upper()
response = await getattr(session, method.lower())(
url, *args, **kwargs
)
except httpx.HTTPError as e:
if hasattr(e, "response"):
response = getattr(e, "response")
else:
logger.error(
f"{method.upper()} {url} received no response!",
exc_info=True,
)
return None
response.__class__ = TestingResponse
if raw_cookies:
response.raw_cookies = {}
for cookie in response.cookies.jar:
response.raw_cookies[cookie.name] = cookie
return response
@classmethod
def _collect_request(cls, results, request):
if results[0] is None:
results[0] = request
async def _collect_response(
self,
method,
url,
exceptions,
results,
sanic,
loop,
**request_kwargs,
):
try:
response = await self._local_request(method, url, **request_kwargs)
results[-1] = response
if method == "websocket":
await response.ws.close()
except Exception as e:
logger.exception("Exception")
exceptions.append(e)
finally:
self.app.stop()
async def _error_handler(self, request, exception):
if request.method in ["HEAD", "PATCH", "PUT", "DELETE"]:
return text("", exception.status_code, headers=exception.headers)
else:
return self.app.error_handler.default(request, exception)
def _sanic_endpoint_test(
self,
method: str = "get",
uri: str = "/",
gather_request: bool = True,
debug: bool = False,
server_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None,
host: str = None,
allow_none: bool = False,
*request_args,
**request_kwargs,
) -> typing.Tuple[
typing.Optional[Request], typing.Optional[TestingResponse]
]:
results = [None, None]
exceptions: typing.List[Exception] = []
server_kwargs = server_kwargs or {"auto_reload": False}
_collect_request = partial(self._collect_request, results)
self.app.router.reset()
self.app.signal_router.reset()
if gather_request:
self.app.request_middleware.appendleft( # type: ignore
_collect_request
)
self.app.exception(MethodNotSupported)(self._error_handler)
if self.port:
server_kwargs = dict(
host=host or self.host,
port=self.port,
**server_kwargs,
)
host, port = host or self.host, self.port
else:
bind = host or self.host
ip = ip_address(bind)
if isinstance(ip, IPv6Address):
sock = socket(AF_INET6, SOCK_STREAM)
port = ASGI_PORT
else:
sock = socket()
port = 0
sock.bind((bind, port))
server_kwargs = dict(sock=sock, **server_kwargs)
if isinstance(ip, IPv6Address):
host, port, _, _ = sock.getsockname()
host = f"[{host}]"
else:
host, port = sock.getsockname()
self.port = port
if uri.startswith(
("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")
):
url = uri
else:
uri = uri if uri.startswith("/") else f"/{uri}"
scheme = "ws" if method == "websocket" else "http"
url = f"{scheme}://{host}:{port}{uri}"
# Tests construct URLs using PORT = None, which means random port not
# known until this function is called, so fix that here
url = url.replace(":None/", f":{port}/")
self._do_request = partial(
self._collect_response,
method,
url,
exceptions,
results,
**request_kwargs,
)
self.app.run(debug=debug, **server_kwargs)
if exceptions:
raise ValueError(f"Exception during request: {exceptions}")
if gather_request:
try:
self.app.request_middleware.remove( # type: ignore
_collect_request
)
except BaseException: # noqa
pass
try:
request, response = results
if response is None:
if not allow_none:
raise ValueError(
"No response returned to Sanic Test Client."
)
return request, response
except BaseException: # noqa
if not allow_none:
raise ValueError(
"Request and response object expected, "
f"got ({results})"
)
return None, None
else:
try:
if results[-1] is None:
if not allow_none:
raise ValueError(
"No response returned to Sanic Test Client."
)
return None, results[-1]
except BaseException: # noqa
if not allow_none:
raise ValueError(
f"Request object expected, got ({results})"
)
return None, None
def request(self, *args, **kwargs):
return self._sanic_endpoint_test("request", *args, **kwargs)
def get(self, *args, **kwargs):
return self._sanic_endpoint_test("get", *args, **kwargs)
def post(self, *args, **kwargs):
return self._sanic_endpoint_test("post", *args, **kwargs)
def put(self, *args, **kwargs):
return self._sanic_endpoint_test("put", *args, **kwargs)
def delete(self, *args, **kwargs):
return self._sanic_endpoint_test("delete", *args, **kwargs)
def patch(self, *args, **kwargs):
return self._sanic_endpoint_test("patch", *args, **kwargs)
def options(self, *args, **kwargs):
return self._sanic_endpoint_test("options", *args, **kwargs)
def head(self, *args, **kwargs):
return self._sanic_endpoint_test("head", *args, **kwargs)
def websocket(self, *args, **kwargs):
return self._sanic_endpoint_test("websocket", *args, **kwargs)
class TestASGIApp(ASGIApp):
async def __call__(self):
await super().__call__()
return self.request
async def app_call_with_return(self, scope, receive, send):
asgi_app = await TestASGIApp.create(self, scope, receive, send)
return await asgi_app()
class SanicASGITestClient(httpx.AsyncClient):
def __init__(
self,
app: Sanic,
base_url: str = ASGI_BASE_URL,
suppress_exceptions: bool = False,
) -> None:
app.__class__.__call__ = app_call_with_return # type: ignore
app.asgi = True
self.sanic_app = app
transport = httpx.ASGITransport(app=app, client=(ASGI_HOST, ASGI_PORT))
super().__init__(transport=transport, base_url=base_url)
self.gather_request = True
self.last_request = None
def _collect_request(self, request):
if self.gather_request:
self.last_request = request
else:
self.last_request = None
@classmethod
def _start_test_mode(cls, sanic, *args, **kwargs):
Sanic.test_mode = True
@classmethod
def _end_test_mode(cls, sanic, *args, **kwargs):
Sanic.test_mode = False
async def request( # type: ignore
self, method, url, gather_request=True, *args, **kwargs
) -> typing.Tuple[
typing.Optional[Request], typing.Optional[TestingResponse]
]:
self.sanic_app.router.reset()
self.sanic_app.signal_router.reset()
await self.sanic_app._startup() # type: ignore
await self.sanic_app._server_event("init", "before")
await self.sanic_app._server_event("init", "after")
if not url.startswith(
("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")
):
url = url if url.startswith("/") else f"/{url}"
scheme = "ws" if method == "websocket" else "http"
url = f"{scheme}://{ASGI_HOST}:{ASGI_PORT}{url}"
if self._collect_request not in self.sanic_app.request_middleware:
self.sanic_app.request_middleware.appendleft( # type: ignore
self._collect_request
)
self.gather_request = gather_request
response = await super().request(method, url, *args, **kwargs)
await self.sanic_app._server_event("shutdown", "before")
await self.sanic_app._server_event("shutdown", "after")
response.__class__ = TestingResponse
if gather_request:
return self.last_request, response # type: ignore
return None, response # type: ignore
@classmethod
async def _ws_receive(cls):
return {}
@classmethod
async def _ws_send(cls, message):
pass
async def websocket(self, uri, subprotocols=None, *args, **kwargs):
scheme = "ws"
path = uri
root_path = f"{scheme}://{ASGI_HOST}:{ASGI_PORT}"
headers = kwargs.get("headers", {})
headers.setdefault("connection", "upgrade")
headers.setdefault("sec-websocket-key", "testserver==")
headers.setdefault("sec-websocket-version", "13")
if subprotocols is not None:
headers.setdefault(
"sec-websocket-protocol", ", ".join(subprotocols)
)
scope = {
"type": "websocket",
"asgi": {"version": "3.0"},
"http_version": "1.1",
"headers": [map(lambda y: y.encode(), x) for x in headers.items()],
"scheme": scheme,
"root_path": root_path,
"path": path,
"query_string": b"",
"subprotocols": subprotocols,
}
self.sanic_app.router.reset()
self.sanic_app.signal_router.reset()
await self.sanic_app._startup()
await self.sanic_app(scope, self._ws_receive, self._ws_send)
return None, {"opened": True}
def __getstate__(self):
# Cookies cannot be pickled, because they contain a ThreadLock
try:
del self._cookies
except AttributeError:
pass
return self.__dict__
def __setstate__(self, d):
try:
del d["_cookies"]
except LookupError:
pass
self.__dict__.update(d)
# Need to create a new CookieJar when unpickling,
# because it was killed on Pickle
self._cookies = httpx.Cookies()