Skip to content
Myr

test_cookies.py

This file

Path

tests/client/test_cookies.py

164 lines · 4.8 KiB · Python

Last changed by

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

History of this file

Take it

The walk stopped after 200 commits: lines older than b574230 point at it.

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 4–5why, one step back
4import pytest
5

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 6why, one step back
6from httpx import AsyncClient, Cookies, Request, Response

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 7why, one step back
7from httpx.config import CertTypes, TimeoutTypes, VerifyTypes

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 8why, one step back
8from httpx.dispatch.base import AsyncDispatcher

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 9–10why, one step back
9
10

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

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

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 12–13why, one step back
12 async def send(
13 self,

Drop sync (#544)Tom Christiebefore adoption206c537

line 14why, one step back
14 request: Request,

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 15–17why, one step back
15 verify: VerifyTypes = None,
16 cert: CertTypes = None,17 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

line 18why, one step back
18 ) -> Response:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 19–20why, one step back
19 if request.url.path.startswith("/echo_cookies"):
20 body = json.dumps({"cookies": request.headers.get("Cookie")}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 21why, one step back
21 return Response(200, content=body, request=request)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 22–23why, one step back
22 elif request.url.path.startswith("/set_cookie"):
23 headers = {"set-cookie": "example-name=example-value"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 24why, one step back
24 return Response(200, headers=headers, request=request)

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

lines 25–26why, one step back
25 else:
26 raise NotImplementedError # pragma: no cover

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 27–28why, one step back
27
28

Drop sync (#544)Tom Christiebefore adoption206c537

line 29why, one step back
29@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 30why, one step back
30async def test_set_cookie() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 31–36why, one step back
31 """
32 Send a request including a cookie.33 """34 url = "http://example.org/echo_cookies"35 cookies = {"example-name": "example-value"}36

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 37why, one step back
37 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 38why, one step back
38 response = await client.get(url, cookies=cookies)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 39–43why, one step back
39
40 assert response.status_code == 20041 assert response.json() == {"cookies": "example-name=example-value"}4243

Drop sync (#544)Tom Christiebefore adoption206c537

line 44why, one step back
44@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 45why, one step back
45async def test_set_cookie_with_cookiejar() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 46–67why, one step back
46 """
47 Send a request including a cookie, using a `CookieJar` instance.48 """4950 url = "http://example.org/echo_cookies"51 cookies = CookieJar()52 cookie = Cookie(53 version=0,54 name="example-name",55 value="example-value",56 port=None,57 port_specified=False,58 domain="",59 domain_specified=False,60 domain_initial_dot=False,61 path="/",62 path_specified=True,63 secure=False,64 expires=None,65 discard=True,66 comment=None,67 comment_url=None,

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 68why, one step back
68 rest={"HttpOnly": ""},

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 69–72why, one step back
69 rfc2109=False,
70 )71 cookies.set_cookie(cookie)72

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 73why, one step back
73 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 74why, one step back
74 response = await client.get(url, cookies=cookies)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 75–79why, one step back
75
76 assert response.status_code == 20077 assert response.json() == {"cookies": "example-name=example-value"}7879

Drop sync (#544)Tom Christiebefore adoption206c537

line 80why, one step back
80@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 81why, one step back
81async def test_setting_client_cookies_to_cookiejar() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 82–103why, one step back
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,

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 104why, one step back
104 rest={"HttpOnly": ""},

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 105–108why, one step back
105 rfc2109=False,
106 )107 cookies.set_cookie(cookie)108

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 109why, one step back
109 client = AsyncClient(dispatch=MockDispatch())

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 110why, one step back
110 client.cookies = cookies # type: ignore

Drop sync (#544)Tom Christiebefore adoption206c537

line 111why, one step back
111 response = await client.get(url)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 112–116why, one step back
112
113 assert response.status_code == 200114 assert response.json() == {"cookies": "example-name=example-value"}115116

Drop sync (#544)Tom Christiebefore adoption206c537

line 117why, one step back
117@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 118why, one step back
118async def test_set_cookie_with_cookies_model() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 119–126why, one step back
119 """
120 Send a request including a cookie, using a `Cookies` instance.121 """122123 url = "http://example.org/echo_cookies"124 cookies = Cookies()125 cookies["example-name"] = "example-value"126

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 127why, one step back
127 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 128why, one step back
128 response = await client.get(url, cookies=cookies)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 129–133why, one step back
129
130 assert response.status_code == 200131 assert response.json() == {"cookies": "example-name=example-value"}132133

Drop sync (#544)Tom Christiebefore adoption206c537

line 134why, one step back
134@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 135why, one step back
135async def test_get_cookie() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 136–137why, one step back
136 url = "http://example.org/set_cookie"
137

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 138why, one step back
138 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 139why, one step back
139 response = await client.get(url)

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 140–145why, one step back
140
141 assert response.status_code == 200142 assert response.cookies["example-name"] == "example-value"143 assert client.cookies["example-name"] == "example-value"144145

Drop sync (#544)Tom Christiebefore adoption206c537

line 146why, one step back
146@pytest.mark.asyncio

Type-check test_cookies.py (#677)Florimond Mancabefore adoption0ee0005

line 147why, one step back
147async def test_cookie_persistence() -> None:

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

lines 148–150why, one step back
148 """
149 Ensure that Client instances persist cookies between requests.150 """

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 151why, one step back
151 client = AsyncClient(dispatch=MockDispatch())

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

line 152why, one step back
152

Drop sync (#544)Tom Christiebefore adoption206c537

lines 153–155why, one step back
153 response = await client.get("http://example.org/echo_cookies")
154 assert response.status_code == 200155 assert response.json() == {"cookies": None}

Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)Seth Michael Larsonbefore adoptionb574230

line 156why, one step back
156

Drop sync (#544)Tom Christiebefore adoption206c537

lines 157–164why, one step back
157 response = await client.get("http://example.org/set_cookie")
158 assert response.status_code == 200159 assert response.cookies["example-name"] == "example-value"160 assert client.cookies["example-name"] == "example-value"161162 response = await client.get("http://example.org/echo_cookies")163 assert response.status_code == 200164 assert response.json() == {"cookies": "example-name=example-value"}