test_cookies.py
This file
Path
tests/models/test_cookies.py
87 lines · 2.5 KiB · Python
Last changed by
Fixed test_multiple_set_cookie (#1270)cdelerbefore adoption15187e7
History of this fileTake it
1import http23import pytest45import httpx678def test_cookies():9 cookies = httpx.Cookies({"name": "value"})10 assert cookies["name"] == "value"11 assert "name" in cookies12 assert len(cookies) == 113 assert dict(cookies) == {"name": "value"}14 assert bool(cookies) is True1516 del cookies["name"]17 assert "name" not in cookies18 assert len(cookies) == 019 assert dict(cookies) == {}20 assert bool(cookies) is False212223def test_cookies_update():24 cookies = httpx.Cookies()25 more_cookies = httpx.Cookies()26 more_cookies.set("name", "value", domain="example.com")2728 cookies.update(more_cookies)29 assert dict(cookies) == {"name": "value"}30 assert cookies.get("name", domain="example.com") == "value"313233def test_cookies_with_domain():34 cookies = httpx.Cookies()35 cookies.set("name", "value", domain="example.com")36 cookies.set("name", "value", domain="example.org")3738 with pytest.raises(httpx.CookieConflict):39 cookies["name"]4041 cookies.clear(domain="example.com")42 assert len(cookies) == 1434445def test_cookies_with_domain_and_path():46 cookies = httpx.Cookies()47 cookies.set("name", "value", domain="example.com", path="/subpath/1")48 cookies.set("name", "value", domain="example.com", path="/subpath/2")49 cookies.clear(domain="example.com", path="/subpath/1")50 assert len(cookies) == 151 cookies.delete("name", domain="example.com", path="/subpath/2")52 assert len(cookies) == 0535455def test_multiple_set_cookie():56 jar = http.cookiejar.CookieJar()57 headers = [58 (59 b"Set-Cookie",60 b"1P_JAR=2020-08-09-18; expires=Tue, 08-Sep-2099 18:33:35 GMT; "61 b"path=/; domain=.example.org; Secure",62 ),63 (64 b"Set-Cookie",65 b"NID=204=KWdXOuypc86YvRfBSiWoW1dEXfSl_5qI7sxZY4umlk4J35yNTeNEkw15"66 b"MRaujK6uYCwkrtjihTTXZPp285z_xDOUzrdHt4dj0Z5C0VOpbvdLwRdHatHAzQs7"67 b"7TsaiWY78a3qU9r7KP_RbSLvLl2hlhnWFR2Hp5nWKPsAcOhQgSg; expires=Mon, "68 b"08-Feb-2099 18:33:35 GMT; path=/; domain=.example.org; HttpOnly",69 ),70 ]71 request = httpx.Request("GET", "https://www.example.org")72 response = httpx.Response(200, request=request, headers=headers)7374 cookies = httpx.Cookies(jar)75 cookies.extract_cookies(response)7677 assert len(cookies) == 2787980def test_cookies_can_be_a_list_of_tuples():81 cookies_val = [("name1", "val1"), ("name2", "val2")]8283 cookies = httpx.Cookies(cookies_val)8485 assert len(cookies.items()) == 286 for k, v in cookies_val:87 assert cookies[k] == v