Skip to content
Myr

test_cookies.py

This file

Path

tests/client/test_cookies.py

169 lines · 4.6 KiB · Python

Last changed by

Drop sync (#544)Tom Christiebefore adoption206c537

History of this file

Take it

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 6–9why, one step back
6from httpx import (
7 CertTypes,8 Client,9 Cookies,

Drop sync (#544)Tom Christiebefore adoption206c537

lines 10–12why, one step back
10 Dispatcher,
11 Request,12 Response,

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 13–17why, one step back
13 TimeoutTypes,
14 VerifyTypes,15)1617

Drop sync (#544)Tom Christiebefore adoption206c537

line 18why, one step back
18class MockDispatch(Dispatcher):

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 19–20why, one step back
19 async def send(
20 self,

Drop sync (#544)Tom Christiebefore adoption206c537

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 32–33why, one step back
32
33

Drop sync (#544)Tom Christiebefore adoption206c537

lines 34–35why, one step back
34@pytest.mark.asyncio
35async def test_set_cookie():

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 36–41why, one step back
36 """
37 Send a request including a cookie.38 """39 url = "http://example.org/echo_cookies"40 cookies = {"example-name": "example-value"}41

Drop sync (#544)Tom Christiebefore adoption206c537

lines 42–43why, one step back
42 client = Client(dispatch=MockDispatch())
43 response = await client.get(url, cookies=cookies)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 44–48why, one step back
44
45 assert response.status_code == 20046 assert response.json() == {"cookies": "example-name=example-value"}4748

Drop sync (#544)Tom Christiebefore adoption206c537

lines 49–50why, one step back
49@pytest.mark.asyncio
50async def test_set_cookie_with_cookiejar():

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 51–77why, one step back
51 """
52 Send a request including a cookie, using a `CookieJar` instance.53 """5455 url = "http://example.org/echo_cookies"56 cookies = CookieJar()57 cookie = Cookie(58 version=0,59 name="example-name",60 value="example-value",61 port=None,62 port_specified=False,63 domain="",64 domain_specified=False,65 domain_initial_dot=False,66 path="/",67 path_specified=True,68 secure=False,69 expires=None,70 discard=True,71 comment=None,72 comment_url=None,73 rest={"HttpOnly": None},74 rfc2109=False,75 )76 cookies.set_cookie(cookie)77

Drop sync (#544)Tom Christiebefore adoption206c537

lines 78–79why, one step back
78 client = Client(dispatch=MockDispatch())
79 response = await client.get(url, cookies=cookies)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 80–84why, one step back
80
81 assert response.status_code == 20082 assert response.json() == {"cookies": "example-name=example-value"}8384

Drop sync (#544)Tom Christiebefore adoption206c537

lines 85–86why, one step back
85@pytest.mark.asyncio
86async def test_setting_client_cookies_to_cookiejar():

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

lines 87–113why, one step back
87 """
88 Send a request including a cookie, using a `CookieJar` instance.89 """9091 url = "http://example.org/echo_cookies"92 cookies = CookieJar()93 cookie = Cookie(94 version=0,95 name="example-name",96 value="example-value",97 port=None,98 port_specified=False,99 domain="",100 domain_specified=False,101 domain_initial_dot=False,102 path="/",103 path_specified=True,104 secure=False,105 expires=None,106 discard=True,107 comment=None,108 comment_url=None,109 rest={"HttpOnly": None},110 rfc2109=False,111 )112 cookies.set_cookie(cookie)113

Drop sync (#544)Tom Christiebefore adoption206c537

lines 114–116why, one step back
114 client = Client(dispatch=MockDispatch())
115 client.cookies = cookies116 response = await client.get(url)

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

lines 117–121why, one step back
117
118 assert response.status_code == 200119 assert response.json() == {"cookies": "example-name=example-value"}120121

Drop sync (#544)Tom Christiebefore adoption206c537

lines 122–123why, one step back
122@pytest.mark.asyncio
123async def test_set_cookie_with_cookies_model():

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 132–133why, one step back
132 client = Client(dispatch=MockDispatch())
133 response = await client.get(url, cookies=cookies)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 134–138why, one step back
134
135 assert response.status_code == 200136 assert response.json() == {"cookies": "example-name=example-value"}137138

Drop sync (#544)Tom Christiebefore adoption206c537

lines 139–140why, one step back
139@pytest.mark.asyncio
140async def test_get_cookie():

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 143–144why, one step back
143 client = Client(dispatch=MockDispatch())
144 response = await client.get(url)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 145–150why, one step back
145
146 assert response.status_code == 200147 assert response.cookies["example-name"] == "example-value"148 assert client.cookies["example-name"] == "example-value"149150

Drop sync (#544)Tom Christiebefore adoption206c537

lines 151–152why, one step back
151@pytest.mark.asyncio
152async def test_cookie_persistence():

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

lines 153–155why, one step back
153 """
154 Ensure that Client instances persist cookies between requests.155 """

Drop sync (#544)Tom Christiebefore adoption206c537

line 156why, one step back
156 client = Client(dispatch=MockDispatch())

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 157why, one step back
157

Drop sync (#544)Tom Christiebefore adoption206c537

lines 158–160why, one step back
158 response = await client.get("http://example.org/echo_cookies")
159 assert response.status_code == 200160 assert response.json() == {"cookies": None}

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 161why, one step back
161

Drop sync (#544)Tom Christiebefore adoption206c537

lines 162–169why, one step back
162 response = await client.get("http://example.org/set_cookie")
163 assert response.status_code == 200164 assert response.cookies["example-name"] == "example-value"165 assert client.cookies["example-name"] == "example-value"166167 response = await client.get("http://example.org/echo_cookies")168 assert response.status_code == 200169 assert response.json() == {"cookies": "example-name=example-value"}