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 ..")
async def test_cookie_load(): with NamedTemporaryFile(mode="w") as json_fd: data = { '.httpbin.org': { '/': { 'foo': { 'expires': None, 'port': None, 'secure': False, 'value': 'bar', 'version': 0 } }, '/welcome/': { 'dead': { 'expires': None, 'port': None, 'secure': False, 'value': 'beef', 'version': 0 } } } } json.dump(data, json_fd) json_cookie = JsonCookie() json_cookie.load(json_fd.name) jar = json_cookie.cookiejar("httpbin.org") for cookie in jar: assert (cookie.name == "foo" and cookie.value == "bar" and cookie.path == "/") or\ (cookie.name == "dead" and cookie.value == "beef" and cookie.path == "/welcome/") json_cookie.dump()