async def test_cookie_dump(): with NamedTemporaryFile() as json_fd: json_cookie = JsonCookie() json_cookie.open(json_fd.name) json_cookie.delete("httpbin.org") url = "http://httpbin.org/welcome/" respx.get(url).mock(return_value=httpx.Response( 200, headers=[( "set-cookie", "foo=bar; Path=/"), ("set-cookie", "dead=beef; Path=/welcome/")])) crawler = AsyncCrawler(url) await crawler.async_get(Request(url)) json_cookie.addcookies(crawler.session_cookies) await crawler.close() json_cookie.dump() json_cookie.close() data = json.load(open(json_fd.name)) assert 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 } } } }
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 ..")