예제 #1
0
async def test_cookie_delete():
    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.open(json_fd.name)
        json_cookie.delete("httpbin.org")
        json_cookie.dump()
        json_cookie.close()

        assert open(json_fd.name).read() == '{}'
예제 #2
0
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.open(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()
        json_cookie.close()
예제 #3
0
async def test_exception_jsoncookie():
    json_cookie = JsonCookie()

    with mock.patch("builtins.open",
                    MagicMock(side_effect=IOError)) as open_mock:
        try:
            json_cookie.load(json_cookie_path)
            pytest.fail("Should raise an exception ..")
        except (IOError, ValueError):
            open_mock.assert_called_with(json_cookie_path,
                                         "w+",
                                         encoding='utf-8')
예제 #4
0
async def test_cookie_dump():
    with NamedTemporaryFile() as json_fd:
        json_cookie = JsonCookie()
        json_cookie.load(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(Request(url))
        await crawler.async_get(Request(url))

        json_cookie.addcookies(crawler.session_cookies)

        await crawler.close()
        json_cookie.dump()

        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
                    }
                }
            }
        }
예제 #5
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 ..")