Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. import json
  2. import mimetypes
  3. import os
  4. import re
  5. import sys
  6. from copy import copy
  7. from functools import partial
  8. from importlib import import_module
  9. from io import BytesIO
  10. from urllib.parse import unquote_to_bytes, urljoin, urlparse, urlsplit
  11. from django.conf import settings
  12. from django.core.handlers.base import BaseHandler
  13. from django.core.handlers.wsgi import WSGIRequest
  14. from django.core.signals import (
  15. got_request_exception, request_finished, request_started,
  16. )
  17. from django.db import close_old_connections
  18. from django.http import HttpRequest, QueryDict, SimpleCookie
  19. from django.template import TemplateDoesNotExist
  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. __all__ = ('Client', 'RedirectCycleError', 'RequestFactory', 'encode_file', 'encode_multipart')
  28. BOUNDARY = 'BoUnDaRyStRiNg'
  29. MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
  30. CONTENT_TYPE_RE = re.compile(r'.*; charset=([\w\d-]+);?')
  31. # JSON Vendor Tree spec: https://tools.ietf.org/html/rfc6838#section-3.2
  32. JSON_CONTENT_TYPE_RE = re.compile(r'^application\/(vnd\..+\+)?json')
  33. class RedirectCycleError(Exception):
  34. """The test client has been asked to follow a redirect loop."""
  35. def __init__(self, message, last_response):
  36. super().__init__(message)
  37. self.last_response = last_response
  38. self.redirect_chain = last_response.redirect_chain
  39. class FakePayload:
  40. """
  41. A wrapper around BytesIO that restricts what can be read since data from
  42. the network can't be seeked and cannot be read outside of its content
  43. length. This makes sure that views can't do anything under the test client
  44. that wouldn't work in real life.
  45. """
  46. def __init__(self, content=None):
  47. self.__content = BytesIO()
  48. self.__len = 0
  49. self.read_started = False
  50. if content is not None:
  51. self.write(content)
  52. def __len__(self):
  53. return self.__len
  54. def read(self, num_bytes=None):
  55. if not self.read_started:
  56. self.__content.seek(0)
  57. self.read_started = True
  58. if num_bytes is None:
  59. num_bytes = self.__len or 0
  60. assert self.__len >= num_bytes, "Cannot read more than the available bytes from the HTTP incoming data."
  61. content = self.__content.read(num_bytes)
  62. self.__len -= num_bytes
  63. return content
  64. def write(self, content):
  65. if self.read_started:
  66. raise ValueError("Unable to write a payload after he's been read")
  67. content = force_bytes(content)
  68. self.__content.write(content)
  69. self.__len += len(content)
  70. def closing_iterator_wrapper(iterable, close):
  71. try:
  72. yield from iterable
  73. finally:
  74. request_finished.disconnect(close_old_connections)
  75. close() # will fire request_finished
  76. request_finished.connect(close_old_connections)
  77. def conditional_content_removal(request, response):
  78. """
  79. Simulate the behavior of most Web servers by removing the content of
  80. responses for HEAD requests, 1xx, 204, and 304 responses. Ensure
  81. compliance with RFC 7230, section 3.3.3.
  82. """
  83. if 100 <= response.status_code < 200 or response.status_code in (204, 304):
  84. if response.streaming:
  85. response.streaming_content = []
  86. else:
  87. response.content = b''
  88. if request.method == 'HEAD':
  89. if response.streaming:
  90. response.streaming_content = []
  91. else:
  92. response.content = b''
  93. return response
  94. class ClientHandler(BaseHandler):
  95. """
  96. A HTTP Handler that can be used for testing purposes. Use the WSGI
  97. interface to compose requests, but return the raw HttpResponse object with
  98. the originating WSGIRequest attached to its ``wsgi_request`` attribute.
  99. """
  100. def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
  101. self.enforce_csrf_checks = enforce_csrf_checks
  102. super().__init__(*args, **kwargs)
  103. def __call__(self, environ):
  104. # Set up middleware if needed. We couldn't do this earlier, because
  105. # settings weren't available.
  106. if self._middleware_chain is None:
  107. self.load_middleware()
  108. request_started.disconnect(close_old_connections)
  109. request_started.send(sender=self.__class__, environ=environ)
  110. request_started.connect(close_old_connections)
  111. request = WSGIRequest(environ)
  112. # sneaky little hack so that we can easily get round
  113. # CsrfViewMiddleware. This makes life easier, and is probably
  114. # required for backwards compatibility with external tests against
  115. # admin views.
  116. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
  117. # Request goes through middleware.
  118. response = self.get_response(request)
  119. # Simulate behaviors of most Web servers.
  120. conditional_content_removal(request, response)
  121. # Attach the originating request to the response so that it could be
  122. # later retrieved.
  123. response.wsgi_request = request
  124. # Emulate a WSGI server by calling the close method on completion.
  125. if response.streaming:
  126. response.streaming_content = closing_iterator_wrapper(
  127. response.streaming_content, response.close)
  128. else:
  129. request_finished.disconnect(close_old_connections)
  130. response.close() # will fire request_finished
  131. request_finished.connect(close_old_connections)
  132. return response
  133. def store_rendered_templates(store, signal, sender, template, context, **kwargs):
  134. """
  135. Store templates and contexts that are rendered.
  136. The context is copied so that it is an accurate representation at the time
  137. of rendering.
  138. """
  139. store.setdefault('templates', []).append(template)
  140. if 'context' not in store:
  141. store['context'] = ContextList()
  142. store['context'].append(copy(context))
  143. def encode_multipart(boundary, data):
  144. """
  145. Encode multipart POST data from a dictionary of form values.
  146. The key will be used as the form data name; the value will be transmitted
  147. as content. If the value is a file, the contents of the file will be sent
  148. as an application/octet-stream; otherwise, str(value) will be sent.
  149. """
  150. lines = []
  151. def to_bytes(s):
  152. return force_bytes(s, settings.DEFAULT_CHARSET)
  153. # Not by any means perfect, but good enough for our purposes.
  154. def is_file(thing):
  155. return hasattr(thing, "read") and callable(thing.read)
  156. # Each bit of the multipart form data could be either a form value or a
  157. # file, or a *list* of form values and/or files. Remember that HTTP field
  158. # names can be duplicated!
  159. for (key, value) in data.items():
  160. if is_file(value):
  161. lines.extend(encode_file(boundary, key, value))
  162. elif not isinstance(value, str) and is_iterable(value):
  163. for item in value:
  164. if is_file(item):
  165. lines.extend(encode_file(boundary, key, item))
  166. else:
  167. lines.extend(to_bytes(val) for val in [
  168. '--%s' % boundary,
  169. 'Content-Disposition: form-data; name="%s"' % key,
  170. '',
  171. item
  172. ])
  173. else:
  174. lines.extend(to_bytes(val) for val in [
  175. '--%s' % boundary,
  176. 'Content-Disposition: form-data; name="%s"' % key,
  177. '',
  178. value
  179. ])
  180. lines.extend([
  181. to_bytes('--%s--' % boundary),
  182. b'',
  183. ])
  184. return b'\r\n'.join(lines)
  185. def encode_file(boundary, key, file):
  186. def to_bytes(s):
  187. return force_bytes(s, settings.DEFAULT_CHARSET)
  188. # file.name might not be a string. For example, it's an int for
  189. # tempfile.TemporaryFile().
  190. file_has_string_name = hasattr(file, 'name') and isinstance(file.name, str)
  191. filename = os.path.basename(file.name) if file_has_string_name else ''
  192. if hasattr(file, 'content_type'):
  193. content_type = file.content_type
  194. elif filename:
  195. content_type = mimetypes.guess_type(filename)[0]
  196. else:
  197. content_type = None
  198. if content_type is None:
  199. content_type = 'application/octet-stream'
  200. if not filename:
  201. filename = key
  202. return [
  203. to_bytes('--%s' % boundary),
  204. to_bytes('Content-Disposition: form-data; name="%s"; filename="%s"'
  205. % (key, filename)),
  206. to_bytes('Content-Type: %s' % content_type),
  207. b'',
  208. to_bytes(file.read())
  209. ]
  210. class RequestFactory:
  211. """
  212. Class that lets you create mock Request objects for use in testing.
  213. Usage:
  214. rf = RequestFactory()
  215. get_request = rf.get('/hello/')
  216. post_request = rf.post('/submit/', {'foo': 'bar'})
  217. Once you have a request object you can pass it to any view function,
  218. just as if that view had been hooked up using a URLconf.
  219. """
  220. def __init__(self, **defaults):
  221. self.defaults = defaults
  222. self.cookies = SimpleCookie()
  223. self.errors = BytesIO()
  224. def _base_environ(self, **request):
  225. """
  226. The base environment for a request.
  227. """
  228. # This is a minimal valid WSGI environ dictionary, plus:
  229. # - HTTP_COOKIE: for cookie support,
  230. # - REMOTE_ADDR: often useful, see #8551.
  231. # See http://www.python.org/dev/peps/pep-3333/#environ-variables
  232. environ = {
  233. 'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
  234. 'PATH_INFO': '/',
  235. 'REMOTE_ADDR': '127.0.0.1',
  236. 'REQUEST_METHOD': 'GET',
  237. 'SCRIPT_NAME': '',
  238. 'SERVER_NAME': 'testserver',
  239. 'SERVER_PORT': '80',
  240. 'SERVER_PROTOCOL': 'HTTP/1.1',
  241. 'wsgi.version': (1, 0),
  242. 'wsgi.url_scheme': 'http',
  243. 'wsgi.input': FakePayload(b''),
  244. 'wsgi.errors': self.errors,
  245. 'wsgi.multiprocess': True,
  246. 'wsgi.multithread': False,
  247. 'wsgi.run_once': False,
  248. }
  249. environ.update(self.defaults)
  250. environ.update(request)
  251. return environ
  252. def request(self, **request):
  253. "Construct a generic request object."
  254. return WSGIRequest(self._base_environ(**request))
  255. def _encode_data(self, data, content_type):
  256. if content_type is MULTIPART_CONTENT:
  257. return encode_multipart(BOUNDARY, data)
  258. else:
  259. # Encode the content so that the byte representation is correct.
  260. match = CONTENT_TYPE_RE.match(content_type)
  261. if match:
  262. charset = match.group(1)
  263. else:
  264. charset = settings.DEFAULT_CHARSET
  265. return force_bytes(data, encoding=charset)
  266. def _get_path(self, parsed):
  267. path = parsed.path
  268. # If there are parameters, add them
  269. if parsed.params:
  270. path += ";" + parsed.params
  271. path = unquote_to_bytes(path)
  272. # Replace the behavior where non-ASCII values in the WSGI environ are
  273. # arbitrarily decoded with ISO-8859-1.
  274. # Refs comment in `get_bytes_from_wsgi()`.
  275. return path.decode('iso-8859-1')
  276. def get(self, path, data=None, secure=False, **extra):
  277. """Construct a GET request."""
  278. data = {} if data is None else data
  279. r = {
  280. 'QUERY_STRING': urlencode(data, doseq=True),
  281. }
  282. r.update(extra)
  283. return self.generic('GET', path, secure=secure, **r)
  284. def post(self, path, data=None, content_type=MULTIPART_CONTENT,
  285. secure=False, **extra):
  286. """Construct a POST request."""
  287. data = {} if data is None else data
  288. post_data = self._encode_data(data, content_type)
  289. return self.generic('POST', path, post_data, content_type,
  290. secure=secure, **extra)
  291. def head(self, path, data=None, secure=False, **extra):
  292. """Construct a HEAD request."""
  293. data = {} if data is None else data
  294. r = {
  295. 'QUERY_STRING': urlencode(data, doseq=True),
  296. }
  297. r.update(extra)
  298. return self.generic('HEAD', path, secure=secure, **r)
  299. def trace(self, path, secure=False, **extra):
  300. """Construct a TRACE request."""
  301. return self.generic('TRACE', path, secure=secure, **extra)
  302. def options(self, path, data='', content_type='application/octet-stream',
  303. secure=False, **extra):
  304. "Construct an OPTIONS request."
  305. return self.generic('OPTIONS', path, data, content_type,
  306. secure=secure, **extra)
  307. def put(self, path, data='', content_type='application/octet-stream',
  308. secure=False, **extra):
  309. """Construct a PUT request."""
  310. return self.generic('PUT', path, data, content_type,
  311. secure=secure, **extra)
  312. def patch(self, path, data='', content_type='application/octet-stream',
  313. secure=False, **extra):
  314. """Construct a PATCH request."""
  315. return self.generic('PATCH', path, data, content_type,
  316. secure=secure, **extra)
  317. def delete(self, path, data='', content_type='application/octet-stream',
  318. secure=False, **extra):
  319. """Construct a DELETE request."""
  320. return self.generic('DELETE', path, data, content_type,
  321. secure=secure, **extra)
  322. def generic(self, method, path, data='',
  323. content_type='application/octet-stream', secure=False,
  324. **extra):
  325. """Construct an arbitrary HTTP request."""
  326. parsed = urlparse(str(path)) # path can be lazy
  327. data = force_bytes(data, settings.DEFAULT_CHARSET)
  328. r = {
  329. 'PATH_INFO': self._get_path(parsed),
  330. 'REQUEST_METHOD': method,
  331. 'SERVER_PORT': '443' if secure else '80',
  332. 'wsgi.url_scheme': 'https' if secure else 'http',
  333. }
  334. if data:
  335. r.update({
  336. 'CONTENT_LENGTH': len(data),
  337. 'CONTENT_TYPE': content_type,
  338. 'wsgi.input': FakePayload(data),
  339. })
  340. r.update(extra)
  341. # If QUERY_STRING is absent or empty, we want to extract it from the URL.
  342. if not r.get('QUERY_STRING'):
  343. # WSGI requires latin-1 encoded strings. See get_path_info().
  344. query_string = force_bytes(parsed[4]).decode('iso-8859-1')
  345. r['QUERY_STRING'] = query_string
  346. return self.request(**r)
  347. class Client(RequestFactory):
  348. """
  349. A class that can act as a client for testing purposes.
  350. It allows the user to compose GET and POST requests, and
  351. obtain the response that the server gave to those requests.
  352. The server Response objects are annotated with the details
  353. of the contexts and templates that were rendered during the
  354. process of serving the request.
  355. Client objects are stateful - they will retain cookie (and
  356. thus session) details for the lifetime of the Client instance.
  357. This is not intended as a replacement for Twill/Selenium or
  358. the like - it is here to allow testing against the
  359. contexts and templates produced by a view, rather than the
  360. HTML rendered to the end-user.
  361. """
  362. def __init__(self, enforce_csrf_checks=False, **defaults):
  363. super().__init__(**defaults)
  364. self.handler = ClientHandler(enforce_csrf_checks)
  365. self.exc_info = None
  366. def store_exc_info(self, **kwargs):
  367. """Store exceptions when they are generated by a view."""
  368. self.exc_info = sys.exc_info()
  369. @property
  370. def session(self):
  371. """Return the current session variables."""
  372. engine = import_module(settings.SESSION_ENGINE)
  373. cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
  374. if cookie:
  375. return engine.SessionStore(cookie.value)
  376. session = engine.SessionStore()
  377. session.save()
  378. self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
  379. return session
  380. def request(self, **request):
  381. """
  382. The master request method. Compose the environment dictionary and pass
  383. to the handler, return the result of the handler. Assume defaults for
  384. the query environment, which can be overridden using the arguments to
  385. the request.
  386. """
  387. environ = self._base_environ(**request)
  388. # Curry a data dictionary into an instance of the template renderer
  389. # callback function.
  390. data = {}
  391. on_template_render = partial(store_rendered_templates, data)
  392. signal_uid = "template-render-%s" % id(request)
  393. signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
  394. # Capture exceptions created by the handler.
  395. exception_uid = "request-exception-%s" % id(request)
  396. got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
  397. try:
  398. try:
  399. response = self.handler(environ)
  400. except TemplateDoesNotExist as e:
  401. # If the view raises an exception, Django will attempt to show
  402. # the 500.html template. If that template is not available,
  403. # we should ignore the error in favor of re-raising the
  404. # underlying exception that caused the 500 error. Any other
  405. # template found to be missing during view error handling
  406. # should be reported as-is.
  407. if e.args != ('500.html',):
  408. raise
  409. # Look for a signalled exception, clear the current context
  410. # exception data, then re-raise the signalled exception.
  411. # Also make sure that the signalled exception is cleared from
  412. # the local cache!
  413. if self.exc_info:
  414. _, exc_value, _ = self.exc_info
  415. self.exc_info = None
  416. raise exc_value
  417. # Save the client and request that stimulated the response.
  418. response.client = self
  419. response.request = request
  420. # Add any rendered template detail to the response.
  421. response.templates = data.get("templates", [])
  422. response.context = data.get("context")
  423. response.json = partial(self._parse_json, response)
  424. # Attach the ResolverMatch instance to the response
  425. response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO']))
  426. # Flatten a single context. Not really necessary anymore thanks to
  427. # the __getattr__ flattening in ContextList, but has some edge-case
  428. # backwards-compatibility implications.
  429. if response.context and len(response.context) == 1:
  430. response.context = response.context[0]
  431. # Update persistent cookie data.
  432. if response.cookies:
  433. self.cookies.update(response.cookies)
  434. return response
  435. finally:
  436. signals.template_rendered.disconnect(dispatch_uid=signal_uid)
  437. got_request_exception.disconnect(dispatch_uid=exception_uid)
  438. def get(self, path, data=None, follow=False, secure=False, **extra):
  439. """Request a response from the server using GET."""
  440. response = super().get(path, data=data, secure=secure, **extra)
  441. if follow:
  442. response = self._handle_redirects(response, **extra)
  443. return response
  444. def post(self, path, data=None, content_type=MULTIPART_CONTENT,
  445. follow=False, secure=False, **extra):
  446. """Request a response from the server using POST."""
  447. response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
  448. if follow:
  449. response = self._handle_redirects(response, **extra)
  450. return response
  451. def head(self, path, data=None, follow=False, secure=False, **extra):
  452. """Request a response from the server using HEAD."""
  453. response = super().head(path, data=data, secure=secure, **extra)
  454. if follow:
  455. response = self._handle_redirects(response, **extra)
  456. return response
  457. def options(self, path, data='', content_type='application/octet-stream',
  458. follow=False, secure=False, **extra):
  459. """Request a response from the server using OPTIONS."""
  460. response = super().options(path, data=data, content_type=content_type, secure=secure, **extra)
  461. if follow:
  462. response = self._handle_redirects(response, **extra)
  463. return response
  464. def put(self, path, data='', content_type='application/octet-stream',
  465. follow=False, secure=False, **extra):
  466. """Send a resource to the server using PUT."""
  467. response = super().put(path, data=data, content_type=content_type, secure=secure, **extra)
  468. if follow:
  469. response = self._handle_redirects(response, **extra)
  470. return response
  471. def patch(self, path, data='', content_type='application/octet-stream',
  472. follow=False, secure=False, **extra):
  473. """Send a resource to the server using PATCH."""
  474. response = super().patch(path, data=data, content_type=content_type, secure=secure, **extra)
  475. if follow:
  476. response = self._handle_redirects(response, **extra)
  477. return response
  478. def delete(self, path, data='', content_type='application/octet-stream',
  479. follow=False, secure=False, **extra):
  480. """Send a DELETE request to the server."""
  481. response = super().delete(path, data=data, content_type=content_type, secure=secure, **extra)
  482. if follow:
  483. response = self._handle_redirects(response, **extra)
  484. return response
  485. def trace(self, path, data='', follow=False, secure=False, **extra):
  486. """Send a TRACE request to the server."""
  487. response = super().trace(path, data=data, secure=secure, **extra)
  488. if follow:
  489. response = self._handle_redirects(response, **extra)
  490. return response
  491. def login(self, **credentials):
  492. """
  493. Set the Factory to appear as if it has successfully logged into a site.
  494. Return True if login is possible; False if the provided credentials
  495. are incorrect.
  496. """
  497. from django.contrib.auth import authenticate
  498. user = authenticate(**credentials)
  499. if user:
  500. self._login(user)
  501. return True
  502. else:
  503. return False
  504. def force_login(self, user, backend=None):
  505. def get_backend():
  506. from django.contrib.auth import load_backend
  507. for backend_path in settings.AUTHENTICATION_BACKENDS:
  508. backend = load_backend(backend_path)
  509. if hasattr(backend, 'get_user'):
  510. return backend_path
  511. if backend is None:
  512. backend = get_backend()
  513. user.backend = backend
  514. self._login(user, backend)
  515. def _login(self, user, backend=None):
  516. from django.contrib.auth import login
  517. engine = import_module(settings.SESSION_ENGINE)
  518. # Create a fake request to store login details.
  519. request = HttpRequest()
  520. if self.session:
  521. request.session = self.session
  522. else:
  523. request.session = engine.SessionStore()
  524. login(request, user, backend)
  525. # Save the session values.
  526. request.session.save()
  527. # Set the cookie to represent the session.
  528. session_cookie = settings.SESSION_COOKIE_NAME
  529. self.cookies[session_cookie] = request.session.session_key
  530. cookie_data = {
  531. 'max-age': None,
  532. 'path': '/',
  533. 'domain': settings.SESSION_COOKIE_DOMAIN,
  534. 'secure': settings.SESSION_COOKIE_SECURE or None,
  535. 'expires': None,
  536. }
  537. self.cookies[session_cookie].update(cookie_data)
  538. def logout(self):
  539. """Log out the user by removing the cookies and session object."""
  540. from django.contrib.auth import get_user, logout
  541. request = HttpRequest()
  542. engine = import_module(settings.SESSION_ENGINE)
  543. if self.session:
  544. request.session = self.session
  545. request.user = get_user(request)
  546. else:
  547. request.session = engine.SessionStore()
  548. logout(request)
  549. self.cookies = SimpleCookie()
  550. def _parse_json(self, response, **extra):
  551. if not hasattr(response, '_json'):
  552. if not JSON_CONTENT_TYPE_RE.match(response.get('Content-Type')):
  553. raise ValueError(
  554. 'Content-Type header is "{0}", not "application/json"'
  555. .format(response.get('Content-Type'))
  556. )
  557. response._json = json.loads(response.content.decode(), **extra)
  558. return response._json
  559. def _handle_redirects(self, response, **extra):
  560. """
  561. Follow any redirects by requesting responses from the server using GET.
  562. """
  563. response.redirect_chain = []
  564. while response.status_code in (301, 302, 303, 307):
  565. response_url = response.url
  566. redirect_chain = response.redirect_chain
  567. redirect_chain.append((response_url, response.status_code))
  568. url = urlsplit(response_url)
  569. if url.scheme:
  570. extra['wsgi.url_scheme'] = url.scheme
  571. if url.hostname:
  572. extra['SERVER_NAME'] = url.hostname
  573. if url.port:
  574. extra['SERVER_PORT'] = str(url.port)
  575. # Prepend the request path to handle relative path redirects
  576. path = url.path
  577. if not path.startswith('/'):
  578. path = urljoin(response.request['PATH_INFO'], path)
  579. response = self.get(path, QueryDict(url.query), follow=False, **extra)
  580. response.redirect_chain = redirect_chain
  581. if redirect_chain[-1] in redirect_chain[:-1]:
  582. # Check that we're not redirecting to somewhere we've already
  583. # been to, to prevent loops.
  584. raise RedirectCycleError("Redirect loop detected.", last_response=response)
  585. if len(redirect_chain) > 20:
  586. # Such a lengthy chain likely also means a loop, but one with
  587. # a growing path, changing view, or changing query argument;
  588. # 20 is the value of "network.http.redirection-limit" from Firefox.
  589. raise RedirectCycleError("Too many redirects.", last_response=response)
  590. return response