Construct an arbitrary HTTP request.
(
self,
method,
path,
data="",
content_type="application/octet-stream",
secure=False,
*,
headers=None,
query_params=None,
**extra,
)
| 631 | ) |
| 632 | |
| 633 | def generic( |
| 634 | self, |
| 635 | method, |
| 636 | path, |
| 637 | data="", |
| 638 | content_type="application/octet-stream", |
| 639 | secure=False, |
| 640 | *, |
| 641 | headers=None, |
| 642 | query_params=None, |
| 643 | **extra, |
| 644 | ): |
| 645 | """Construct an arbitrary HTTP request.""" |
| 646 | parsed = urlsplit(str(path)) # path can be lazy |
| 647 | data = force_bytes(data, settings.DEFAULT_CHARSET) |
| 648 | r = { |
| 649 | "PATH_INFO": self._get_path(parsed), |
| 650 | "REQUEST_METHOD": method, |
| 651 | "SERVER_PORT": "443" if secure else "80", |
| 652 | "wsgi.url_scheme": "https" if secure else "http", |
| 653 | } |
| 654 | if data: |
| 655 | r.update( |
| 656 | { |
| 657 | "CONTENT_LENGTH": str(len(data)), |
| 658 | "CONTENT_TYPE": content_type, |
| 659 | "wsgi.input": FakePayload(data), |
| 660 | } |
| 661 | ) |
| 662 | if headers: |
| 663 | extra.update(HttpHeaders.to_wsgi_names(headers)) |
| 664 | if query_params: |
| 665 | extra["QUERY_STRING"] = urlencode(query_params, doseq=True) |
| 666 | r.update(extra) |
| 667 | # If QUERY_STRING is absent or empty, extract it from the URL. |
| 668 | if not r.get("QUERY_STRING"): |
| 669 | # WSGI requires latin-1 encoded strings. See get_path_info(). |
| 670 | r["QUERY_STRING"] = parsed.query.encode().decode("iso-8859-1") |
| 671 | return self.request(**r) |
| 672 | |
| 673 | |
| 674 | class AsyncRequestFactory(RequestFactory): |