Skip to content
Myr

test_cookies.py

This file

Path

tests/client/test_cookies.py

160 lines · 4.5 KiB · Python

Last changed by

Automatically convert Client.cookies and Client.headers on set (#274)toppkbefore adoption9e420a5

History of this file

Take it

Tweak test namingTom Christiebefore adoptiona284d0b

lines 1–3why, one step back
1import json
2from http.cookiejar import Cookie, CookieJar3

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 4why, one step back
4from httpx import (

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 5–7why, one step back
5 AsyncDispatcher,
6 AsyncRequest,7 AsyncResponse,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 8why, one step back
8 CertTypes,

Tweak test namingTom Christiebefore adoptiona284d0b

lines 9–10why, one step back
9 Client,
10 Cookies,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 11–12why, one step back
11 TimeoutTypes,
12 VerifyTypes,

Tweak test namingTom Christiebefore adoptiona284d0b

lines 13–15why, one step back
13)
1415

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 16why, one step back
16class MockDispatch(AsyncDispatcher):

Tweak test namingTom Christiebefore adoptiona284d0b

lines 17–18why, one step back
17 async def send(
18 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 19why, one step back
19 request: AsyncRequest,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 20–22why, one step back
20 verify: VerifyTypes = None,
21 cert: CertTypes = None,22 timeout: TimeoutTypes = None,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 23why, one step back
23 ) -> AsyncResponse:

Tweak test namingTom Christiebefore adoptiona284d0b

lines 24–25why, one step back
24 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 back
26 return AsyncResponse(200, content=body, request=request)

Tweak test namingTom Christiebefore adoptiona284d0b

lines 27–28why, one step back
27 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 back
29 return AsyncResponse(200, headers=headers, request=request)

Tweak test namingTom Christiebefore adoptiona284d0b

lines 30–42why, one step back
30
3132def 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 == 200

Add response.json()Tom Christiebefore adoption2116d44

line 43why, one step back
43 assert response.json() == {"cookies": "example-name=example-value"}

Tweak test namingTom Christiebefore adoptiona284d0b

lines 44–77why, one step back
44
4546def 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 == 200

Add response.json()Tom Christiebefore adoption2116d44

line 78why, one step back
78 assert response.json() == {"cookies": "example-name=example-value"}

Tweak test namingTom Christiebefore adoptiona284d0b

lines 79–80why, one step back
79
80

Automatically convert Client.cookies and Client.headers on set (#274)toppkbefore adoption9e420a5

lines 81–116why, one step back
81def test_setting_client_cookies_to_cookiejar():
82 """83 Send a request including a cookie, using a `CookieJar` instance.84 """8586 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)108109 with Client(dispatch=MockDispatch()) as client:110 client.cookies = cookies111 response = client.get(url)112113 assert response.status_code == 200114 assert response.json() == {"cookies": "example-name=example-value"}115116

Tweak test namingTom Christiebefore adoptiona284d0b

lines 117–129why, one step back
117def test_set_cookie_with_cookies_model():
118 """119 Send a request including a cookie, using a `Cookies` instance.120 """121122 url = "http://example.org/echo_cookies"123 cookies = Cookies()124 cookies["example-name"] = "example-value"125126 with Client(dispatch=MockDispatch()) as client:127 response = client.get(url, cookies=cookies)128129 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 130why, one step back
130 assert response.json() == {"cookies": "example-name=example-value"}

Tweak test namingTom Christiebefore adoptiona284d0b

lines 131–150why, one step back
131
132133def test_get_cookie():134 url = "http://example.org/set_cookie"135136 with Client(dispatch=MockDispatch()) as client:137 response = client.get(url)138139 assert response.status_code == 200140 assert response.cookies["example-name"] == "example-value"141 assert client.cookies["example-name"] == "example-value"142143144def test_cookie_persistence():145 """146 Ensure that Client instances persist cookies between requests.147 """148 with Client(dispatch=MockDispatch()) as client:149 response = client.get("http://example.org/echo_cookies")150 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 151why, one step back
151 assert response.json() == {"cookies": None}

Tweak test namingTom Christiebefore adoptiona284d0b

lines 152–159why, one step back
152
153 response = client.get("http://example.org/set_cookie")154 assert response.status_code == 200155 assert response.cookies["example-name"] == "example-value"156 assert client.cookies["example-name"] == "example-value"157158 response = client.get("http://example.org/echo_cookies")159 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 160why, one step back
160 assert response.json() == {"cookies": "example-name=example-value"}