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 fileTake 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 back1import json2from http.cookiejar import Cookie, CookieJar3Drop sync (#544)Tom Christiebefore adoption206c537
lines 4–5why, one step back4import pytest5Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 6–9why, one step back6from httpx import (7 CertTypes,8 Client,9 Cookies,Drop sync (#544)Tom Christiebefore adoption206c537
lines 10–12why, one step back10 Dispatcher,11 Request,12 Response,Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 13–17why, one step back13 TimeoutTypes,14 VerifyTypes,15)1617Drop sync (#544)Tom Christiebefore adoption206c537
line 18why, one step back18class MockDispatch(Dispatcher):Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 19–20why, one step back19 async def send(20 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 21why, one step back21 request: Request,Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 22–24why, one step back22 verify: VerifyTypes = None,23 cert: CertTypes = None,24 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 25why, one step back25 ) -> Response:Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 26–27why, one step back26 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 back28 return Response(200, content=body, request=request)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 29–30why, one step back29 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 back31 return Response(200, headers=headers, request=request)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 32–33why, one step back3233Drop sync (#544)Tom Christiebefore adoption206c537
lines 34–35why, one step back34@pytest.mark.asyncio35async def test_set_cookie():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 36–41why, one step back36 """37 Send a request including a cookie.38 """39 url = "http://example.org/echo_cookies"40 cookies = {"example-name": "example-value"}41Drop sync (#544)Tom Christiebefore adoption206c537
lines 42–43why, one step back42 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 back4445 assert response.status_code == 20046 assert response.json() == {"cookies": "example-name=example-value"}4748Drop sync (#544)Tom Christiebefore adoption206c537
lines 49–50why, one step back49@pytest.mark.asyncio50async def test_set_cookie_with_cookiejar():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 51–77why, one step back51 """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)77Drop sync (#544)Tom Christiebefore adoption206c537
lines 78–79why, one step back78 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 back8081 assert response.status_code == 20082 assert response.json() == {"cookies": "example-name=example-value"}8384Drop sync (#544)Tom Christiebefore adoption206c537
lines 85–86why, one step back85@pytest.mark.asyncio86async def test_setting_client_cookies_to_cookiejar():Automatically convert Client.cookies and Client.headers on set (#274)toppkbefore adoption9e420a5
lines 87–113why, one step back87 """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)113Drop sync (#544)Tom Christiebefore adoption206c537
lines 114–116why, one step back114 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 back117118 assert response.status_code == 200119 assert response.json() == {"cookies": "example-name=example-value"}120121Drop sync (#544)Tom Christiebefore adoption206c537
lines 122–123why, one step back122@pytest.mark.asyncio123async def test_set_cookie_with_cookies_model():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 124–131why, one step back124 """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"131Drop sync (#544)Tom Christiebefore adoption206c537
lines 132–133why, one step back132 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 back134135 assert response.status_code == 200136 assert response.json() == {"cookies": "example-name=example-value"}137138Drop sync (#544)Tom Christiebefore adoption206c537
lines 139–140why, one step back139@pytest.mark.asyncio140async def test_get_cookie():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 141–142why, one step back141 url = "http://example.org/set_cookie"142Drop sync (#544)Tom Christiebefore adoption206c537
lines 143–144why, one step back143 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 back145146 assert response.status_code == 200147 assert response.cookies["example-name"] == "example-value"148 assert client.cookies["example-name"] == "example-value"149150Drop sync (#544)Tom Christiebefore adoption206c537
lines 151–152why, one step back151@pytest.mark.asyncio152async def test_cookie_persistence():Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
lines 153–155why, one step back153 """154 Ensure that Client instances persist cookies between requests.155 """Drop sync (#544)Tom Christiebefore adoption206c537
line 156why, one step back156 client = Client(dispatch=MockDispatch())Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 157why, one step back157Drop sync (#544)Tom Christiebefore adoption206c537
lines 158–160why, one step back158 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 back161Drop sync (#544)Tom Christiebefore adoption206c537
lines 162–169why, one step back162 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"}