MCPcopy
hub / github.com/psf/requests / MockRequest

Class MockRequest

src/requests/cookies.py:31–111  ·  view source on GitHub ↗

Wraps a `requests.PreparedRequest` to mimic a `urllib2.Request`. The code in `http.cookiejar.CookieJar` expects this interface in order to correctly manage cookie policies, i.e., determine whether a cookie can be set, given the domains of the request and the cookie. The original re

Source from the content-addressed store, hash-verified

29
30
31class MockRequest:
32 """Wraps a `requests.PreparedRequest` to mimic a `urllib2.Request`.
33
34 The code in `http.cookiejar.CookieJar` expects this interface in order to correctly
35 manage cookie policies, i.e., determine whether a cookie can be set, given the
36 domains of the request and the cookie.
37
38 The original request object is read-only. The client is responsible for collecting
39 the new headers via `get_new_headers()` and interpreting them appropriately. You
40 probably want `get_cookie_header`, defined below.
41 """
42
43 type: str
44
45 def __init__(self, request: PreparedRequest) -> None:
46 assert _is_prepared(request)
47 self._r = request
48 self._new_headers: dict[str, str] = {}
49 self.type = urlparse(self._r.url).scheme
50
51 def get_type(self) -> str:
52 return self.type
53
54 def get_host(self) -> str:
55 return urlparse(self._r.url).netloc
56
57 def get_origin_req_host(self) -> str:
58 return self.get_host()
59
60 def get_full_url(self) -> str:
61 # Only return the response's URL if the user hadn't set the Host
62 # header
63 if not self._r.headers.get("Host"):
64 return self._r.url
65 # If they did set it, retrieve it and reconstruct the expected domain
66 host = to_native_string(self._r.headers["Host"], encoding="utf-8")
67 parsed = urlparse(self._r.url)
68 # Reconstruct the URL as we expect it
69 return urlunparse(
70 [
71 parsed.scheme,
72 host,
73 parsed.path,
74 parsed.params,
75 parsed.query,
76 parsed.fragment,
77 ]
78 )
79
80 def is_unverifiable(self) -> bool:
81 return True
82
83 def has_header(self, name: str) -> bool:
84 return name in self._r.headers or name in self._new_headers
85
86 def get_header(self, name: str, default: str | None = None) -> str | None:
87 return self._r.headers.get(name, self._new_headers.get(name, default)) # type: ignore[return-value]
88

Callers 2

extract_cookies_to_jarFunction · 0.85
get_cookie_headerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected