client.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. REDIRECT_STATUS_CODES = frozenset(
  43. [
  44. HTTPStatus.MOVED_PERMANENTLY,
  45. HTTPStatus.FOUND,
  46. HTTPStatus.SEE_OTHER,
  47. HTTPStatus.TEMPORARY_REDIRECT,
  48. HTTPStatus.PERMANENT_REDIRECT,
  49. ]
  50. )
  51. class RedirectCycleError(Exception):
  52. """The test client has been asked to follow a redirect loop."""
  53. def __init__(self, message, last_response):
  54. super().__init__(message)
  55. self.last_response = last_response
  56. self.redirect_chain = last_response.redirect_chain
  57. class FakePayload(IOBase):
  58. """
  59. A wrapper around BytesIO that restricts what can be read since data from
  60. the network can't be sought and cannot be read outside of its content
  61. length. This makes sure that views can't do anything under the test client
  62. that wouldn't work in real life.
  63. """
  64. def __init__(self, initial_bytes=None):
  65. self.__content = BytesIO()
  66. self.__len = 0
  67. self.read_started = False
  68. if initial_bytes is not None:
  69. self.write(initial_bytes)
  70. def __len__(self):
  71. return self.__len
  72. def read(self, size=-1, /):
  73. if not self.read_started:
  74. self.__content.seek(0)
  75. self.read_started = True
  76. if size == -1 or size is None:
  77. size = self.__len
  78. assert (
  79. self.__len >= size
  80. ), "Cannot read more than the available bytes from the HTTP incoming data."
  81. content = self.__content.read(size)
  82. self.__len -= len(content)
  83. return content
  84. def readline(self, size=-1, /):
  85. if not self.read_started:
  86. self.__content.seek(0)
  87. self.read_started = True
  88. if size == -1 or size is None:
  89. size = self.__len
  90. assert (
  91. self.__len >= size
  92. ), "Cannot read more than the available bytes from the HTTP incoming data."
  93. content = self.__content.readline(size)
  94. self.__len -= len(content)
  95. return content
  96. def write(self, b, /):
  97. if self.read_started:
  98. raise ValueError("Unable to write a payload after it's been read")
  99. content = force_bytes(b)
  100. self.__content.write(content)
  101. self.__len += len(content)
  102. def closing_iterator_wrapper(iterable, close):
  103. try:
  104. yield from iterable
  105. finally:
  106. request_finished.disconnect(close_old_connections)
  107. close() # will fire request_finished
  108. request_finished.connect(close_old_connections)
  109. async def aclosing_iterator_wrapper(iterable, close):
  110. try:
  111. async for chunk in iterable:
  112. yield chunk
  113. finally:
  114. request_finished.disconnect(close_old_connections)
  115. close() # will fire request_finished
  116. request_finished.connect(close_old_connections)
  117. def conditional_content_removal(request, response):
  118. """
  119. Simulate the behavior of most web servers by removing the content of
  120. responses for HEAD requests, 1xx, 204, and 304 responses. Ensure
  121. compliance with RFC 9112 Section 6.3.
  122. """
  123. if 100 <= response.status_code < 200 or response.status_code in (204, 304):
  124. if response.streaming:
  125. response.streaming_content = []
  126. else:
  127. response.content = b""
  128. if request.method == "HEAD":
  129. if response.streaming:
  130. response.streaming_content = []
  131. else:
  132. response.content = b""
  133. return response
  134. class ClientHandler(BaseHandler):
  135. """
  136. An HTTP Handler that can be used for testing purposes. Use the WSGI
  137. interface to compose requests, but return the raw HttpResponse object with
  138. the originating WSGIRequest attached to its ``wsgi_request`` attribute.
  139. """
  140. def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
  141. self.enforce_csrf_checks = enforce_csrf_checks
  142. super().__init__(*args, **kwargs)
  143. def __call__(self, environ):
  144. # Set up middleware if needed. We couldn't do this earlier, because
  145. # settings weren't available.
  146. if self._middleware_chain is None:
  147. self.load_middleware()
  148. request_started.disconnect(close_old_connections)
  149. request_started.send(sender=self.__class__, environ=environ)
  150. request_started.connect(close_old_connections)
  151. request = WSGIRequest(environ)
  152. # sneaky little hack so that we can easily get round
  153. # CsrfViewMiddleware. This makes life easier, and is probably
  154. # required for backwards compatibility with external tests against
  155. # admin views.
  156. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
  157. # Request goes through middleware.
  158. response = self.get_response(request)
  159. # Simulate behaviors of most web servers.
  160. conditional_content_removal(request, response)
  161. # Attach the originating request to the response so that it could be
  162. # later retrieved.
  163. response.wsgi_request = request
  164. # Emulate a WSGI server by calling the close method on completion.
  165. if response.streaming:
  166. if response.is_async:
  167. response.streaming_content = aclosing_iterator_wrapper(
  168. response.streaming_content, response.close
  169. )
  170. else:
  171. response.streaming_content = closing_iterator_wrapper(
  172. response.streaming_content, response.close
  173. )
  174. else:
  175. request_finished.disconnect(close_old_connections)
  176. response.close() # will fire request_finished
  177. request_finished.connect(close_old_connections)
  178. return response
  179. class AsyncClientHandler(BaseHandler):
  180. """An async version of ClientHandler."""
  181. def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
  182. self.enforce_csrf_checks = enforce_csrf_checks
  183. super().__init__(*args, **kwargs)
  184. async def __call__(self, scope):
  185. # Set up middleware if needed. We couldn't do this earlier, because
  186. # settings weren't available.
  187. if self._middleware_chain is None:
  188. self.load_middleware(is_async=True)
  189. # Extract body file from the scope, if provided.
  190. if "_body_file" in scope:
  191. body_file = scope.pop("_body_file")
  192. else:
  193. body_file = FakePayload("")
  194. request_started.disconnect(close_old_connections)
  195. await request_started.asend(sender=self.__class__, scope=scope)
  196. request_started.connect(close_old_connections)
  197. # Wrap FakePayload body_file to allow large read() in test environment.
  198. request = ASGIRequest(scope, LimitedStream(body_file, len(body_file)))
  199. # Sneaky little hack so that we can easily get round
  200. # CsrfViewMiddleware. This makes life easier, and is probably required
  201. # for backwards compatibility with external tests against admin views.
  202. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
  203. # Request goes through middleware.
  204. response = await self.get_response_async(request)
  205. # Simulate behaviors of most web servers.
  206. conditional_content_removal(request, response)
  207. # Attach the originating ASGI request to the response so that it could
  208. # be later retrieved.
  209. response.asgi_request = request
  210. # Emulate a server by calling the close method on completion.
  211. if response.streaming:
  212. if response.is_async:
  213. response.streaming_content = aclosing_iterator_wrapper(
  214. response.streaming_content, response.close
  215. )
  216. else:
  217. response.streaming_content = closing_iterator_wrapper(
  218. response.streaming_content, response.close
  219. )
  220. else:
  221. request_finished.disconnect(close_old_connections)
  222. # Will fire request_finished.
  223. await sync_to_async(response.close, thread_sensitive=False)()
  224. request_finished.connect(close_old_connections)
  225. return response
  226. def store_rendered_templates(store, signal, sender, template, context, **kwargs):
  227. """
  228. Store templates and contexts that are rendered.
  229. The context is copied so that it is an accurate representation at the time
  230. of rendering.
  231. """
  232. store.setdefault("templates", []).append(template)
  233. if "context" not in store:
  234. store["context"] = ContextList()
  235. store["context"].append(copy(context))
  236. def encode_multipart(boundary, data):
  237. """
  238. Encode multipart POST data from a dictionary of form values.
  239. The key will be used as the form data name; the value will be transmitted
  240. as content. If the value is a file, the contents of the file will be sent
  241. as an application/octet-stream; otherwise, str(value) will be sent.
  242. """
  243. lines = []
  244. def to_bytes(s):
  245. return force_bytes(s, settings.DEFAULT_CHARSET)
  246. # Not by any means perfect, but good enough for our purposes.
  247. def is_file(thing):
  248. return hasattr(thing, "read") and callable(thing.read)
  249. # Each bit of the multipart form data could be either a form value or a
  250. # file, or a *list* of form values and/or files. Remember that HTTP field
  251. # names can be duplicated!
  252. for key, value in data.items():
  253. if value is None:
  254. raise TypeError(
  255. "Cannot encode None for key '%s' as POST data. Did you mean "
  256. "to pass an empty string or omit the value?" % key
  257. )
  258. elif is_file(value):
  259. lines.extend(encode_file(boundary, key, value))
  260. elif not isinstance(value, str) and is_iterable(value):
  261. for item in value:
  262. if is_file(item):
  263. lines.extend(encode_file(boundary, key, item))
  264. else:
  265. lines.extend(
  266. to_bytes(val)
  267. for val in [
  268. "--%s" % boundary,
  269. 'Content-Disposition: form-data; name="%s"' % key,
  270. "",
  271. item,
  272. ]
  273. )
  274. else:
  275. lines.extend(
  276. to_bytes(val)
  277. for val in [
  278. "--%s" % boundary,
  279. 'Content-Disposition: form-data; name="%s"' % key,
  280. "",
  281. value,
  282. ]
  283. )
  284. lines.extend(
  285. [
  286. to_bytes("--%s--" % boundary),
  287. b"",
  288. ]
  289. )
  290. return b"\r\n".join(lines)
  291. def encode_file(boundary, key, file):
  292. def to_bytes(s):
  293. return force_bytes(s, settings.DEFAULT_CHARSET)
  294. # file.name might not be a string. For example, it's an int for
  295. # tempfile.TemporaryFile().
  296. file_has_string_name = hasattr(file, "name") and isinstance(file.name, str)
  297. filename = os.path.basename(file.name) if file_has_string_name else ""
  298. if hasattr(file, "content_type"):
  299. content_type = file.content_type
  300. elif filename:
  301. content_type = mimetypes.guess_type(filename)[0]
  302. else:
  303. content_type = None
  304. if content_type is None:
  305. content_type = "application/octet-stream"
  306. filename = filename or key
  307. return [
  308. to_bytes("--%s" % boundary),
  309. to_bytes(
  310. 'Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)
  311. ),
  312. to_bytes("Content-Type: %s" % content_type),
  313. b"",
  314. to_bytes(file.read()),
  315. ]
  316. class RequestFactory:
  317. """
  318. Class that lets you create mock Request objects for use in testing.
  319. Usage:
  320. rf = RequestFactory()
  321. get_request = rf.get('/hello/')
  322. post_request = rf.post('/submit/', {'foo': 'bar'})
  323. Once you have a request object you can pass it to any view function,
  324. just as if that view had been hooked up using a URLconf.
  325. """
  326. def __init__(
  327. self,
  328. *,
  329. json_encoder=DjangoJSONEncoder,
  330. headers=None,
  331. query_params=None,
  332. **defaults,
  333. ):
  334. self.json_encoder = json_encoder
  335. self.defaults = defaults
  336. self.cookies = SimpleCookie()
  337. self.errors = BytesIO()
  338. if headers:
  339. self.defaults.update(HttpHeaders.to_wsgi_names(headers))
  340. if query_params:
  341. self.defaults["QUERY_STRING"] = urlencode(query_params, doseq=True)
  342. def _base_environ(self, **request):
  343. """
  344. The base environment for a request.
  345. """
  346. # This is a minimal valid WSGI environ dictionary, plus:
  347. # - HTTP_COOKIE: for cookie support,
  348. # - REMOTE_ADDR: often useful, see #8551.
  349. # See https://www.python.org/dev/peps/pep-3333/#environ-variables
  350. return {
  351. "HTTP_COOKIE": "; ".join(
  352. sorted(
  353. "%s=%s" % (morsel.key, morsel.coded_value)
  354. for morsel in self.cookies.values()
  355. )
  356. ),
  357. "PATH_INFO": "/",
  358. "REMOTE_ADDR": "127.0.0.1",
  359. "REQUEST_METHOD": "GET",
  360. "SCRIPT_NAME": "",
  361. "SERVER_NAME": "testserver",
  362. "SERVER_PORT": "80",
  363. "SERVER_PROTOCOL": "HTTP/1.1",
  364. "wsgi.version": (1, 0),
  365. "wsgi.url_scheme": "http",
  366. "wsgi.input": FakePayload(b""),
  367. "wsgi.errors": self.errors,
  368. "wsgi.multiprocess": True,
  369. "wsgi.multithread": False,
  370. "wsgi.run_once": False,
  371. **self.defaults,
  372. **request,
  373. }
  374. def request(self, **request):
  375. "Construct a generic request object."
  376. return WSGIRequest(self._base_environ(**request))
  377. def _encode_data(self, data, content_type):
  378. if content_type is MULTIPART_CONTENT:
  379. return encode_multipart(BOUNDARY, data)
  380. else:
  381. # Encode the content so that the byte representation is correct.
  382. match = CONTENT_TYPE_RE.match(content_type)
  383. if match:
  384. charset = match[1]
  385. else:
  386. charset = settings.DEFAULT_CHARSET
  387. return force_bytes(data, encoding=charset)
  388. def _encode_json(self, data, content_type):
  389. """
  390. Return encoded JSON if data is a dict, list, or tuple and content_type
  391. is application/json.
  392. """
  393. should_encode = JSON_CONTENT_TYPE_RE.match(content_type) and isinstance(
  394. data, (dict, list, tuple)
  395. )
  396. return json.dumps(data, cls=self.json_encoder) if should_encode else data
  397. def _get_path(self, parsed):
  398. path = parsed.path
  399. # If there are parameters, add them
  400. if parsed.params:
  401. path += ";" + parsed.params
  402. path = unquote_to_bytes(path)
  403. # Replace the behavior where non-ASCII values in the WSGI environ are
  404. # arbitrarily decoded with ISO-8859-1.
  405. # Refs comment in `get_bytes_from_wsgi()`.
  406. return path.decode("iso-8859-1")
  407. def get(
  408. self, path, data=None, secure=False, *, headers=None, query_params=None, **extra
  409. ):
  410. """Construct a GET request."""
  411. if query_params and data:
  412. raise ValueError("query_params and data arguments are mutually exclusive.")
  413. query_params = data or query_params
  414. query_params = {} if query_params is None else query_params
  415. return self.generic(
  416. "GET",
  417. path,
  418. secure=secure,
  419. headers=headers,
  420. query_params=query_params,
  421. **extra,
  422. )
  423. def post(
  424. self,
  425. path,
  426. data=None,
  427. content_type=MULTIPART_CONTENT,
  428. secure=False,
  429. *,
  430. headers=None,
  431. query_params=None,
  432. **extra,
  433. ):
  434. """Construct a POST request."""
  435. data = self._encode_json({} if data is None else data, content_type)
  436. post_data = self._encode_data(data, content_type)
  437. return self.generic(
  438. "POST",
  439. path,
  440. post_data,
  441. content_type,
  442. secure=secure,
  443. headers=headers,
  444. query_params=query_params,
  445. **extra,
  446. )
  447. def head(
  448. self, path, data=None, secure=False, *, headers=None, query_params=None, **extra
  449. ):
  450. """Construct a HEAD request."""
  451. if query_params and data:
  452. raise ValueError("query_params and data arguments are mutually exclusive.")
  453. query_params = data or query_params
  454. query_params = {} if query_params is None else query_params
  455. return self.generic(
  456. "HEAD",
  457. path,
  458. secure=secure,
  459. headers=headers,
  460. query_params=query_params,
  461. **extra,
  462. )
  463. def trace(self, path, secure=False, *, headers=None, query_params=None, **extra):
  464. """Construct a TRACE request."""
  465. return self.generic(
  466. "TRACE",
  467. path,
  468. secure=secure,
  469. headers=headers,
  470. query_params=query_params,
  471. **extra,
  472. )
  473. def options(
  474. self,
  475. path,
  476. data="",
  477. content_type="application/octet-stream",
  478. secure=False,
  479. *,
  480. headers=None,
  481. query_params=None,
  482. **extra,
  483. ):
  484. "Construct an OPTIONS request."
  485. return self.generic(
  486. "OPTIONS",
  487. path,
  488. data,
  489. content_type,
  490. secure=secure,
  491. headers=headers,
  492. query_params=query_params,
  493. **extra,
  494. )
  495. def put(
  496. self,
  497. path,
  498. data="",
  499. content_type="application/octet-stream",
  500. secure=False,
  501. *,
  502. headers=None,
  503. query_params=None,
  504. **extra,
  505. ):
  506. """Construct a PUT request."""
  507. data = self._encode_json(data, content_type)
  508. return self.generic(
  509. "PUT",
  510. path,
  511. data,
  512. content_type,
  513. secure=secure,
  514. headers=headers,
  515. query_params=query_params,
  516. **extra,
  517. )
  518. def patch(
  519. self,
  520. path,
  521. data="",
  522. content_type="application/octet-stream",
  523. secure=False,
  524. *,
  525. headers=None,
  526. query_params=None,
  527. **extra,
  528. ):
  529. """Construct a PATCH request."""
  530. data = self._encode_json(data, content_type)
  531. return self.generic(
  532. "PATCH",
  533. path,
  534. data,
  535. content_type,
  536. secure=secure,
  537. headers=headers,
  538. query_params=query_params,
  539. **extra,
  540. )
  541. def delete(
  542. self,
  543. path,
  544. data="",
  545. content_type="application/octet-stream",
  546. secure=False,
  547. *,
  548. headers=None,
  549. query_params=None,
  550. **extra,
  551. ):
  552. """Construct a DELETE request."""
  553. data = self._encode_json(data, content_type)
  554. return self.generic(
  555. "DELETE",
  556. path,
  557. data,
  558. content_type,
  559. secure=secure,
  560. headers=headers,
  561. query_params=query_params,
  562. **extra,
  563. )
  564. def generic(
  565. self,
  566. method,
  567. path,
  568. data="",
  569. content_type="application/octet-stream",
  570. secure=False,
  571. *,
  572. headers=None,
  573. query_params=None,
  574. **extra,
  575. ):
  576. """Construct an arbitrary HTTP request."""
  577. parsed = urlparse(str(path)) # path can be lazy
  578. data = force_bytes(data, settings.DEFAULT_CHARSET)
  579. r = {
  580. "PATH_INFO": self._get_path(parsed),
  581. "REQUEST_METHOD": method,
  582. "SERVER_PORT": "443" if secure else "80",
  583. "wsgi.url_scheme": "https" if secure else "http",
  584. }
  585. if data:
  586. r.update(
  587. {
  588. "CONTENT_LENGTH": str(len(data)),
  589. "CONTENT_TYPE": content_type,
  590. "wsgi.input": FakePayload(data),
  591. }
  592. )
  593. if headers:
  594. extra.update(HttpHeaders.to_wsgi_names(headers))
  595. if query_params:
  596. extra["QUERY_STRING"] = urlencode(query_params, doseq=True)
  597. r.update(extra)
  598. # If QUERY_STRING is absent or empty, we want to extract it from the URL.
  599. if not r.get("QUERY_STRING"):
  600. # WSGI requires latin-1 encoded strings. See get_path_info().
  601. query_string = parsed[4].encode().decode("iso-8859-1")
  602. r["QUERY_STRING"] = query_string
  603. return self.request(**r)
  604. class AsyncRequestFactory(RequestFactory):
  605. """
  606. Class that lets you create mock ASGI-like Request objects for use in
  607. testing. Usage:
  608. rf = AsyncRequestFactory()
  609. get_request = rf.get("/hello/")
  610. post_request = rf.post("/submit/", {"foo": "bar"})
  611. Once you have a request object you can pass it to any view function,
  612. including synchronous ones. The reason we have a separate class here is:
  613. a) this makes ASGIRequest subclasses, and
  614. b) AsyncTestClient can subclass it.
  615. """
  616. def _base_scope(self, **request):
  617. """The base scope for a request."""
  618. # This is a minimal valid ASGI scope, plus:
  619. # - headers['cookie'] for cookie support,
  620. # - 'client' often useful, see #8551.
  621. scope = {
  622. "asgi": {"version": "3.0"},
  623. "type": "http",
  624. "http_version": "1.1",
  625. "client": ["127.0.0.1", 0],
  626. "server": ("testserver", "80"),
  627. "scheme": "http",
  628. "method": "GET",
  629. "headers": [],
  630. **self.defaults,
  631. **request,
  632. }
  633. scope["headers"].append(
  634. (
  635. b"cookie",
  636. b"; ".join(
  637. sorted(
  638. ("%s=%s" % (morsel.key, morsel.coded_value)).encode("ascii")
  639. for morsel in self.cookies.values()
  640. )
  641. ),
  642. )
  643. )
  644. return scope
  645. def request(self, **request):
  646. """Construct a generic request object."""
  647. # This is synchronous, which means all methods on this class are.
  648. # AsyncClient, however, has an async request function, which makes all
  649. # its methods async.
  650. if "_body_file" in request:
  651. body_file = request.pop("_body_file")
  652. else:
  653. body_file = FakePayload("")
  654. # Wrap FakePayload body_file to allow large read() in test environment.
  655. return ASGIRequest(
  656. self._base_scope(**request), LimitedStream(body_file, len(body_file))
  657. )
  658. def generic(
  659. self,
  660. method,
  661. path,
  662. data="",
  663. content_type="application/octet-stream",
  664. secure=False,
  665. *,
  666. headers=None,
  667. query_params=None,
  668. **extra,
  669. ):
  670. """Construct an arbitrary HTTP request."""
  671. parsed = urlparse(str(path)) # path can be lazy.
  672. data = force_bytes(data, settings.DEFAULT_CHARSET)
  673. s = {
  674. "method": method,
  675. "path": self._get_path(parsed),
  676. "server": ("127.0.0.1", "443" if secure else "80"),
  677. "scheme": "https" if secure else "http",
  678. "headers": [(b"host", b"testserver")],
  679. }
  680. if data:
  681. s["headers"].extend(
  682. [
  683. (b"content-length", str(len(data)).encode("ascii")),
  684. (b"content-type", content_type.encode("ascii")),
  685. ]
  686. )
  687. s["_body_file"] = FakePayload(data)
  688. if query_params:
  689. s["query_string"] = urlencode(query_params, doseq=True)
  690. elif query_string := extra.pop("QUERY_STRING", None):
  691. s["query_string"] = query_string
  692. else:
  693. # If QUERY_STRING is absent or empty, we want to extract it from
  694. # the URL.
  695. s["query_string"] = parsed[4]
  696. if headers:
  697. extra.update(HttpHeaders.to_asgi_names(headers))
  698. s["headers"] += [
  699. (key.lower().encode("ascii"), value.encode("latin1"))
  700. for key, value in extra.items()
  701. ]
  702. return self.request(**s)
  703. class ClientMixin:
  704. """
  705. Mixin with common methods between Client and AsyncClient.
  706. """
  707. def store_exc_info(self, **kwargs):
  708. """Store exceptions when they are generated by a view."""
  709. self.exc_info = sys.exc_info()
  710. def check_exception(self, response):
  711. """
  712. Look for a signaled exception, clear the current context exception
  713. data, re-raise the signaled exception, and clear the signaled exception
  714. from the local cache.
  715. """
  716. response.exc_info = self.exc_info
  717. if self.exc_info:
  718. _, exc_value, _ = self.exc_info
  719. self.exc_info = None
  720. if self.raise_request_exception:
  721. raise exc_value
  722. @property
  723. def session(self):
  724. """Return the current session variables."""
  725. engine = import_module(settings.SESSION_ENGINE)
  726. cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
  727. if cookie:
  728. return engine.SessionStore(cookie.value)
  729. session = engine.SessionStore()
  730. session.save()
  731. self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
  732. return session
  733. async def asession(self):
  734. return await sync_to_async(lambda: self.session)()
  735. def login(self, **credentials):
  736. """
  737. Set the Factory to appear as if it has successfully logged into a site.
  738. Return True if login is possible or False if the provided credentials
  739. are incorrect.
  740. """
  741. from django.contrib.auth import authenticate
  742. user = authenticate(**credentials)
  743. if user:
  744. self._login(user)
  745. return True
  746. return False
  747. async def alogin(self, **credentials):
  748. """See login()."""
  749. from django.contrib.auth import aauthenticate
  750. user = await aauthenticate(**credentials)
  751. if user:
  752. await self._alogin(user)
  753. return True
  754. return False
  755. def force_login(self, user, backend=None):
  756. if backend is None:
  757. backend = self._get_backend()
  758. user.backend = backend
  759. self._login(user, backend)
  760. async def aforce_login(self, user, backend=None):
  761. if backend is None:
  762. backend = self._get_backend()
  763. user.backend = backend
  764. await self._alogin(user, backend)
  765. def _get_backend(self):
  766. from django.contrib.auth import load_backend
  767. for backend_path in settings.AUTHENTICATION_BACKENDS:
  768. backend = load_backend(backend_path)
  769. if hasattr(backend, "get_user"):
  770. return backend_path
  771. def _login(self, user, backend=None):
  772. from django.contrib.auth import login
  773. # Create a fake request to store login details.
  774. request = HttpRequest()
  775. if self.session:
  776. request.session = self.session
  777. else:
  778. engine = import_module(settings.SESSION_ENGINE)
  779. request.session = engine.SessionStore()
  780. login(request, user, backend)
  781. # Save the session values.
  782. request.session.save()
  783. self._set_login_cookies(request)
  784. async def _alogin(self, user, backend=None):
  785. from django.contrib.auth import alogin
  786. # Create a fake request to store login details.
  787. request = HttpRequest()
  788. session = await self.asession()
  789. if session:
  790. request.session = session
  791. else:
  792. engine = import_module(settings.SESSION_ENGINE)
  793. request.session = engine.SessionStore()
  794. await alogin(request, user, backend)
  795. # Save the session values.
  796. await sync_to_async(request.session.save)()
  797. self._set_login_cookies(request)
  798. def _set_login_cookies(self, request):
  799. # Set the cookie to represent the session.
  800. session_cookie = settings.SESSION_COOKIE_NAME
  801. self.cookies[session_cookie] = request.session.session_key
  802. cookie_data = {
  803. "max-age": None,
  804. "path": "/",
  805. "domain": settings.SESSION_COOKIE_DOMAIN,
  806. "secure": settings.SESSION_COOKIE_SECURE or None,
  807. "expires": None,
  808. }
  809. self.cookies[session_cookie].update(cookie_data)
  810. def logout(self):
  811. """Log out the user by removing the cookies and session object."""
  812. from django.contrib.auth import get_user, logout
  813. request = HttpRequest()
  814. if self.session:
  815. request.session = self.session
  816. request.user = get_user(request)
  817. else:
  818. engine = import_module(settings.SESSION_ENGINE)
  819. request.session = engine.SessionStore()
  820. logout(request)
  821. self.cookies = SimpleCookie()
  822. async def alogout(self):
  823. """See logout()."""
  824. from django.contrib.auth import aget_user, alogout
  825. request = HttpRequest()
  826. session = await self.asession()
  827. if session:
  828. request.session = session
  829. request.user = await aget_user(request)
  830. else:
  831. engine = import_module(settings.SESSION_ENGINE)
  832. request.session = engine.SessionStore()
  833. await alogout(request)
  834. self.cookies = SimpleCookie()
  835. def _parse_json(self, response, **extra):
  836. if not hasattr(response, "_json"):
  837. if not JSON_CONTENT_TYPE_RE.match(response.get("Content-Type")):
  838. raise ValueError(
  839. 'Content-Type header is "%s", not "application/json"'
  840. % response.get("Content-Type")
  841. )
  842. response._json = json.loads(
  843. response.content.decode(response.charset), **extra
  844. )
  845. return response._json
  846. def _follow_redirect(
  847. self,
  848. response,
  849. *,
  850. data="",
  851. content_type="",
  852. headers=None,
  853. query_params=None,
  854. **extra,
  855. ):
  856. """Follow a single redirect contained in response using GET."""
  857. response_url = response.url
  858. redirect_chain = response.redirect_chain
  859. redirect_chain.append((response_url, response.status_code))
  860. url = urlsplit(response_url)
  861. if url.scheme:
  862. extra["wsgi.url_scheme"] = url.scheme
  863. if url.hostname:
  864. extra["SERVER_NAME"] = url.hostname
  865. extra["HTTP_HOST"] = url.hostname
  866. if url.port:
  867. extra["SERVER_PORT"] = str(url.port)
  868. path = url.path
  869. # RFC 3986 Section 6.2.3: Empty path should be normalized to "/".
  870. if not path and url.netloc:
  871. path = "/"
  872. # Prepend the request path to handle relative path redirects
  873. if not path.startswith("/"):
  874. path = urljoin(response.request["PATH_INFO"], path)
  875. if response.status_code in (
  876. HTTPStatus.TEMPORARY_REDIRECT,
  877. HTTPStatus.PERMANENT_REDIRECT,
  878. ):
  879. # Preserve request method and query string (if needed)
  880. # post-redirect for 307/308 responses.
  881. request_method = response.request["REQUEST_METHOD"].lower()
  882. if request_method not in ("get", "head"):
  883. extra["QUERY_STRING"] = url.query
  884. request_method = getattr(self, request_method)
  885. else:
  886. request_method = self.get
  887. data = QueryDict(url.query)
  888. content_type = None
  889. return request_method(
  890. path,
  891. data=data,
  892. content_type=content_type,
  893. follow=False,
  894. headers=headers,
  895. query_params=query_params,
  896. **extra,
  897. )
  898. def _ensure_redirects_not_cyclic(self, response):
  899. """
  900. Raise a RedirectCycleError if response contains too many redirects.
  901. """
  902. redirect_chain = response.redirect_chain
  903. if redirect_chain[-1] in redirect_chain[:-1]:
  904. # Check that we're not redirecting to somewhere we've already been
  905. # to, to prevent loops.
  906. raise RedirectCycleError("Redirect loop detected.", last_response=response)
  907. if len(redirect_chain) > 20:
  908. # Such a lengthy chain likely also means a loop, but one with a
  909. # growing path, changing view, or changing query argument. 20 is
  910. # the value of "network.http.redirection-limit" from Firefox.
  911. raise RedirectCycleError("Too many redirects.", last_response=response)
  912. class Client(ClientMixin, RequestFactory):
  913. """
  914. A class that can act as a client for testing purposes.
  915. It allows the user to compose GET and POST requests, and
  916. obtain the response that the server gave to those requests.
  917. The server Response objects are annotated with the details
  918. of the contexts and templates that were rendered during the
  919. process of serving the request.
  920. Client objects are stateful - they will retain cookie (and
  921. thus session) details for the lifetime of the Client instance.
  922. This is not intended as a replacement for Twill/Selenium or
  923. the like - it is here to allow testing against the
  924. contexts and templates produced by a view, rather than the
  925. HTML rendered to the end-user.
  926. """
  927. def __init__(
  928. self,
  929. enforce_csrf_checks=False,
  930. raise_request_exception=True,
  931. *,
  932. headers=None,
  933. query_params=None,
  934. **defaults,
  935. ):
  936. super().__init__(headers=headers, query_params=query_params, **defaults)
  937. self.handler = ClientHandler(enforce_csrf_checks)
  938. self.raise_request_exception = raise_request_exception
  939. self.exc_info = None
  940. self.extra = None
  941. self.headers = None
  942. def request(self, **request):
  943. """
  944. Make a generic request. Compose the environment dictionary and pass
  945. to the handler, return the result of the handler. Assume defaults for
  946. the query environment, which can be overridden using the arguments to
  947. the request.
  948. """
  949. environ = self._base_environ(**request)
  950. # Curry a data dictionary into an instance of the template renderer
  951. # callback function.
  952. data = {}
  953. on_template_render = partial(store_rendered_templates, data)
  954. signal_uid = "template-render-%s" % id(request)
  955. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  956. # Capture exceptions created by the handler.
  957. exception_uid = "request-exception-%s" % id(request)
  958. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  959. try:
  960. response = self.handler(environ)
  961. finally:
  962. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  963. got_request_exception.disconnect(dispatch_uid=exception_uid)
  964. # Check for signaled exceptions.
  965. self.check_exception(response)
  966. # Save the client and request that stimulated the response.
  967. response.client = self
  968. response.request = request
  969. # Add any rendered template detail to the response.
  970. response.templates = data.get("templates", [])
  971. response.context = data.get("context")
  972. response.json = partial(self._parse_json, response)
  973. # Attach the ResolverMatch instance to the response.
  974. urlconf = getattr(response.wsgi_request, "urlconf", None)
  975. response.resolver_match = SimpleLazyObject(
  976. lambda: resolve(request["PATH_INFO"], urlconf=urlconf),
  977. )
  978. # Flatten a single context. Not really necessary anymore thanks to the
  979. # __getattr__ flattening in ContextList, but has some edge case
  980. # backwards compatibility implications.
  981. if response.context and len(response.context) == 1:
  982. response.context = response.context[0]
  983. # Update persistent cookie data.
  984. if response.cookies:
  985. self.cookies.update(response.cookies)
  986. return response
  987. def get(
  988. self,
  989. path,
  990. data=None,
  991. follow=False,
  992. secure=False,
  993. *,
  994. headers=None,
  995. query_params=None,
  996. **extra,
  997. ):
  998. """Request a response from the server using GET."""
  999. self.extra = extra
  1000. self.headers = headers
  1001. response = super().get(
  1002. path,
  1003. data=data,
  1004. secure=secure,
  1005. headers=headers,
  1006. query_params=query_params,
  1007. **extra,
  1008. )
  1009. if follow:
  1010. response = self._handle_redirects(
  1011. response, data=data, headers=headers, query_params=query_params, **extra
  1012. )
  1013. return response
  1014. def post(
  1015. self,
  1016. path,
  1017. data=None,
  1018. content_type=MULTIPART_CONTENT,
  1019. follow=False,
  1020. secure=False,
  1021. *,
  1022. headers=None,
  1023. query_params=None,
  1024. **extra,
  1025. ):
  1026. """Request a response from the server using POST."""
  1027. self.extra = extra
  1028. self.headers = headers
  1029. response = super().post(
  1030. path,
  1031. data=data,
  1032. content_type=content_type,
  1033. secure=secure,
  1034. headers=headers,
  1035. query_params=query_params,
  1036. **extra,
  1037. )
  1038. if follow:
  1039. response = self._handle_redirects(
  1040. response,
  1041. data=data,
  1042. content_type=content_type,
  1043. headers=headers,
  1044. query_params=query_params,
  1045. **extra,
  1046. )
  1047. return response
  1048. def head(
  1049. self,
  1050. path,
  1051. data=None,
  1052. follow=False,
  1053. secure=False,
  1054. *,
  1055. headers=None,
  1056. query_params=None,
  1057. **extra,
  1058. ):
  1059. """Request a response from the server using HEAD."""
  1060. self.extra = extra
  1061. self.headers = headers
  1062. response = super().head(
  1063. path,
  1064. data=data,
  1065. secure=secure,
  1066. headers=headers,
  1067. query_params=query_params,
  1068. **extra,
  1069. )
  1070. if follow:
  1071. response = self._handle_redirects(
  1072. response, data=data, headers=headers, query_params=query_params, **extra
  1073. )
  1074. return response
  1075. def options(
  1076. self,
  1077. path,
  1078. data="",
  1079. content_type="application/octet-stream",
  1080. follow=False,
  1081. secure=False,
  1082. *,
  1083. headers=None,
  1084. query_params=None,
  1085. **extra,
  1086. ):
  1087. """Request a response from the server using OPTIONS."""
  1088. self.extra = extra
  1089. self.headers = headers
  1090. response = super().options(
  1091. path,
  1092. data=data,
  1093. content_type=content_type,
  1094. secure=secure,
  1095. headers=headers,
  1096. query_params=query_params,
  1097. **extra,
  1098. )
  1099. if follow:
  1100. response = self._handle_redirects(
  1101. response,
  1102. data=data,
  1103. content_type=content_type,
  1104. headers=headers,
  1105. query_params=query_params,
  1106. **extra,
  1107. )
  1108. return response
  1109. def put(
  1110. self,
  1111. path,
  1112. data="",
  1113. content_type="application/octet-stream",
  1114. follow=False,
  1115. secure=False,
  1116. *,
  1117. headers=None,
  1118. query_params=None,
  1119. **extra,
  1120. ):
  1121. """Send a resource to the server using PUT."""
  1122. self.extra = extra
  1123. self.headers = headers
  1124. response = super().put(
  1125. path,
  1126. data=data,
  1127. content_type=content_type,
  1128. secure=secure,
  1129. headers=headers,
  1130. query_params=query_params,
  1131. **extra,
  1132. )
  1133. if follow:
  1134. response = self._handle_redirects(
  1135. response,
  1136. data=data,
  1137. content_type=content_type,
  1138. headers=headers,
  1139. query_params=query_params,
  1140. **extra,
  1141. )
  1142. return response
  1143. def patch(
  1144. self,
  1145. path,
  1146. data="",
  1147. content_type="application/octet-stream",
  1148. follow=False,
  1149. secure=False,
  1150. *,
  1151. headers=None,
  1152. query_params=None,
  1153. **extra,
  1154. ):
  1155. """Send a resource to the server using PATCH."""
  1156. self.extra = extra
  1157. self.headers = headers
  1158. response = super().patch(
  1159. path,
  1160. data=data,
  1161. content_type=content_type,
  1162. secure=secure,
  1163. headers=headers,
  1164. query_params=query_params,
  1165. **extra,
  1166. )
  1167. if follow:
  1168. response = self._handle_redirects(
  1169. response,
  1170. data=data,
  1171. content_type=content_type,
  1172. headers=headers,
  1173. query_params=query_params,
  1174. **extra,
  1175. )
  1176. return response
  1177. def delete(
  1178. self,
  1179. path,
  1180. data="",
  1181. content_type="application/octet-stream",
  1182. follow=False,
  1183. secure=False,
  1184. *,
  1185. headers=None,
  1186. query_params=None,
  1187. **extra,
  1188. ):
  1189. """Send a DELETE request to the server."""
  1190. self.extra = extra
  1191. self.headers = headers
  1192. response = super().delete(
  1193. path,
  1194. data=data,
  1195. content_type=content_type,
  1196. secure=secure,
  1197. headers=headers,
  1198. query_params=query_params,
  1199. **extra,
  1200. )
  1201. if follow:
  1202. response = self._handle_redirects(
  1203. response,
  1204. data=data,
  1205. content_type=content_type,
  1206. headers=headers,
  1207. query_params=query_params,
  1208. **extra,
  1209. )
  1210. return response
  1211. def trace(
  1212. self,
  1213. path,
  1214. data="",
  1215. follow=False,
  1216. secure=False,
  1217. *,
  1218. headers=None,
  1219. query_params=None,
  1220. **extra,
  1221. ):
  1222. """Send a TRACE request to the server."""
  1223. self.extra = extra
  1224. self.headers = headers
  1225. response = super().trace(
  1226. path,
  1227. data=data,
  1228. secure=secure,
  1229. headers=headers,
  1230. query_params=query_params,
  1231. **extra,
  1232. )
  1233. if follow:
  1234. response = self._handle_redirects(
  1235. response, data=data, headers=headers, query_params=query_params, **extra
  1236. )
  1237. return response
  1238. def _handle_redirects(
  1239. self,
  1240. response,
  1241. data="",
  1242. content_type="",
  1243. headers=None,
  1244. query_params=None,
  1245. **extra,
  1246. ):
  1247. """
  1248. Follow any redirects by requesting responses from the server using GET.
  1249. """
  1250. response.redirect_chain = []
  1251. while response.status_code in REDIRECT_STATUS_CODES:
  1252. redirect_chain = response.redirect_chain
  1253. response = self._follow_redirect(
  1254. response,
  1255. data=data,
  1256. content_type=content_type,
  1257. headers=headers,
  1258. query_params=query_params,
  1259. **extra,
  1260. )
  1261. response.redirect_chain = redirect_chain
  1262. self._ensure_redirects_not_cyclic(response)
  1263. return response
  1264. class AsyncClient(ClientMixin, AsyncRequestFactory):
  1265. """
  1266. An async version of Client that creates ASGIRequests and calls through an
  1267. async request path.
  1268. Does not currently support "follow" on its methods.
  1269. """
  1270. def __init__(
  1271. self,
  1272. enforce_csrf_checks=False,
  1273. raise_request_exception=True,
  1274. *,
  1275. headers=None,
  1276. query_params=None,
  1277. **defaults,
  1278. ):
  1279. super().__init__(headers=headers, query_params=query_params, **defaults)
  1280. self.handler = AsyncClientHandler(enforce_csrf_checks)
  1281. self.raise_request_exception = raise_request_exception
  1282. self.exc_info = None
  1283. self.extra = None
  1284. self.headers = None
  1285. async def request(self, **request):
  1286. """
  1287. Make a generic request. Compose the scope dictionary and pass to the
  1288. handler, return the result of the handler. Assume defaults for the
  1289. query environment, which can be overridden using the arguments to the
  1290. request.
  1291. """
  1292. scope = self._base_scope(**request)
  1293. # Curry a data dictionary into an instance of the template renderer
  1294. # callback function.
  1295. data = {}
  1296. on_template_render = partial(store_rendered_templates, data)
  1297. signal_uid = "template-render-%s" % id(request)
  1298. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  1299. # Capture exceptions created by the handler.
  1300. exception_uid = "request-exception-%s" % id(request)
  1301. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  1302. try:
  1303. response = await self.handler(scope)
  1304. finally:
  1305. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  1306. got_request_exception.disconnect(dispatch_uid=exception_uid)
  1307. # Check for signaled exceptions.
  1308. self.check_exception(response)
  1309. # Save the client and request that stimulated the response.
  1310. response.client = self
  1311. response.request = request
  1312. # Add any rendered template detail to the response.
  1313. response.templates = data.get("templates", [])
  1314. response.context = data.get("context")
  1315. response.json = partial(self._parse_json, response)
  1316. # Attach the ResolverMatch instance to the response.
  1317. urlconf = getattr(response.asgi_request, "urlconf", None)
  1318. response.resolver_match = SimpleLazyObject(
  1319. lambda: resolve(request["path"], urlconf=urlconf),
  1320. )
  1321. # Flatten a single context. Not really necessary anymore thanks to the
  1322. # __getattr__ flattening in ContextList, but has some edge case
  1323. # backwards compatibility implications.
  1324. if response.context and len(response.context) == 1:
  1325. response.context = response.context[0]
  1326. # Update persistent cookie data.
  1327. if response.cookies:
  1328. self.cookies.update(response.cookies)
  1329. return response
  1330. async def get(
  1331. self,
  1332. path,
  1333. data=None,
  1334. follow=False,
  1335. secure=False,
  1336. *,
  1337. headers=None,
  1338. query_params=None,
  1339. **extra,
  1340. ):
  1341. """Request a response from the server using GET."""
  1342. self.extra = extra
  1343. self.headers = headers
  1344. response = await super().get(
  1345. path,
  1346. data=data,
  1347. secure=secure,
  1348. headers=headers,
  1349. query_params=query_params,
  1350. **extra,
  1351. )
  1352. if follow:
  1353. response = await self._ahandle_redirects(
  1354. response, data=data, headers=headers, query_params=query_params, **extra
  1355. )
  1356. return response
  1357. async def post(
  1358. self,
  1359. path,
  1360. data=None,
  1361. content_type=MULTIPART_CONTENT,
  1362. follow=False,
  1363. secure=False,
  1364. *,
  1365. headers=None,
  1366. query_params=None,
  1367. **extra,
  1368. ):
  1369. """Request a response from the server using POST."""
  1370. self.extra = extra
  1371. self.headers = headers
  1372. response = await super().post(
  1373. path,
  1374. data=data,
  1375. content_type=content_type,
  1376. secure=secure,
  1377. headers=headers,
  1378. query_params=query_params,
  1379. **extra,
  1380. )
  1381. if follow:
  1382. response = await self._ahandle_redirects(
  1383. response,
  1384. data=data,
  1385. content_type=content_type,
  1386. headers=headers,
  1387. query_params=query_params,
  1388. **extra,
  1389. )
  1390. return response
  1391. async def head(
  1392. self,
  1393. path,
  1394. data=None,
  1395. follow=False,
  1396. secure=False,
  1397. *,
  1398. headers=None,
  1399. query_params=None,
  1400. **extra,
  1401. ):
  1402. """Request a response from the server using HEAD."""
  1403. self.extra = extra
  1404. self.headers = headers
  1405. response = await super().head(
  1406. path,
  1407. data=data,
  1408. secure=secure,
  1409. headers=headers,
  1410. query_params=query_params,
  1411. **extra,
  1412. )
  1413. if follow:
  1414. response = await self._ahandle_redirects(
  1415. response, data=data, headers=headers, query_params=query_params, **extra
  1416. )
  1417. return response
  1418. async def options(
  1419. self,
  1420. path,
  1421. data="",
  1422. content_type="application/octet-stream",
  1423. follow=False,
  1424. secure=False,
  1425. *,
  1426. headers=None,
  1427. query_params=None,
  1428. **extra,
  1429. ):
  1430. """Request a response from the server using OPTIONS."""
  1431. self.extra = extra
  1432. self.headers = headers
  1433. response = await super().options(
  1434. path,
  1435. data=data,
  1436. content_type=content_type,
  1437. secure=secure,
  1438. headers=headers,
  1439. query_params=query_params,
  1440. **extra,
  1441. )
  1442. if follow:
  1443. response = await self._ahandle_redirects(
  1444. response,
  1445. data=data,
  1446. content_type=content_type,
  1447. headers=headers,
  1448. query_params=query_params,
  1449. **extra,
  1450. )
  1451. return response
  1452. async def put(
  1453. self,
  1454. path,
  1455. data="",
  1456. content_type="application/octet-stream",
  1457. follow=False,
  1458. secure=False,
  1459. *,
  1460. headers=None,
  1461. query_params=None,
  1462. **extra,
  1463. ):
  1464. """Send a resource to the server using PUT."""
  1465. self.extra = extra
  1466. self.headers = headers
  1467. response = await super().put(
  1468. path,
  1469. data=data,
  1470. content_type=content_type,
  1471. secure=secure,
  1472. headers=headers,
  1473. query_params=query_params,
  1474. **extra,
  1475. )
  1476. if follow:
  1477. response = await self._ahandle_redirects(
  1478. response,
  1479. data=data,
  1480. content_type=content_type,
  1481. headers=headers,
  1482. query_params=query_params,
  1483. **extra,
  1484. )
  1485. return response
  1486. async def patch(
  1487. self,
  1488. path,
  1489. data="",
  1490. content_type="application/octet-stream",
  1491. follow=False,
  1492. secure=False,
  1493. *,
  1494. headers=None,
  1495. query_params=None,
  1496. **extra,
  1497. ):
  1498. """Send a resource to the server using PATCH."""
  1499. self.extra = extra
  1500. self.headers = headers
  1501. response = await super().patch(
  1502. path,
  1503. data=data,
  1504. content_type=content_type,
  1505. secure=secure,
  1506. headers=headers,
  1507. query_params=query_params,
  1508. **extra,
  1509. )
  1510. if follow:
  1511. response = await self._ahandle_redirects(
  1512. response,
  1513. data=data,
  1514. content_type=content_type,
  1515. headers=headers,
  1516. query_params=query_params,
  1517. **extra,
  1518. )
  1519. return response
  1520. async def delete(
  1521. self,
  1522. path,
  1523. data="",
  1524. content_type="application/octet-stream",
  1525. follow=False,
  1526. secure=False,
  1527. *,
  1528. headers=None,
  1529. query_params=None,
  1530. **extra,
  1531. ):
  1532. """Send a DELETE request to the server."""
  1533. self.extra = extra
  1534. self.headers = headers
  1535. response = await super().delete(
  1536. path,
  1537. data=data,
  1538. content_type=content_type,
  1539. secure=secure,
  1540. headers=headers,
  1541. query_params=query_params,
  1542. **extra,
  1543. )
  1544. if follow:
  1545. response = await self._ahandle_redirects(
  1546. response,
  1547. data=data,
  1548. content_type=content_type,
  1549. headers=headers,
  1550. query_params=query_params,
  1551. **extra,
  1552. )
  1553. return response
  1554. async def trace(
  1555. self,
  1556. path,
  1557. data="",
  1558. follow=False,
  1559. secure=False,
  1560. *,
  1561. headers=None,
  1562. query_params=None,
  1563. **extra,
  1564. ):
  1565. """Send a TRACE request to the server."""
  1566. self.extra = extra
  1567. self.headers = headers
  1568. response = await super().trace(
  1569. path,
  1570. data=data,
  1571. secure=secure,
  1572. headers=headers,
  1573. query_params=query_params,
  1574. **extra,
  1575. )
  1576. if follow:
  1577. response = await self._ahandle_redirects(
  1578. response, data=data, headers=headers, query_params=query_params, **extra
  1579. )
  1580. return response
  1581. async def _ahandle_redirects(
  1582. self,
  1583. response,
  1584. data="",
  1585. content_type="",
  1586. headers=None,
  1587. query_params=None,
  1588. **extra,
  1589. ):
  1590. """
  1591. Follow any redirects by requesting responses from the server using GET.
  1592. """
  1593. response.redirect_chain = []
  1594. while response.status_code in REDIRECT_STATUS_CODES:
  1595. redirect_chain = response.redirect_chain
  1596. response = await self._follow_redirect(
  1597. response,
  1598. data=data,
  1599. content_type=content_type,
  1600. headers=headers,
  1601. query_params=query_params,
  1602. **extra,
  1603. )
  1604. response.redirect_chain = redirect_chain
  1605. self._ensure_redirects_not_cyclic(response)
  1606. return response