MockRequest Class — requests Architecture
Architecture documentation for the MockRequest class in cookies.py from the requests codebase.
Entity Profile
Dependency Diagram
graph TD ad93b63e_dc2f_11b4_dbba_995ffe6bcf13["MockRequest"] 270696ff_2a4f_ef5b_92e8_33a79e68a2d8["cookies.py"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|defined in| 270696ff_2a4f_ef5b_92e8_33a79e68a2d8 2f078143_3cd3_524b_cc61_9aa20776c62f["__init__()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 2f078143_3cd3_524b_cc61_9aa20776c62f 1b260d58_172c_7fd9_ccc3_73f2ccf6da95["get_type()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 1b260d58_172c_7fd9_ccc3_73f2ccf6da95 12329c13_8fec_ecc4_a995_93e3e9a1b72d["get_host()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 12329c13_8fec_ecc4_a995_93e3e9a1b72d 3f0e9c17_865c_bf7a_d792_9b1cfe7b7a1b["get_origin_req_host()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 3f0e9c17_865c_bf7a_d792_9b1cfe7b7a1b 63cf6778_9f88_2740_3c09_2b2c4c8e761c["get_full_url()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 63cf6778_9f88_2740_3c09_2b2c4c8e761c c61c468b_1fe4_4d77_20d6_f8ddd3b770ac["is_unverifiable()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| c61c468b_1fe4_4d77_20d6_f8ddd3b770ac 9432366e_71db_2fad_ed15_6afac3c7047b["has_header()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 9432366e_71db_2fad_ed15_6afac3c7047b aab71a68_e0d2_7315_8f2e_a4e7ca902ef9["get_header()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| aab71a68_e0d2_7315_8f2e_a4e7ca902ef9 aae59d68_31d3_c68b_3f3d_e84a114e5da7["add_header()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| aae59d68_31d3_c68b_3f3d_e84a114e5da7 d50c1686_8666_8262_bbfc_879b42e7d015["add_unredirected_header()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| d50c1686_8666_8262_bbfc_879b42e7d015 4c38d61e_6c5d_f45f_3269_d7e9ef19c8ff["get_new_headers()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 4c38d61e_6c5d_f45f_3269_d7e9ef19c8ff 4a3a50e0_bd2f_a057_48bc_ec3283a1e834["unverifiable()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 4a3a50e0_bd2f_a057_48bc_ec3283a1e834 9869de3e_3d49_ace6_1445_040b13192c70["origin_req_host()"] ad93b63e_dc2f_11b4_dbba_995ffe6bcf13 -->|method| 9869de3e_3d49_ace6_1445_040b13192c70
Relationship Graph
Source Code
src/requests/cookies.py lines 23–100
class MockRequest:
"""Wraps a `requests.Request` 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 request object is read-only. The client is responsible for collecting
the new headers via `get_new_headers()` and interpreting them appropriately. You
probably want `get_cookie_header`, defined below.
"""
def __init__(self, request):
self._r = request
self._new_headers = {}
self.type = urlparse(self._r.url).scheme
def get_type(self):
return self.type
def get_host(self):
return urlparse(self._r.url).netloc
def get_origin_req_host(self):
return self.get_host()
def get_full_url(self):
# Only return the response's URL if the user hadn't set the Host
# header
if not self._r.headers.get("Host"):
return self._r.url
# If they did set it, retrieve it and reconstruct the expected domain
host = to_native_string(self._r.headers["Host"], encoding="utf-8")
parsed = urlparse(self._r.url)
# Reconstruct the URL as we expect it
return urlunparse(
[
parsed.scheme,
host,
parsed.path,
parsed.params,
parsed.query,
parsed.fragment,
]
)
def is_unverifiable(self):
return True
def has_header(self, name):
return name in self._r.headers or name in self._new_headers
def get_header(self, name, default=None):
return self._r.headers.get(name, self._new_headers.get(name, default))
def add_header(self, key, val):
"""cookiejar has no legitimate use for this method; add it back if you find one."""
raise NotImplementedError(
"Cookie headers should be added with add_unredirected_header()"
)
def add_unredirected_header(self, name, value):
self._new_headers[name] = value
def get_new_headers(self):
return self._new_headers
@property
def unverifiable(self):
return self.is_unverifiable()
@property
def origin_req_host(self):
return self.get_origin_req_host()
@property
def host(self):
return self.get_host()
Domain
Defined In
Source
Frequently Asked Questions
What is the MockRequest class?
MockRequest is a class in the requests codebase, defined in src/requests/cookies.py.
Where is MockRequest defined?
MockRequest is defined in src/requests/cookies.py at line 23.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free