def test_cookies_with_domain_and_path(): cookies = Cookies() cookies.set("name", "value", domain="example.com", path="/subpath/1") cookies.set("name", "value", domain="example.com", path="/subpath/2") cookies.clear(domain="example.com", path="/subpath/1") assert len(cookies) == 1 cookies.delete("name", domain="example.com", path="/subpath/2") assert len(cookies) == 0
def test_cookies_update(): cookies = Cookies() more_cookies = Cookies() more_cookies.set("name", "value", domain="example.com") cookies.update(more_cookies) assert dict(cookies) == {"name": "value"} assert cookies.get("name", domain="example.com") == "value"
async def test_jsoncookie(): files = { f'{json_cookie_path}': cookie_content, } json_cookie = JsonCookie() with mock.patch("builtins.open", get_mock_open(files)) as open_mock: try: json_cookie.load(json_cookie_path) open_mock.assert_called_once_with(json_cookie_path, "r+", encoding='utf-8') except (IOError, ValueError): pytest.fail("Unexpected IOError ..") assert json_cookie.filename == json_cookie_path assert len(json_cookie.cookiedict) == 1 cookie_jar: CookieJar = json_cookie.cookiejar(cookie_domain_1) assert cookie_jar is not None assert cookie_jar.__len__() == 1 cookies = Cookies() cookies.set("secret_cookie", "secret", cookie_domain_1, "/") cookies.set("secret_cookie", "secret", cookie_domain_2, "/") assert json_cookie.addcookies(cookies) is not False cookie_jar = json_cookie.cookiejar(cookie_domain_1) assert cookie_jar is not None assert cookie_jar.__len__() == 2 assert json_cookie.delete(cookie_domain_1) is True cookie_jar = json_cookie.cookiejar(cookie_domain_1) assert cookie_jar is not None assert cookie_jar.__len__() == 0 cookie_jar = json_cookie.cookiejar(cookie_domain_2) assert cookie_jar is not None assert cookie_jar.__len__() == 1 with mock.patch("builtins.open", get_mock_open(files)) as open_mock: try: json_cookie.dump() open_mock.assert_called_once_with(json_cookie_path, "r+", encoding='utf-8') except (IOError, ValueError): pytest.fail("Unexpected IOError ..")
def test_cookies_with_domain(): cookies = Cookies() cookies.set("name", "value", domain="example.com") cookies.set("name", "value", domain="example.org") with pytest.raises(CookieConflict): cookies["name"] cookies.clear(domain="example.com") assert len(cookies) == 1