Skip to content
Myr

test_cookies.py

This file

Path

tests/client/test_cookies.py

146 lines · 4.3 KiB · Python

Last changed by

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

History of this file

Take it

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 1–2why, one step back
1from http.cookiejar import Cookie, CookieJar
2

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 3why, one step back
3import httpx

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 4why, one step back
4from tests.utils import MockTransport

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 5–6why, one step back
5
6

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 7–9why, one step back
7def get_and_set_cookies(request: httpx.Request) -> httpx.Response:
8 if request.url.path == "/echo_cookies":9 data = {"cookies": request.headers.get("cookie")}

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 10why, one step back
10 return httpx.Response(200, json=data)

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 11–14why, one step back
11 elif request.url.path == "/set_cookie":
12 return httpx.Response(200, headers={"set-cookie": "example-name=example-value"})13 else:14 raise NotImplementedError() # pragma: no cover

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 15–16why, one step back
15
16

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 18–23why, one step back
18 """
19 Send a request including a cookie.20 """21 url = "http://example.org/echo_cookies"22 cookies = {"example-name": "example-value"}23

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 24why, one step back
24 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 26–30why, one step back
26
27 assert response.status_code == 20028 assert response.json() == {"cookies": "example-name=example-value"}2930

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 32–58why, one step back
32 """
33 Send a request including a cookie, using a `CookieJar` instance.34 """3536 url = "http://example.org/echo_cookies"37 cookies = CookieJar()38 cookie = Cookie(39 version=0,40 name="example-name",41 value="example-value",42 port=None,43 port_specified=False,44 domain="",45 domain_specified=False,46 domain_initial_dot=False,47 path="/",48 path_specified=True,49 secure=False,50 expires=None,51 discard=True,52 comment=None,53 comment_url=None,54 rest={"HttpOnly": ""},55 rfc2109=False,56 )57 cookies.set_cookie(cookie)58

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 59why, one step back
59 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 61–65why, one step back
61
62 assert response.status_code == 20063 assert response.json() == {"cookies": "example-name=example-value"}6465

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 67–93why, one step back
67 """
68 Send a request including a cookie, using a `CookieJar` instance.69 """7071 url = "http://example.org/echo_cookies"72 cookies = CookieJar()73 cookie = Cookie(74 version=0,75 name="example-name",76 value="example-value",77 port=None,78 port_specified=False,79 domain="",80 domain_specified=False,81 domain_initial_dot=False,82 path="/",83 path_specified=True,84 secure=False,85 expires=None,86 discard=True,87 comment=None,88 comment_url=None,89 rest={"HttpOnly": ""},90 rfc2109=False,91 )92 cookies.set_cookie(cookie)93

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 94why, one step back
94 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 97–101why, one step back
97
98 assert response.status_code == 20099 assert response.json() == {"cookies": "example-name=example-value"}100101

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 103–107why, one step back
103 """
104 Send a request including a cookie, using a `Cookies` instance.105 """106107 url = "http://example.org/echo_cookies"

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 108why, one step back
108 cookies = httpx.Cookies()

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 109–110why, one step back
109 cookies["example-name"] = "example-value"
110

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 111why, one step back
111 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 113–117why, one step back
113
114 assert response.status_code == 200115 assert response.json() == {"cookies": "example-name=example-value"}116117

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 121why, one step back
121 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 123–128why, one step back
123
124 assert response.status_code == 200125 assert response.cookies["example-name"] == "example-value"126 assert client.cookies["example-name"] == "example-value"127128

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 130–132why, one step back
130 """
131 Ensure that Client instances persist cookies between requests.132 """

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 133why, one step back
133 client = httpx.Client(transport=MockTransport(get_and_set_cookies))

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 134why, one step back
134

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 135why, one step back
135 response = client.get("http://example.org/echo_cookies")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 136–138why, one step back
136 assert response.status_code == 200
137 assert response.json() == {"cookies": None}138

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 139why, one step back
139 response = client.get("http://example.org/set_cookie")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 144why, one step back
144 response = client.get("http://example.org/echo_cookies")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 145–146why, one step back
145 assert response.status_code == 200
146 assert response.json() == {"cookies": "example-name=example-value"}