client.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. import json
  2. import mimetypes
  3. import os
  4. import sys
  5. from copy import copy
  6. from functools import partial
  7. from http import HTTPStatus
  8. from importlib import import_module
  9. from io import BytesIO, IOBase
  10. from urllib.parse import unquote_to_bytes, urljoin, urlparse, urlsplit
  11. from asgiref.sync import sync_to_async
  12. from django.conf import settings
  13. from django.core.handlers.asgi import ASGIRequest
  14. from django.core.handlers.base import BaseHandler
  15. from django.core.handlers.wsgi import LimitedStream, WSGIRequest
  16. from django.core.serializers.json import DjangoJSONEncoder
  17. from django.core.signals import got_request_exception, request_finished, request_started
  18. from django.db import close_old_connections
  19. from django.http import HttpHeaders, HttpRequest, QueryDict, SimpleCookie
  20. from django.test import signals
  21. from django.test.utils import ContextList
  22. from django.urls import resolve
  23. from django.utils.encoding import force_bytes
  24. from django.utils.functional import SimpleLazyObject
  25. from django.utils.http import urlencode
  26. from django.utils.itercompat import is_iterable
  27. from django.utils.regex_helper import _lazy_re_compile
  28. __all__ = (
  29. "AsyncClient",
  30. "AsyncRequestFactory",
  31. "Client",
  32. "RedirectCycleError",
  33. "RequestFactory",
  34. "encode_file",
  35. "encode_multipart",
  36. )
  37. BOUNDARY = "BoUnDaRyStRiNg"
  38. MULTIPART_CONTENT = "multipart/form-data; boundary=%s" % BOUNDARY
  39. CONTENT_TYPE_RE = _lazy_re_compile(r".*; charset=([\w-]+);?")
  40. # Structured suffix spec: https://tools.ietf.org/html/rfc6838#section-4.2.8
  41. JSON_CONTENT_TYPE_RE = _lazy_re_compile(r"^application\/(.+\+)?json")
  42. class RedirectCycleError(Exception):
  43. """The test client has been asked to follow a redirect loop."""
  44. def __init__(self, message, last_response):
  45. super().__init__(message)
  46. self.last_response = last_response
  47. self.redirect_chain = last_response.redirect_chain
  48. class FakePayload(IOBase):
  49. """
  50. A wrapper around BytesIO that restricts what can be read since data from
  51. the network can't be sought and cannot be read outside of its content
  52. length. This makes sure that views can't do anything under the test client
  53. that wouldn't work in real life.
  54. """
  55. def __init__(self, initial_bytes=None):
  56. self.__content = BytesIO()
  57. self.__len = 0
  58. self.read_started = False
  59. if initial_bytes is not None:
  60. self.write(initial_bytes)
  61. def __len__(self):
  62. return self.__len
  63. def read(self, size=-1, /):
  64. if not self.read_started:
  65. self.__content.seek(0)
  66. self.read_started = True
  67. if size == -1 or size is None:
  68. size = self.__len
  69. assert (
  70. self.__len >= size
  71. ), "Cannot read more than the available bytes from the HTTP incoming data."
  72. content = self.__content.read(size)
  73. self.__len -= len(content)
  74. return content
  75. def readline(self, size=-1, /):
  76. if not self.read_started:
  77. self.__content.seek(0)
  78. self.read_started = True
  79. if size == -1 or size is None:
  80. size = self.__len
  81. assert (
  82. self.__len >= size
  83. ), "Cannot read more than the available bytes from the HTTP incoming data."
  84. content = self.__content.readline(size)
  85. self.__len -= len(content)
  86. return content
  87. def write(self, b, /):
  88. if self.read_started:
  89. raise ValueError("Unable to write a payload after it's been read")
  90. content = force_bytes(b)
  91. self.__content.write(content)
  92. self.__len += len(content)
  93. def closing_iterator_wrapper(iterable, close):
  94. try:
  95. yield from iterable
  96. finally:
  97. request_finished.disconnect(close_old_connections)
  98. close() # will fire request_finished
  99. request_finished.connect(close_old_connections)
  100. async def aclosing_iterator_wrapper(iterable, close):
  101. try:
  102. async for chunk in iterable:
  103. yield chunk
  104. finally:
  105. request_finished.disconnect(close_old_connections)
  106. close() # will fire request_finished
  107. request_finished.connect(close_old_connections)
  108. def conditional_content_removal(request, response):
  109. """
  110. Simulate the behavior of most web servers by removing the content of
  111. responses for HEAD requests, 1xx, 204, and 304 responses. Ensure
  112. compliance with RFC 9112 Section 6.3.
  113. """
  114. if 100 <= response.status_code < 200 or response.status_code in (204, 304):
  115. if response.streaming:
  116. response.streaming_content = []
  117. else:
  118. response.content = b""
  119. if request.method == "HEAD":
  120. if response.streaming:
  121. response.streaming_content = []
  122. else:
  123. response.content = b""
  124. return response
  125. class ClientHandler(BaseHandler):
  126. """
  127. An HTTP Handler that can be used for testing purposes. Use the WSGI
  128. interface to compose requests, but return the raw HttpResponse object with
  129. the originating WSGIRequest attached to its ``wsgi_request`` attribute.
  130. """
  131. def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
  132. self.enforce_csrf_checks = enforce_csrf_checks
  133. super().__init__(*args, **kwargs)
  134. def __call__(self, environ):
  135. # Set up middleware if needed. We couldn't do this earlier, because
  136. # settings weren't available.
  137. if self._middleware_chain is None:
  138. self.load_middleware()
  139. request_started.disconnect(close_old_connections)
  140. request_started.send(sender=self.__class__, environ=environ)
  141. request_started.connect(close_old_connections)
  142. request = WSGIRequest(environ)
  143. # sneaky little hack so that we can easily get round
  144. # CsrfViewMiddleware. This makes life easier, and is probably
  145. # required for backwards compatibility with external tests against
  146. # admin views.
  147. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
  148. # Request goes through middleware.
  149. response = self.get_response(request)
  150. # Simulate behaviors of most web servers.
  151. conditional_content_removal(request, response)
  152. # Attach the originating request to the response so that it could be
  153. # later retrieved.
  154. response.wsgi_request = request
  155. # Emulate a WSGI server by calling the close method on completion.
  156. if response.streaming:
  157. if response.is_async:
  158. response.streaming_content = aclosing_iterator_wrapper(
  159. response.streaming_content, response.close
  160. )
  161. else:
  162. response.streaming_content = closing_iterator_wrapper(
  163. response.streaming_content, response.close
  164. )
  165. else:
  166. request_finished.disconnect(close_old_connections)
  167. response.close() # will fire request_finished
  168. request_finished.connect(close_old_connections)
  169. return response
  170. class AsyncClientHandler(BaseHandler):
  171. """An async version of ClientHandler."""
  172. def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
  173. self.enforce_csrf_checks = enforce_csrf_checks
  174. super().__init__(*args, **kwargs)
  175. async def __call__(self, scope):
  176. # Set up middleware if needed. We couldn't do this earlier, because
  177. # settings weren't available.
  178. if self._middleware_chain is None:
  179. self.load_middleware(is_async=True)
  180. # Extract body file from the scope, if provided.
  181. if "_body_file" in scope:
  182. body_file = scope.pop("_body_file")
  183. else:
  184. body_file = FakePayload("")
  185. request_started.disconnect(close_old_connections)
  186. await sync_to_async(request_started.send, thread_sensitive=False)(
  187. sender=self.__class__, scope=scope
  188. )
  189. request_started.connect(close_old_connections)
  190. # Wrap FakePayload body_file to allow large read() in test environment.
  191. request = ASGIRequest(scope, LimitedStream(body_file, len(body_file)))
  192. # Sneaky little hack so that we can easily get round
  193. # CsrfViewMiddleware. This makes life easier, and is probably required
  194. # for backwards compatibility with external tests against admin views.
  195. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
  196. # Request goes through middleware.
  197. response = await self.get_response_async(request)
  198. # Simulate behaviors of most web servers.
  199. conditional_content_removal(request, response)
  200. # Attach the originating ASGI request to the response so that it could
  201. # be later retrieved.
  202. response.asgi_request = request
  203. # Emulate a server by calling the close method on completion.
  204. if response.streaming:
  205. if response.is_async:
  206. response.streaming_content = aclosing_iterator_wrapper(
  207. response.streaming_content, response.close
  208. )
  209. else:
  210. response.streaming_content = closing_iterator_wrapper(
  211. response.streaming_content, response.close
  212. )
  213. else:
  214. request_finished.disconnect(close_old_connections)
  215. # Will fire request_finished.
  216. await sync_to_async(response.close, thread_sensitive=False)()
  217. request_finished.connect(close_old_connections)
  218. return response
  219. def store_rendered_templates(store, signal, sender, template, context, **kwargs):
  220. """
  221. Store templates and contexts that are rendered.
  222. The context is copied so that it is an accurate representation at the time
  223. of rendering.
  224. """
  225. store.setdefault("templates", []).append(template)
  226. if "context" not in store:
  227. store["context"] = ContextList()
  228. store["context"].append(copy(context))
  229. def encode_multipart(boundary, data):
  230. """
  231. Encode multipart POST data from a dictionary of form values.
  232. The key will be used as the form data name; the value will be transmitted
  233. as content. If the value is a file, the contents of the file will be sent
  234. as an application/octet-stream; otherwise, str(value) will be sent.
  235. """
  236. lines = []
  237. def to_bytes(s):
  238. return force_bytes(s, settings.DEFAULT_CHARSET)
  239. # Not by any means perfect, but good enough for our purposes.
  240. def is_file(thing):
  241. return hasattr(thing, "read") and callable(thing.read)
  242. # Each bit of the multipart form data could be either a form value or a
  243. # file, or a *list* of form values and/or files. Remember that HTTP field
  244. # names can be duplicated!
  245. for key, value in data.items():
  246. if value is None:
  247. raise TypeError(
  248. "Cannot encode None for key '%s' as POST data. Did you mean "
  249. "to pass an empty string or omit the value?" % key
  250. )
  251. elif is_file(value):
  252. lines.extend(encode_file(boundary, key, value))
  253. elif not isinstance(value, str) and is_iterable(value):
  254. for item in value:
  255. if is_file(item):
  256. lines.extend(encode_file(boundary, key, item))
  257. else:
  258. lines.extend(
  259. to_bytes(val)
  260. for val in [
  261. "--%s" % boundary,
  262. 'Content-Disposition: form-data; name="%s"' % key,
  263. "",
  264. item,
  265. ]
  266. )
  267. else:
  268. lines.extend(
  269. to_bytes(val)
  270. for val in [
  271. "--%s" % boundary,
  272. 'Content-Disposition: form-data; name="%s"' % key,
  273. "",
  274. value,
  275. ]
  276. )
  277. lines.extend(
  278. [
  279. to_bytes("--%s--" % boundary),
  280. b"",
  281. ]
  282. )
  283. return b"\r\n".join(lines)
  284. def encode_file(boundary, key, file):
  285. def to_bytes(s):
  286. return force_bytes(s, settings.DEFAULT_CHARSET)
  287. # file.name might not be a string. For example, it's an int for
  288. # tempfile.TemporaryFile().
  289. file_has_string_name = hasattr(file, "name") and isinstance(file.name, str)
  290. filename = os.path.basename(file.name) if file_has_string_name else ""
  291. if hasattr(file, "content_type"):
  292. content_type = file.content_type
  293. elif filename:
  294. content_type = mimetypes.guess_type(filename)[0]
  295. else:
  296. content_type = None
  297. if content_type is None:
  298. content_type = "application/octet-stream"
  299. filename = filename or key
  300. return [
  301. to_bytes("--%s" % boundary),
  302. to_bytes(
  303. 'Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)
  304. ),
  305. to_bytes("Content-Type: %s" % content_type),
  306. b"",
  307. to_bytes(file.read()),
  308. ]
  309. class RequestFactory:
  310. """
  311. Class that lets you create mock Request objects for use in testing.
  312. Usage:
  313. rf = RequestFactory()
  314. get_request = rf.get('/hello/')
  315. post_request = rf.post('/submit/', {'foo': 'bar'})
  316. Once you have a request object you can pass it to any view function,
  317. just as if that view had been hooked up using a URLconf.
  318. """
  319. def __init__(self, *, json_encoder=DjangoJSONEncoder, headers=None, **defaults):
  320. self.json_encoder = json_encoder
  321. self.defaults = defaults
  322. self.cookies = SimpleCookie()
  323. self.errors = BytesIO()
  324. if headers:
  325. self.defaults.update(HttpHeaders.to_wsgi_names(headers))
  326. def _base_environ(self, **request):
  327. """
  328. The base environment for a request.
  329. """
  330. # This is a minimal valid WSGI environ dictionary, plus:
  331. # - HTTP_COOKIE: for cookie support,
  332. # - REMOTE_ADDR: often useful, see #8551.
  333. # See https://www.python.org/dev/peps/pep-3333/#environ-variables
  334. return {
  335. "HTTP_COOKIE": "; ".join(
  336. sorted(
  337. "%s=%s" % (morsel.key, morsel.coded_value)
  338. for morsel in self.cookies.values()
  339. )
  340. ),
  341. "PATH_INFO": "/",
  342. "REMOTE_ADDR": "127.0.0.1",
  343. "REQUEST_METHOD": "GET",
  344. "SCRIPT_NAME": "",
  345. "SERVER_NAME": "testserver",
  346. "SERVER_PORT": "80",
  347. "SERVER_PROTOCOL": "HTTP/1.1",
  348. "wsgi.version": (1, 0),
  349. "wsgi.url_scheme": "http",
  350. "wsgi.input": FakePayload(b""),
  351. "wsgi.errors": self.errors,
  352. "wsgi.multiprocess": True,
  353. "wsgi.multithread": False,
  354. "wsgi.run_once": False,
  355. **self.defaults,
  356. **request,
  357. }
  358. def request(self, **request):
  359. "Construct a generic request object."
  360. return WSGIRequest(self._base_environ(**request))
  361. def _encode_data(self, data, content_type):
  362. if content_type is MULTIPART_CONTENT:
  363. return encode_multipart(BOUNDARY, data)
  364. else:
  365. # Encode the content so that the byte representation is correct.
  366. match = CONTENT_TYPE_RE.match(content_type)
  367. if match:
  368. charset = match[1]
  369. else:
  370. charset = settings.DEFAULT_CHARSET
  371. return force_bytes(data, encoding=charset)
  372. def _encode_json(self, data, content_type):
  373. """
  374. Return encoded JSON if data is a dict, list, or tuple and content_type
  375. is application/json.
  376. """
  377. should_encode = JSON_CONTENT_TYPE_RE.match(content_type) and isinstance(
  378. data, (dict, list, tuple)
  379. )
  380. return json.dumps(data, cls=self.json_encoder) if should_encode else data
  381. def _get_path(self, parsed):
  382. path = parsed.path
  383. # If there are parameters, add them
  384. if parsed.params:
  385. path += ";" + parsed.params
  386. path = unquote_to_bytes(path)
  387. # Replace the behavior where non-ASCII values in the WSGI environ are
  388. # arbitrarily decoded with ISO-8859-1.
  389. # Refs comment in `get_bytes_from_wsgi()`.
  390. return path.decode("iso-8859-1")
  391. def get(self, path, data=None, secure=False, *, headers=None, **extra):
  392. """Construct a GET request."""
  393. data = {} if data is None else data
  394. return self.generic(
  395. "GET",
  396. path,
  397. secure=secure,
  398. headers=headers,
  399. **{
  400. "QUERY_STRING": urlencode(data, doseq=True),
  401. **extra,
  402. },
  403. )
  404. def post(
  405. self,
  406. path,
  407. data=None,
  408. content_type=MULTIPART_CONTENT,
  409. secure=False,
  410. *,
  411. headers=None,
  412. **extra,
  413. ):
  414. """Construct a POST request."""
  415. data = self._encode_json({} if data is None else data, content_type)
  416. post_data = self._encode_data(data, content_type)
  417. return self.generic(
  418. "POST",
  419. path,
  420. post_data,
  421. content_type,
  422. secure=secure,
  423. headers=headers,
  424. **extra,
  425. )
  426. def head(self, path, data=None, secure=False, *, headers=None, **extra):
  427. """Construct a HEAD request."""
  428. data = {} if data is None else data
  429. return self.generic(
  430. "HEAD",
  431. path,
  432. secure=secure,
  433. headers=headers,
  434. **{
  435. "QUERY_STRING": urlencode(data, doseq=True),
  436. **extra,
  437. },
  438. )
  439. def trace(self, path, secure=False, *, headers=None, **extra):
  440. """Construct a TRACE request."""
  441. return self.generic("TRACE", path, secure=secure, headers=headers, **extra)
  442. def options(
  443. self,
  444. path,
  445. data="",
  446. content_type="application/octet-stream",
  447. secure=False,
  448. *,
  449. headers=None,
  450. **extra,
  451. ):
  452. "Construct an OPTIONS request."
  453. return self.generic(
  454. "OPTIONS", path, data, content_type, secure=secure, headers=headers, **extra
  455. )
  456. def put(
  457. self,
  458. path,
  459. data="",
  460. content_type="application/octet-stream",
  461. secure=False,
  462. *,
  463. headers=None,
  464. **extra,
  465. ):
  466. """Construct a PUT request."""
  467. data = self._encode_json(data, content_type)
  468. return self.generic(
  469. "PUT", path, data, content_type, secure=secure, headers=headers, **extra
  470. )
  471. def patch(
  472. self,
  473. path,
  474. data="",
  475. content_type="application/octet-stream",
  476. secure=False,
  477. *,
  478. headers=None,
  479. **extra,
  480. ):
  481. """Construct a PATCH request."""
  482. data = self._encode_json(data, content_type)
  483. return self.generic(
  484. "PATCH", path, data, content_type, secure=secure, headers=headers, **extra
  485. )
  486. def delete(
  487. self,
  488. path,
  489. data="",
  490. content_type="application/octet-stream",
  491. secure=False,
  492. *,
  493. headers=None,
  494. **extra,
  495. ):
  496. """Construct a DELETE request."""
  497. data = self._encode_json(data, content_type)
  498. return self.generic(
  499. "DELETE", path, data, content_type, secure=secure, headers=headers, **extra
  500. )
  501. def generic(
  502. self,
  503. method,
  504. path,
  505. data="",
  506. content_type="application/octet-stream",
  507. secure=False,
  508. *,
  509. headers=None,
  510. **extra,
  511. ):
  512. """Construct an arbitrary HTTP request."""
  513. parsed = urlparse(str(path)) # path can be lazy
  514. data = force_bytes(data, settings.DEFAULT_CHARSET)
  515. r = {
  516. "PATH_INFO": self._get_path(parsed),
  517. "REQUEST_METHOD": method,
  518. "SERVER_PORT": "443" if secure else "80",
  519. "wsgi.url_scheme": "https" if secure else "http",
  520. }
  521. if data:
  522. r.update(
  523. {
  524. "CONTENT_LENGTH": str(len(data)),
  525. "CONTENT_TYPE": content_type,
  526. "wsgi.input": FakePayload(data),
  527. }
  528. )
  529. if headers:
  530. extra.update(HttpHeaders.to_wsgi_names(headers))
  531. r.update(extra)
  532. # If QUERY_STRING is absent or empty, we want to extract it from the URL.
  533. if not r.get("QUERY_STRING"):
  534. # WSGI requires latin-1 encoded strings. See get_path_info().
  535. query_string = parsed[4].encode().decode("iso-8859-1")
  536. r["QUERY_STRING"] = query_string
  537. return self.request(**r)
  538. class AsyncRequestFactory(RequestFactory):
  539. """
  540. Class that lets you create mock ASGI-like Request objects for use in
  541. testing. Usage:
  542. rf = AsyncRequestFactory()
  543. get_request = await rf.get('/hello/')
  544. post_request = await rf.post('/submit/', {'foo': 'bar'})
  545. Once you have a request object you can pass it to any view function,
  546. including synchronous ones. The reason we have a separate class here is:
  547. a) this makes ASGIRequest subclasses, and
  548. b) AsyncTestClient can subclass it.
  549. """
  550. def _base_scope(self, **request):
  551. """The base scope for a request."""
  552. # This is a minimal valid ASGI scope, plus:
  553. # - headers['cookie'] for cookie support,
  554. # - 'client' often useful, see #8551.
  555. scope = {
  556. "asgi": {"version": "3.0"},
  557. "type": "http",
  558. "http_version": "1.1",
  559. "client": ["127.0.0.1", 0],
  560. "server": ("testserver", "80"),
  561. "scheme": "http",
  562. "method": "GET",
  563. "headers": [],
  564. **self.defaults,
  565. **request,
  566. }
  567. scope["headers"].append(
  568. (
  569. b"cookie",
  570. b"; ".join(
  571. sorted(
  572. ("%s=%s" % (morsel.key, morsel.coded_value)).encode("ascii")
  573. for morsel in self.cookies.values()
  574. )
  575. ),
  576. )
  577. )
  578. return scope
  579. def request(self, **request):
  580. """Construct a generic request object."""
  581. # This is synchronous, which means all methods on this class are.
  582. # AsyncClient, however, has an async request function, which makes all
  583. # its methods async.
  584. if "_body_file" in request:
  585. body_file = request.pop("_body_file")
  586. else:
  587. body_file = FakePayload("")
  588. # Wrap FakePayload body_file to allow large read() in test environment.
  589. return ASGIRequest(
  590. self._base_scope(**request), LimitedStream(body_file, len(body_file))
  591. )
  592. def generic(
  593. self,
  594. method,
  595. path,
  596. data="",
  597. content_type="application/octet-stream",
  598. secure=False,
  599. *,
  600. headers=None,
  601. **extra,
  602. ):
  603. """Construct an arbitrary HTTP request."""
  604. parsed = urlparse(str(path)) # path can be lazy.
  605. data = force_bytes(data, settings.DEFAULT_CHARSET)
  606. s = {
  607. "method": method,
  608. "path": self._get_path(parsed),
  609. "server": ("127.0.0.1", "443" if secure else "80"),
  610. "scheme": "https" if secure else "http",
  611. "headers": [(b"host", b"testserver")],
  612. }
  613. if data:
  614. s["headers"].extend(
  615. [
  616. (b"content-length", str(len(data)).encode("ascii")),
  617. (b"content-type", content_type.encode("ascii")),
  618. ]
  619. )
  620. s["_body_file"] = FakePayload(data)
  621. follow = extra.pop("follow", None)
  622. if follow is not None:
  623. s["follow"] = follow
  624. if query_string := extra.pop("QUERY_STRING", None):
  625. s["query_string"] = query_string
  626. if headers:
  627. extra.update(HttpHeaders.to_asgi_names(headers))
  628. s["headers"] += [
  629. # Avoid breaking test clients that just want to supply normalized
  630. # ASGI names, regardless of the fact that ASGIRequest drops headers
  631. # with underscores (CVE-2026-3902).
  632. (key.lower().replace("_", "-").encode("ascii"), value.encode("latin1"))
  633. for key, value in extra.items()
  634. ]
  635. # If QUERY_STRING is absent or empty, we want to extract it from the
  636. # URL.
  637. if not s.get("query_string"):
  638. s["query_string"] = parsed[4]
  639. return self.request(**s)
  640. class ClientMixin:
  641. """
  642. Mixin with common methods between Client and AsyncClient.
  643. """
  644. def store_exc_info(self, **kwargs):
  645. """Store exceptions when they are generated by a view."""
  646. self.exc_info = sys.exc_info()
  647. def check_exception(self, response):
  648. """
  649. Look for a signaled exception, clear the current context exception
  650. data, re-raise the signaled exception, and clear the signaled exception
  651. from the local cache.
  652. """
  653. response.exc_info = self.exc_info
  654. if self.exc_info:
  655. _, exc_value, _ = self.exc_info
  656. self.exc_info = None
  657. if self.raise_request_exception:
  658. raise exc_value
  659. @property
  660. def session(self):
  661. """Return the current session variables."""
  662. engine = import_module(settings.SESSION_ENGINE)
  663. cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
  664. if cookie:
  665. return engine.SessionStore(cookie.value)
  666. session = engine.SessionStore()
  667. session.save()
  668. self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
  669. return session
  670. def login(self, **credentials):
  671. """
  672. Set the Factory to appear as if it has successfully logged into a site.
  673. Return True if login is possible or False if the provided credentials
  674. are incorrect.
  675. """
  676. from django.contrib.auth import authenticate
  677. user = authenticate(**credentials)
  678. if user:
  679. self._login(user)
  680. return True
  681. return False
  682. def force_login(self, user, backend=None):
  683. def get_backend():
  684. from django.contrib.auth import load_backend
  685. for backend_path in settings.AUTHENTICATION_BACKENDS:
  686. backend = load_backend(backend_path)
  687. if hasattr(backend, "get_user"):
  688. return backend_path
  689. if backend is None:
  690. backend = get_backend()
  691. user.backend = backend
  692. self._login(user, backend)
  693. def _login(self, user, backend=None):
  694. from django.contrib.auth import login
  695. # Create a fake request to store login details.
  696. request = HttpRequest()
  697. if self.session:
  698. request.session = self.session
  699. else:
  700. engine = import_module(settings.SESSION_ENGINE)
  701. request.session = engine.SessionStore()
  702. login(request, user, backend)
  703. # Save the session values.
  704. request.session.save()
  705. # Set the cookie to represent the session.
  706. session_cookie = settings.SESSION_COOKIE_NAME
  707. self.cookies[session_cookie] = request.session.session_key
  708. cookie_data = {
  709. "max-age": None,
  710. "path": "/",
  711. "domain": settings.SESSION_COOKIE_DOMAIN,
  712. "secure": settings.SESSION_COOKIE_SECURE or None,
  713. "expires": None,
  714. }
  715. self.cookies[session_cookie].update(cookie_data)
  716. def logout(self):
  717. """Log out the user by removing the cookies and session object."""
  718. from django.contrib.auth import get_user, logout
  719. request = HttpRequest()
  720. if self.session:
  721. request.session = self.session
  722. request.user = get_user(request)
  723. else:
  724. engine = import_module(settings.SESSION_ENGINE)
  725. request.session = engine.SessionStore()
  726. logout(request)
  727. self.cookies = SimpleCookie()
  728. def _parse_json(self, response, **extra):
  729. if not hasattr(response, "_json"):
  730. if not JSON_CONTENT_TYPE_RE.match(response.get("Content-Type")):
  731. raise ValueError(
  732. 'Content-Type header is "%s", not "application/json"'
  733. % response.get("Content-Type")
  734. )
  735. response._json = json.loads(
  736. response.content.decode(response.charset), **extra
  737. )
  738. return response._json
  739. class Client(ClientMixin, RequestFactory):
  740. """
  741. A class that can act as a client for testing purposes.
  742. It allows the user to compose GET and POST requests, and
  743. obtain the response that the server gave to those requests.
  744. The server Response objects are annotated with the details
  745. of the contexts and templates that were rendered during the
  746. process of serving the request.
  747. Client objects are stateful - they will retain cookie (and
  748. thus session) details for the lifetime of the Client instance.
  749. This is not intended as a replacement for Twill/Selenium or
  750. the like - it is here to allow testing against the
  751. contexts and templates produced by a view, rather than the
  752. HTML rendered to the end-user.
  753. """
  754. def __init__(
  755. self,
  756. enforce_csrf_checks=False,
  757. raise_request_exception=True,
  758. *,
  759. headers=None,
  760. **defaults,
  761. ):
  762. super().__init__(headers=headers, **defaults)
  763. self.handler = ClientHandler(enforce_csrf_checks)
  764. self.raise_request_exception = raise_request_exception
  765. self.exc_info = None
  766. self.extra = None
  767. self.headers = None
  768. def request(self, **request):
  769. """
  770. Make a generic request. Compose the environment dictionary and pass
  771. to the handler, return the result of the handler. Assume defaults for
  772. the query environment, which can be overridden using the arguments to
  773. the request.
  774. """
  775. environ = self._base_environ(**request)
  776. # Curry a data dictionary into an instance of the template renderer
  777. # callback function.
  778. data = {}
  779. on_template_render = partial(store_rendered_templates, data)
  780. signal_uid = "template-render-%s" % id(request)
  781. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  782. # Capture exceptions created by the handler.
  783. exception_uid = "request-exception-%s" % id(request)
  784. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  785. try:
  786. response = self.handler(environ)
  787. finally:
  788. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  789. got_request_exception.disconnect(dispatch_uid=exception_uid)
  790. # Check for signaled exceptions.
  791. self.check_exception(response)
  792. # Save the client and request that stimulated the response.
  793. response.client = self
  794. response.request = request
  795. # Add any rendered template detail to the response.
  796. response.templates = data.get("templates", [])
  797. response.context = data.get("context")
  798. response.json = partial(self._parse_json, response)
  799. # Attach the ResolverMatch instance to the response.
  800. urlconf = getattr(response.wsgi_request, "urlconf", None)
  801. response.resolver_match = SimpleLazyObject(
  802. lambda: resolve(request["PATH_INFO"], urlconf=urlconf),
  803. )
  804. # Flatten a single context. Not really necessary anymore thanks to the
  805. # __getattr__ flattening in ContextList, but has some edge case
  806. # backwards compatibility implications.
  807. if response.context and len(response.context) == 1:
  808. response.context = response.context[0]
  809. # Update persistent cookie data.
  810. if response.cookies:
  811. self.cookies.update(response.cookies)
  812. return response
  813. def get(
  814. self,
  815. path,
  816. data=None,
  817. follow=False,
  818. secure=False,
  819. *,
  820. headers=None,
  821. **extra,
  822. ):
  823. """Request a response from the server using GET."""
  824. self.extra = extra
  825. self.headers = headers
  826. response = super().get(path, data=data, secure=secure, headers=headers, **extra)
  827. if follow:
  828. response = self._handle_redirects(
  829. response, data=data, headers=headers, **extra
  830. )
  831. return response
  832. def post(
  833. self,
  834. path,
  835. data=None,
  836. content_type=MULTIPART_CONTENT,
  837. follow=False,
  838. secure=False,
  839. *,
  840. headers=None,
  841. **extra,
  842. ):
  843. """Request a response from the server using POST."""
  844. self.extra = extra
  845. self.headers = headers
  846. response = super().post(
  847. path,
  848. data=data,
  849. content_type=content_type,
  850. secure=secure,
  851. headers=headers,
  852. **extra,
  853. )
  854. if follow:
  855. response = self._handle_redirects(
  856. response, data=data, content_type=content_type, headers=headers, **extra
  857. )
  858. return response
  859. def head(
  860. self,
  861. path,
  862. data=None,
  863. follow=False,
  864. secure=False,
  865. *,
  866. headers=None,
  867. **extra,
  868. ):
  869. """Request a response from the server using HEAD."""
  870. self.extra = extra
  871. self.headers = headers
  872. response = super().head(
  873. path, data=data, secure=secure, headers=headers, **extra
  874. )
  875. if follow:
  876. response = self._handle_redirects(
  877. response, data=data, headers=headers, **extra
  878. )
  879. return response
  880. def options(
  881. self,
  882. path,
  883. data="",
  884. content_type="application/octet-stream",
  885. follow=False,
  886. secure=False,
  887. *,
  888. headers=None,
  889. **extra,
  890. ):
  891. """Request a response from the server using OPTIONS."""
  892. self.extra = extra
  893. self.headers = headers
  894. response = super().options(
  895. path,
  896. data=data,
  897. content_type=content_type,
  898. secure=secure,
  899. headers=headers,
  900. **extra,
  901. )
  902. if follow:
  903. response = self._handle_redirects(
  904. response, data=data, content_type=content_type, headers=headers, **extra
  905. )
  906. return response
  907. def put(
  908. self,
  909. path,
  910. data="",
  911. content_type="application/octet-stream",
  912. follow=False,
  913. secure=False,
  914. *,
  915. headers=None,
  916. **extra,
  917. ):
  918. """Send a resource to the server using PUT."""
  919. self.extra = extra
  920. self.headers = headers
  921. response = super().put(
  922. path,
  923. data=data,
  924. content_type=content_type,
  925. secure=secure,
  926. headers=headers,
  927. **extra,
  928. )
  929. if follow:
  930. response = self._handle_redirects(
  931. response, data=data, content_type=content_type, headers=headers, **extra
  932. )
  933. return response
  934. def patch(
  935. self,
  936. path,
  937. data="",
  938. content_type="application/octet-stream",
  939. follow=False,
  940. secure=False,
  941. *,
  942. headers=None,
  943. **extra,
  944. ):
  945. """Send a resource to the server using PATCH."""
  946. self.extra = extra
  947. self.headers = headers
  948. response = super().patch(
  949. path,
  950. data=data,
  951. content_type=content_type,
  952. secure=secure,
  953. headers=headers,
  954. **extra,
  955. )
  956. if follow:
  957. response = self._handle_redirects(
  958. response, data=data, content_type=content_type, headers=headers, **extra
  959. )
  960. return response
  961. def delete(
  962. self,
  963. path,
  964. data="",
  965. content_type="application/octet-stream",
  966. follow=False,
  967. secure=False,
  968. *,
  969. headers=None,
  970. **extra,
  971. ):
  972. """Send a DELETE request to the server."""
  973. self.extra = extra
  974. self.headers = headers
  975. response = super().delete(
  976. path,
  977. data=data,
  978. content_type=content_type,
  979. secure=secure,
  980. headers=headers,
  981. **extra,
  982. )
  983. if follow:
  984. response = self._handle_redirects(
  985. response, data=data, content_type=content_type, headers=headers, **extra
  986. )
  987. return response
  988. def trace(
  989. self,
  990. path,
  991. data="",
  992. follow=False,
  993. secure=False,
  994. *,
  995. headers=None,
  996. **extra,
  997. ):
  998. """Send a TRACE request to the server."""
  999. self.extra = extra
  1000. self.headers = headers
  1001. response = super().trace(
  1002. path, data=data, secure=secure, headers=headers, **extra
  1003. )
  1004. if follow:
  1005. response = self._handle_redirects(
  1006. response, data=data, headers=headers, **extra
  1007. )
  1008. return response
  1009. def _handle_redirects(
  1010. self,
  1011. response,
  1012. data="",
  1013. content_type="",
  1014. headers=None,
  1015. **extra,
  1016. ):
  1017. """
  1018. Follow any redirects by requesting responses from the server using GET.
  1019. """
  1020. response.redirect_chain = []
  1021. redirect_status_codes = (
  1022. HTTPStatus.MOVED_PERMANENTLY,
  1023. HTTPStatus.FOUND,
  1024. HTTPStatus.SEE_OTHER,
  1025. HTTPStatus.TEMPORARY_REDIRECT,
  1026. HTTPStatus.PERMANENT_REDIRECT,
  1027. )
  1028. while response.status_code in redirect_status_codes:
  1029. response_url = response.url
  1030. redirect_chain = response.redirect_chain
  1031. redirect_chain.append((response_url, response.status_code))
  1032. url = urlsplit(response_url)
  1033. if url.scheme:
  1034. extra["wsgi.url_scheme"] = url.scheme
  1035. if url.hostname:
  1036. extra["SERVER_NAME"] = url.hostname
  1037. if url.port:
  1038. extra["SERVER_PORT"] = str(url.port)
  1039. path = url.path
  1040. # RFC 3986 Section 6.2.3: Empty path should be normalized to "/".
  1041. if not path and url.netloc:
  1042. path = "/"
  1043. # Prepend the request path to handle relative path redirects
  1044. if not path.startswith("/"):
  1045. path = urljoin(response.request["PATH_INFO"], path)
  1046. if response.status_code in (
  1047. HTTPStatus.TEMPORARY_REDIRECT,
  1048. HTTPStatus.PERMANENT_REDIRECT,
  1049. ):
  1050. # Preserve request method and query string (if needed)
  1051. # post-redirect for 307/308 responses.
  1052. request_method = response.request["REQUEST_METHOD"].lower()
  1053. if request_method not in ("get", "head"):
  1054. extra["QUERY_STRING"] = url.query
  1055. request_method = getattr(self, request_method)
  1056. else:
  1057. request_method = self.get
  1058. data = QueryDict(url.query)
  1059. content_type = None
  1060. response = request_method(
  1061. path,
  1062. data=data,
  1063. content_type=content_type,
  1064. follow=False,
  1065. headers=headers,
  1066. **extra,
  1067. )
  1068. response.redirect_chain = redirect_chain
  1069. if redirect_chain[-1] in redirect_chain[:-1]:
  1070. # Check that we're not redirecting to somewhere we've already
  1071. # been to, to prevent loops.
  1072. raise RedirectCycleError(
  1073. "Redirect loop detected.", last_response=response
  1074. )
  1075. if len(redirect_chain) > 20:
  1076. # Such a lengthy chain likely also means a loop, but one with
  1077. # a growing path, changing view, or changing query argument;
  1078. # 20 is the value of "network.http.redirection-limit" from Firefox.
  1079. raise RedirectCycleError("Too many redirects.", last_response=response)
  1080. return response
  1081. class AsyncClient(ClientMixin, AsyncRequestFactory):
  1082. """
  1083. An async version of Client that creates ASGIRequests and calls through an
  1084. async request path.
  1085. Does not currently support "follow" on its methods.
  1086. """
  1087. def __init__(
  1088. self,
  1089. enforce_csrf_checks=False,
  1090. raise_request_exception=True,
  1091. *,
  1092. headers=None,
  1093. **defaults,
  1094. ):
  1095. super().__init__(headers=headers, **defaults)
  1096. self.handler = AsyncClientHandler(enforce_csrf_checks)
  1097. self.raise_request_exception = raise_request_exception
  1098. self.exc_info = None
  1099. self.extra = None
  1100. self.headers = None
  1101. async def request(self, **request):
  1102. """
  1103. Make a generic request. Compose the scope dictionary and pass to the
  1104. handler, return the result of the handler. Assume defaults for the
  1105. query environment, which can be overridden using the arguments to the
  1106. request.
  1107. """
  1108. if "follow" in request:
  1109. raise NotImplementedError(
  1110. "AsyncClient request methods do not accept the follow parameter."
  1111. )
  1112. scope = self._base_scope(**request)
  1113. # Curry a data dictionary into an instance of the template renderer
  1114. # callback function.
  1115. data = {}
  1116. on_template_render = partial(store_rendered_templates, data)
  1117. signal_uid = "template-render-%s" % id(request)
  1118. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  1119. # Capture exceptions created by the handler.
  1120. exception_uid = "request-exception-%s" % id(request)
  1121. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  1122. try:
  1123. response = await self.handler(scope)
  1124. finally:
  1125. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  1126. got_request_exception.disconnect(dispatch_uid=exception_uid)
  1127. # Check for signaled exceptions.
  1128. self.check_exception(response)
  1129. # Save the client and request that stimulated the response.
  1130. response.client = self
  1131. response.request = request
  1132. # Add any rendered template detail to the response.
  1133. response.templates = data.get("templates", [])
  1134. response.context = data.get("context")
  1135. response.json = partial(self._parse_json, response)
  1136. # Attach the ResolverMatch instance to the response.
  1137. urlconf = getattr(response.asgi_request, "urlconf", None)
  1138. response.resolver_match = SimpleLazyObject(
  1139. lambda: resolve(request["path"], urlconf=urlconf),
  1140. )
  1141. # Flatten a single context. Not really necessary anymore thanks to the
  1142. # __getattr__ flattening in ContextList, but has some edge case
  1143. # backwards compatibility implications.
  1144. if response.context and len(response.context) == 1:
  1145. response.context = response.context[0]
  1146. # Update persistent cookie data.
  1147. if response.cookies:
  1148. self.cookies.update(response.cookies)
  1149. return response