Automatically convert Client.cookies and Client.headers on set (#274)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by toppk · committed by Seth Michael Larson · parent 1f4b8dd · Browse files · 4 files·+73 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+73 added−2 removed
- tests:+53 added−0 removed
- other:+20 added−2 removed
Provenance
- Commit
9e420a5- Tree
b1aa06e- Author
toppk <toppk@bllue.org>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
1f4b8dd
4 files changed+73 added−2 removed
modified:docs/api.md+2 added
25 unmodified lines26272829303132333435120 unmodified linesExpand all25 unmodified lines>>> response = client.get('https://example.org')```
* `def __init__([auth], [headers], [cookies], [verify], [cert], [timeout], [pool_limits], [max_redirects], [app], [dispatch])`* `.headers` - **Headers*** `.cookies` - **Cookies*** `def .get(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`* `def .options(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`* `def .head(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`* `def .post(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`120 unmodified linesExpand allmodified:httpx/client.py+18 added-2 removed
100 unmodified lines101 else:102 self.base_url = URL(base_url)103 104 self.auth = auth105- self.headers = Headers(headers)106- self.cookies = Cookies(cookies)105+ self._headers = Headers(headers)106+ self._cookies = Cookies(cookies)107 self.max_redirects = max_redirects108 self.dispatch = async_dispatch109 self.concurrency_backend = backend110 self.trust_env = True if trust_env is None else trust_env111 112+ @property113+ def headers(self) -> Headers:114+ return self._headers115+116+ @headers.setter117+ def headers(self, headers: HeaderTypes) -> None:118+ self._headers = Headers(headers)119+120+ @property121+ def cookies(self) -> Cookies:122+ return self._cookies123+124+ @cookies.setter125+ def cookies(self, cookies: CookieTypes) -> None:126+ self._cookies = Cookies(cookies)127+128 def check_concurrency_backend(self, backend: ConcurrencyBackend) -> None:129 pass # pragma: no cover130 131 def merge_url(self, url: URLTypes) -> URL:877 unmodified linesmodified:tests/client/test_cookies.py+36 added
76 unmodified lines77 assert response.status_code == 20078 assert response.json() == {"cookies": "example-name=example-value"}79 80 81+def test_setting_client_cookies_to_cookiejar():82+ """83+ Send a request including a cookie, using a `CookieJar` instance.84+ """85+86+ url = "http://example.org/echo_cookies"87+ cookies = CookieJar()88+ cookie = Cookie(89+ version=0,90+ name="example-name",91+ value="example-value",92+ port=None,93+ port_specified=False,94+ domain="",95+ domain_specified=False,96+ domain_initial_dot=False,97+ path="/",98+ path_specified=True,99+ secure=False,100+ expires=None,101+ discard=True,102+ comment=None,103+ comment_url=None,104+ rest={"HttpOnly": None},105+ rfc2109=False,106+ )107+ cookies.set_cookie(cookie)108+109+ with Client(dispatch=MockDispatch()) as client:110+ client.cookies = cookies111+ response = client.get(url)112+113+ assert response.status_code == 200114+ assert response.json() == {"cookies": "example-name=example-value"}115+116+117 def test_set_cookie_with_cookies_model():118 """119 Send a request including a cookie, using a `Cookies` instance.120 """40 unmodified linesadded:tests/client/test_properties.py+17 added
1+from httpx import Client, Cookies, Headers2+3+4+def test_client_headers():5+ client = Client()6+ client.headers = {"a": "b"}7+ assert isinstance(client.headers, Headers)8+ assert client.headers["A"] == "b"9+10+11+def test_client_cookies():12+ client = Client()13+ client.cookies = {"a": "b"}14+ assert isinstance(client.cookies, Cookies)15+ mycookies = list(client.cookies.jar)16+ assert len(mycookies) == 117+ assert mycookies[0].name == "a" and mycookies[0].value == "b"