client.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. (key.lower().encode("ascii"), value.encode("latin1"))
  630. for key, value in extra.items()
  631. ]
  632. # If QUERY_STRING is absent or empty, we want to extract it from the
  633. # URL.
  634. if not s.get("query_string"):
  635. s["query_string"] = parsed[4]
  636. return self.request(**s)
  637. class ClientMixin:
  638. """
  639. Mixin with common methods between Client and AsyncClient.
  640. """
  641. def store_exc_info(self, **kwargs):
  642. """Store exceptions when they are generated by a view."""
  643. self.exc_info = sys.exc_info()
  644. def check_exception(self, response):
  645. """
  646. Look for a signaled exception, clear the current context exception
  647. data, re-raise the signaled exception, and clear the signaled exception
  648. from the local cache.
  649. """
  650. response.exc_info = self.exc_info
  651. if self.exc_info:
  652. _, exc_value, _ = self.exc_info
  653. self.exc_info = None
  654. if self.raise_request_exception:
  655. raise exc_value
  656. @property
  657. def session(self):
  658. """Return the current session variables."""
  659. engine = import_module(settings.SESSION_ENGINE)
  660. cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
  661. if cookie:
  662. return engine.SessionStore(cookie.value)
  663. session = engine.SessionStore()
  664. session.save()
  665. self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
  666. return session
  667. def login(self, **credentials):
  668. """
  669. Set the Factory to appear as if it has successfully logged into a site.
  670. Return True if login is possible or False if the provided credentials
  671. are incorrect.
  672. """
  673. from django.contrib.auth import authenticate
  674. user = authenticate(**credentials)
  675. if user:
  676. self._login(user)
  677. return True
  678. return False
  679. def force_login(self, user, backend=None):
  680. def get_backend():
  681. from django.contrib.auth import load_backend
  682. for backend_path in settings.AUTHENTICATION_BACKENDS:
  683. backend = load_backend(backend_path)
  684. if hasattr(backend, "get_user"):
  685. return backend_path
  686. if backend is None:
  687. backend = get_backend()
  688. user.backend = backend
  689. self._login(user, backend)
  690. def _login(self, user, backend=None):
  691. from django.contrib.auth import login
  692. # Create a fake request to store login details.
  693. request = HttpRequest()
  694. if self.session:
  695. request.session = self.session
  696. else:
  697. engine = import_module(settings.SESSION_ENGINE)
  698. request.session = engine.SessionStore()
  699. login(request, user, backend)
  700. # Save the session values.
  701. request.session.save()
  702. # Set the cookie to represent the session.
  703. session_cookie = settings.SESSION_COOKIE_NAME
  704. self.cookies[session_cookie] = request.session.session_key
  705. cookie_data = {
  706. "max-age": None,
  707. "path": "/",
  708. "domain": settings.SESSION_COOKIE_DOMAIN,
  709. "secure": settings.SESSION_COOKIE_SECURE or None,
  710. "expires": None,
  711. }
  712. self.cookies[session_cookie].update(cookie_data)
  713. def logout(self):
  714. """Log out the user by removing the cookies and session object."""
  715. from django.contrib.auth import get_user, logout
  716. request = HttpRequest()
  717. if self.session:
  718. request.session = self.session
  719. request.user = get_user(request)
  720. else:
  721. engine = import_module(settings.SESSION_ENGINE)
  722. request.session = engine.SessionStore()
  723. logout(request)
  724. self.cookies = SimpleCookie()
  725. def _parse_json(self, response, **extra):
  726. if not hasattr(response, "_json"):
  727. if not JSON_CONTENT_TYPE_RE.match(response.get("Content-Type")):
  728. raise ValueError(
  729. 'Content-Type header is "%s", not "application/json"'
  730. % response.get("Content-Type")
  731. )
  732. response._json = json.loads(
  733. response.content.decode(response.charset), **extra
  734. )
  735. return response._json
  736. class Client(ClientMixin, RequestFactory):
  737. """
  738. A class that can act as a client for testing purposes.
  739. It allows the user to compose GET and POST requests, and
  740. obtain the response that the server gave to those requests.
  741. The server Response objects are annotated with the details
  742. of the contexts and templates that were rendered during the
  743. process of serving the request.
  744. Client objects are stateful - they will retain cookie (and
  745. thus session) details for the lifetime of the Client instance.
  746. This is not intended as a replacement for Twill/Selenium or
  747. the like - it is here to allow testing against the
  748. contexts and templates produced by a view, rather than the
  749. HTML rendered to the end-user.
  750. """
  751. def __init__(
  752. self,
  753. enforce_csrf_checks=False,
  754. raise_request_exception=True,
  755. *,
  756. headers=None,
  757. **defaults,
  758. ):
  759. super().__init__(headers=headers, **defaults)
  760. self.handler = ClientHandler(enforce_csrf_checks)
  761. self.raise_request_exception = raise_request_exception
  762. self.exc_info = None
  763. self.extra = None
  764. self.headers = None
  765. def request(self, **request):
  766. """
  767. Make a generic request. Compose the environment dictionary and pass
  768. to the handler, return the result of the handler. Assume defaults for
  769. the query environment, which can be overridden using the arguments to
  770. the request.
  771. """
  772. environ = self._base_environ(**request)
  773. # Curry a data dictionary into an instance of the template renderer
  774. # callback function.
  775. data = {}
  776. on_template_render = partial(store_rendered_templates, data)
  777. signal_uid = "template-render-%s" % id(request)
  778. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  779. # Capture exceptions created by the handler.
  780. exception_uid = "request-exception-%s" % id(request)
  781. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  782. try:
  783. response = self.handler(environ)
  784. finally:
  785. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  786. got_request_exception.disconnect(dispatch_uid=exception_uid)
  787. # Check for signaled exceptions.
  788. self.check_exception(response)
  789. # Save the client and request that stimulated the response.
  790. response.client = self
  791. response.request = request
  792. # Add any rendered template detail to the response.
  793. response.templates = data.get("templates", [])
  794. response.context = data.get("context")
  795. response.json = partial(self._parse_json, response)
  796. # Attach the ResolverMatch instance to the response.
  797. urlconf = getattr(response.wsgi_request, "urlconf", None)
  798. response.resolver_match = SimpleLazyObject(
  799. lambda: resolve(request["PATH_INFO"], urlconf=urlconf),
  800. )
  801. # Flatten a single context. Not really necessary anymore thanks to the
  802. # __getattr__ flattening in ContextList, but has some edge case
  803. # backwards compatibility implications.
  804. if response.context and len(response.context) == 1:
  805. response.context = response.context[0]
  806. # Update persistent cookie data.
  807. if response.cookies:
  808. self.cookies.update(response.cookies)
  809. return response
  810. def get(
  811. self,
  812. path,
  813. data=None,
  814. follow=False,
  815. secure=False,
  816. *,
  817. headers=None,
  818. **extra,
  819. ):
  820. """Request a response from the server using GET."""
  821. self.extra = extra
  822. self.headers = headers
  823. response = super().get(path, data=data, secure=secure, headers=headers, **extra)
  824. if follow:
  825. response = self._handle_redirects(
  826. response, data=data, headers=headers, **extra
  827. )
  828. return response
  829. def post(
  830. self,
  831. path,
  832. data=None,
  833. content_type=MULTIPART_CONTENT,
  834. follow=False,
  835. secure=False,
  836. *,
  837. headers=None,
  838. **extra,
  839. ):
  840. """Request a response from the server using POST."""
  841. self.extra = extra
  842. self.headers = headers
  843. response = super().post(
  844. path,
  845. data=data,
  846. content_type=content_type,
  847. secure=secure,
  848. headers=headers,
  849. **extra,
  850. )
  851. if follow:
  852. response = self._handle_redirects(
  853. response, data=data, content_type=content_type, headers=headers, **extra
  854. )
  855. return response
  856. def head(
  857. self,
  858. path,
  859. data=None,
  860. follow=False,
  861. secure=False,
  862. *,
  863. headers=None,
  864. **extra,
  865. ):
  866. """Request a response from the server using HEAD."""
  867. self.extra = extra
  868. self.headers = headers
  869. response = super().head(
  870. path, data=data, secure=secure, headers=headers, **extra
  871. )
  872. if follow:
  873. response = self._handle_redirects(
  874. response, data=data, headers=headers, **extra
  875. )
  876. return response
  877. def options(
  878. self,
  879. path,
  880. data="",
  881. content_type="application/octet-stream",
  882. follow=False,
  883. secure=False,
  884. *,
  885. headers=None,
  886. **extra,
  887. ):
  888. """Request a response from the server using OPTIONS."""
  889. self.extra = extra
  890. self.headers = headers
  891. response = super().options(
  892. path,
  893. data=data,
  894. content_type=content_type,
  895. secure=secure,
  896. headers=headers,
  897. **extra,
  898. )
  899. if follow:
  900. response = self._handle_redirects(
  901. response, data=data, content_type=content_type, headers=headers, **extra
  902. )
  903. return response
  904. def put(
  905. self,
  906. path,
  907. data="",
  908. content_type="application/octet-stream",
  909. follow=False,
  910. secure=False,
  911. *,
  912. headers=None,
  913. **extra,
  914. ):
  915. """Send a resource to the server using PUT."""
  916. self.extra = extra
  917. self.headers = headers
  918. response = super().put(
  919. path,
  920. data=data,
  921. content_type=content_type,
  922. secure=secure,
  923. headers=headers,
  924. **extra,
  925. )
  926. if follow:
  927. response = self._handle_redirects(
  928. response, data=data, content_type=content_type, headers=headers, **extra
  929. )
  930. return response
  931. def patch(
  932. self,
  933. path,
  934. data="",
  935. content_type="application/octet-stream",
  936. follow=False,
  937. secure=False,
  938. *,
  939. headers=None,
  940. **extra,
  941. ):
  942. """Send a resource to the server using PATCH."""
  943. self.extra = extra
  944. self.headers = headers
  945. response = super().patch(
  946. path,
  947. data=data,
  948. content_type=content_type,
  949. secure=secure,
  950. headers=headers,
  951. **extra,
  952. )
  953. if follow:
  954. response = self._handle_redirects(
  955. response, data=data, content_type=content_type, headers=headers, **extra
  956. )
  957. return response
  958. def delete(
  959. self,
  960. path,
  961. data="",
  962. content_type="application/octet-stream",
  963. follow=False,
  964. secure=False,
  965. *,
  966. headers=None,
  967. **extra,
  968. ):
  969. """Send a DELETE request to the server."""
  970. self.extra = extra
  971. self.headers = headers
  972. response = super().delete(
  973. path,
  974. data=data,
  975. content_type=content_type,
  976. secure=secure,
  977. headers=headers,
  978. **extra,
  979. )
  980. if follow:
  981. response = self._handle_redirects(
  982. response, data=data, content_type=content_type, headers=headers, **extra
  983. )
  984. return response
  985. def trace(
  986. self,
  987. path,
  988. data="",
  989. follow=False,
  990. secure=False,
  991. *,
  992. headers=None,
  993. **extra,
  994. ):
  995. """Send a TRACE request to the server."""
  996. self.extra = extra
  997. self.headers = headers
  998. response = super().trace(
  999. path, data=data, secure=secure, headers=headers, **extra
  1000. )
  1001. if follow:
  1002. response = self._handle_redirects(
  1003. response, data=data, headers=headers, **extra
  1004. )
  1005. return response
  1006. def _handle_redirects(
  1007. self,
  1008. response,
  1009. data="",
  1010. content_type="",
  1011. headers=None,
  1012. **extra,
  1013. ):
  1014. """
  1015. Follow any redirects by requesting responses from the server using GET.
  1016. """
  1017. response.redirect_chain = []
  1018. redirect_status_codes = (
  1019. HTTPStatus.MOVED_PERMANENTLY,
  1020. HTTPStatus.FOUND,
  1021. HTTPStatus.SEE_OTHER,
  1022. HTTPStatus.TEMPORARY_REDIRECT,
  1023. HTTPStatus.PERMANENT_REDIRECT,
  1024. )
  1025. while response.status_code in redirect_status_codes:
  1026. response_url = response.url
  1027. redirect_chain = response.redirect_chain
  1028. redirect_chain.append((response_url, response.status_code))
  1029. url = urlsplit(response_url)
  1030. if url.scheme:
  1031. extra["wsgi.url_scheme"] = url.scheme
  1032. if url.hostname:
  1033. extra["SERVER_NAME"] = url.hostname
  1034. if url.port:
  1035. extra["SERVER_PORT"] = str(url.port)
  1036. path = url.path
  1037. # RFC 3986 Section 6.2.3: Empty path should be normalized to "/".
  1038. if not path and url.netloc:
  1039. path = "/"
  1040. # Prepend the request path to handle relative path redirects
  1041. if not path.startswith("/"):
  1042. path = urljoin(response.request["PATH_INFO"], path)
  1043. if response.status_code in (
  1044. HTTPStatus.TEMPORARY_REDIRECT,
  1045. HTTPStatus.PERMANENT_REDIRECT,
  1046. ):
  1047. # Preserve request method and query string (if needed)
  1048. # post-redirect for 307/308 responses.
  1049. request_method = response.request["REQUEST_METHOD"].lower()
  1050. if request_method not in ("get", "head"):
  1051. extra["QUERY_STRING"] = url.query
  1052. request_method = getattr(self, request_method)
  1053. else:
  1054. request_method = self.get
  1055. data = QueryDict(url.query)
  1056. content_type = None
  1057. response = request_method(
  1058. path,
  1059. data=data,
  1060. content_type=content_type,
  1061. follow=False,
  1062. headers=headers,
  1063. **extra,
  1064. )
  1065. response.redirect_chain = redirect_chain
  1066. if redirect_chain[-1] in redirect_chain[:-1]:
  1067. # Check that we're not redirecting to somewhere we've already
  1068. # been to, to prevent loops.
  1069. raise RedirectCycleError(
  1070. "Redirect loop detected.", last_response=response
  1071. )
  1072. if len(redirect_chain) > 20:
  1073. # Such a lengthy chain likely also means a loop, but one with
  1074. # a growing path, changing view, or changing query argument;
  1075. # 20 is the value of "network.http.redirection-limit" from Firefox.
  1076. raise RedirectCycleError("Too many redirects.", last_response=response)
  1077. return response
  1078. class AsyncClient(ClientMixin, AsyncRequestFactory):
  1079. """
  1080. An async version of Client that creates ASGIRequests and calls through an
  1081. async request path.
  1082. Does not currently support "follow" on its methods.
  1083. """
  1084. def __init__(
  1085. self,
  1086. enforce_csrf_checks=False,
  1087. raise_request_exception=True,
  1088. *,
  1089. headers=None,
  1090. **defaults,
  1091. ):
  1092. super().__init__(headers=headers, **defaults)
  1093. self.handler = AsyncClientHandler(enforce_csrf_checks)
  1094. self.raise_request_exception = raise_request_exception
  1095. self.exc_info = None
  1096. self.extra = None
  1097. self.headers = None
  1098. async def request(self, **request):
  1099. """
  1100. Make a generic request. Compose the scope dictionary and pass to the
  1101. handler, return the result of the handler. Assume defaults for the
  1102. query environment, which can be overridden using the arguments to the
  1103. request.
  1104. """
  1105. if "follow" in request:
  1106. raise NotImplementedError(
  1107. "AsyncClient request methods do not accept the follow parameter."
  1108. )
  1109. scope = self._base_scope(**request)
  1110. # Curry a data dictionary into an instance of the template renderer
  1111. # callback function.
  1112. data = {}
  1113. on_template_render = partial(store_rendered_templates, data)
  1114. signal_uid = "template-render-%s" % id(request)
  1115. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  1116. # Capture exceptions created by the handler.
  1117. exception_uid = "request-exception-%s" % id(request)
  1118. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  1119. try:
  1120. response = await self.handler(scope)
  1121. finally:
  1122. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  1123. got_request_exception.disconnect(dispatch_uid=exception_uid)
  1124. # Check for signaled exceptions.
  1125. self.check_exception(response)
  1126. # Save the client and request that stimulated the response.
  1127. response.client = self
  1128. response.request = request
  1129. # Add any rendered template detail to the response.
  1130. response.templates = data.get("templates", [])
  1131. response.context = data.get("context")
  1132. response.json = partial(self._parse_json, response)
  1133. # Attach the ResolverMatch instance to the response.
  1134. urlconf = getattr(response.asgi_request, "urlconf", None)
  1135. response.resolver_match = SimpleLazyObject(
  1136. lambda: resolve(request["path"], urlconf=urlconf),
  1137. )
  1138. # Flatten a single context. Not really necessary anymore thanks to the
  1139. # __getattr__ flattening in ContextList, but has some edge case
  1140. # backwards compatibility implications.
  1141. if response.context and len(response.context) == 1:
  1142. response.context = response.context[0]
  1143. # Update persistent cookie data.
  1144. if response.cookies:
  1145. self.cookies.update(response.cookies)
  1146. return response