MCPcopy
hub / github.com/django/django / generic

Method generic

django/test/client.py:733–782  ·  view source on GitHub ↗

Construct an arbitrary HTTP request.

(
        self,
        method,
        path,
        data="",
        content_type="application/octet-stream",
        secure=False,
        *,
        headers=None,
        query_params=None,
        **extra,
    )

Source from the content-addressed store, hash-verified

731 )
732
733 def generic(
734 self,
735 method,
736 path,
737 data="",
738 content_type="application/octet-stream",
739 secure=False,
740 *,
741 headers=None,
742 query_params=None,
743 **extra,
744 ):
745 """Construct an arbitrary HTTP request."""
746 parsed = urlsplit(str(path)) # path can be lazy.
747 data = force_bytes(data, settings.DEFAULT_CHARSET)
748 s = {
749 "method": method,
750 "path": self._get_path(parsed),
751 "server": ("127.0.0.1", "443" if secure else "80"),
752 "scheme": "https" if secure else "http",
753 "headers": [(b"host", b"testserver")],
754 }
755 if self.defaults:
756 extra = {**self.defaults, **extra}
757 if data:
758 s["headers"].extend(
759 [
760 (b"content-length", str(len(data)).encode("ascii")),
761 (b"content-type", content_type.encode("ascii")),
762 ]
763 )
764 s["_body_file"] = FakePayload(data)
765 if query_params:
766 s["query_string"] = urlencode(query_params, doseq=True)
767 elif query_string := extra.pop("QUERY_STRING", None):
768 s["query_string"] = query_string
769 else:
770 # If QUERY_STRING is absent or empty, we want to extract it from
771 # the URL.
772 s["query_string"] = parsed.query
773 if headers:
774 extra.update(HttpHeaders.to_asgi_names(headers))
775 s["headers"] += [
776 # Avoid breaking test clients that just want to supply normalized
777 # ASGI names, regardless of the fact that ASGIRequest drops headers
778 # with underscores (CVE-2026-3902).
779 (key.lower().replace("_", "-").encode("ascii"), value.encode("latin1"))
780 for key, value in extra.items()
781 ]
782 return self.request(**s)
783
784
785class ClientMixin:

Callers

nothing calls this directly

Calls 11

requestMethod · 0.95
force_bytesFunction · 0.90
urlencodeFunction · 0.90
FakePayloadClass · 0.85
_get_pathMethod · 0.80
extendMethod · 0.80
to_asgi_namesMethod · 0.80
encodeMethod · 0.45
popMethod · 0.45
updateMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected