test_cookies.py
This file
Path
tests/client/test_cookies.py
124 lines · 3.6 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
Tweak test namingTom Christiebefore adoptiona284d0b
lines 1–3why, one step back1import json2from http.cookiejar import Cookie, CookieJar3Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 4why, one step back4from httpx import (Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 5–7why, one step back5 AsyncDispatcher,6 AsyncRequest,7 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 8why, one step back8 CertTypes,Tweak test namingTom Christiebefore adoptiona284d0b
lines 9–10why, one step back9 Client,10 Cookies,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 11–12why, one step back11 TimeoutTypes,12 VerifyTypes,Tweak test namingTom Christiebefore adoptiona284d0b
lines 13–15why, one step back13)1415Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 16why, one step back16class MockDispatch(AsyncDispatcher):Tweak test namingTom Christiebefore adoptiona284d0b
lines 17–18why, one step back17 async def send(18 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 19why, one step back19 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 20–22why, one step back20 verify: VerifyTypes = None,21 cert: CertTypes = None,22 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 23why, one step back23 ) -> AsyncResponse:Tweak test namingTom Christiebefore adoptiona284d0b
lines 24–25why, one step back24 if request.url.path.startswith("/echo_cookies"):25 body = json.dumps({"cookies": request.headers.get("Cookie")}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 26why, one step back26 return AsyncResponse(200, content=body, request=request)Tweak test namingTom Christiebefore adoptiona284d0b
lines 27–28why, one step back27 elif request.url.path.startswith("/set_cookie"):28 headers = {"set-cookie": "example-name=example-value"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 29why, one step back29 return AsyncResponse(200, headers=headers, request=request)Tweak test namingTom Christiebefore adoptiona284d0b
lines 30–42why, one step back303132def test_set_cookie():33 """34 Send a request including a cookie.35 """36 url = "http://example.org/echo_cookies"37 cookies = {"example-name": "example-value"}3839 with Client(dispatch=MockDispatch()) as client:40 response = client.get(url, cookies=cookies)4142 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 43why, one step back43 assert response.json() == {"cookies": "example-name=example-value"}Tweak test namingTom Christiebefore adoptiona284d0b
lines 44–77why, one step back444546def test_set_cookie_with_cookiejar():47 """48 Send a request including a cookie, using a `CookieJar` instance.49 """5051 url = "http://example.org/echo_cookies"52 cookies = CookieJar()53 cookie = Cookie(54 version=0,55 name="example-name",56 value="example-value",57 port=None,58 port_specified=False,59 domain="",60 domain_specified=False,61 domain_initial_dot=False,62 path="/",63 path_specified=True,64 secure=False,65 expires=None,66 discard=True,67 comment=None,68 comment_url=None,69 rest={"HttpOnly": None},70 rfc2109=False,71 )72 cookies.set_cookie(cookie)7374 with Client(dispatch=MockDispatch()) as client:75 response = client.get(url, cookies=cookies)7677 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 78why, one step back78 assert response.json() == {"cookies": "example-name=example-value"}Tweak test namingTom Christiebefore adoptiona284d0b
lines 79–93why, one step back798081def test_set_cookie_with_cookies_model():82 """83 Send a request including a cookie, using a `Cookies` instance.84 """8586 url = "http://example.org/echo_cookies"87 cookies = Cookies()88 cookies["example-name"] = "example-value"8990 with Client(dispatch=MockDispatch()) as client:91 response = client.get(url, cookies=cookies)9293 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 94why, one step back94 assert response.json() == {"cookies": "example-name=example-value"}Tweak test namingTom Christiebefore adoptiona284d0b
lines 95–114why, one step back959697def test_get_cookie():98 url = "http://example.org/set_cookie"99100 with Client(dispatch=MockDispatch()) as client:101 response = client.get(url)102103 assert response.status_code == 200104 assert response.cookies["example-name"] == "example-value"105 assert client.cookies["example-name"] == "example-value"106107108def test_cookie_persistence():109 """110 Ensure that Client instances persist cookies between requests.111 """112 with Client(dispatch=MockDispatch()) as client:113 response = client.get("http://example.org/echo_cookies")114 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 115why, one step back115 assert response.json() == {"cookies": None}Tweak test namingTom Christiebefore adoptiona284d0b
lines 116–123why, one step back116117 response = client.get("http://example.org/set_cookie")118 assert response.status_code == 200119 assert response.cookies["example-name"] == "example-value"120 assert client.cookies["example-name"] == "example-value"121122 response = client.get("http://example.org/echo_cookies")123 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 124why, one step back124 assert response.json() == {"cookies": "example-name=example-value"}