def test_get_cookies_for_url_ignores_secure_cookies_for_http():
    jar = CookieJar()

    jar.add(URL(b"https://foo.org"), Cookie(b"hello", b"world", secure=True))

    cookies = list(jar.get_cookies_for_url(URL(b"http://foo.org/hello-world")))
    assert len(cookies) == 0
def test_cookiejar_ignores_cookie_domain_set_as_ipaddress():
    jar = CookieJar()

    assert (jar.get_domain(
        URL(b"https://foo.org/hello-world"),
        Cookie(b"foo", b"foo", domain=b"192.168.1.5"),
    ) == b"foo.org")
Example #3
0
def test_get_cookies_for_url():
    jar = CookieJar()

    jar.add(URL(b"https://foo.org"), Cookie("hello", "world"))

    cookies = list(jar.get_cookies_for_url(URL(b"https://foo.org/hello-world")))

    assert len(cookies) == 1
    assert cookies[0].name == "hello"
    assert cookies[0].value == "world"
Example #4
0
def test_get_cookies_for_url():
    jar = CookieJar()

    jar.add(URL(b'https://foo.org'), Cookie(b'hello', b'world'))

    cookies = list(jar.get_cookies_for_url(
        URL(b'https://foo.org/hello-world')))

    assert len(cookies) == 1
    assert cookies[0].name == b'hello'
    assert cookies[0].value == b'world'
def test_cookie_jar_does_not_override_http_only_cookie_with_non_http_only_cookie(
):
    jar = CookieJar()

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=True,
        ),
    )

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world2",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=True,
        ),
    )

    cookie = jar.get(b"foo.org", b"/", b"hello")
    assert cookie is not None
    assert cookie.cookie.value == b"world2"

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world modified",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=False,
        ),
    )

    cookie = jar.get(b"foo.org", b"/", b"hello")
    assert cookie is not None
    assert cookie.cookie.value == b"world2"
def test_cookie_jar_check_cookies_removes_expired():
    jar = CookieJar()

    # simulate an expired cookie that gets removed
    jar._domain_cookies

    jar._host_only_cookies = {
        b"foo.org": {
            b"/": {
                b"hello":
                StoredCookie(
                    Cookie(b"hello",
                           b"world",
                           expires=b"Fri, 17 Aug 2018 20:55:04 GMT"))
            }
        }
    }

    list(
        jar._get_cookies_checking_exp(
            b"https", jar._host_only_cookies[b"foo.org"][b"/"]))
    assert jar.get(b"foo.org", b"/", b"hello") is None
Example #7
0
def test_cookie_jar_check_cookies_removes_expired():
    jar = CookieJar()

    # simulate an expired cookie that gets removed
    jar._domain_cookies

    jar._host_only_cookies = {
        "foo.org": {
            "/": {
                "hello": StoredCookie(
                    Cookie(
                        "hello",
                        "world",
                        expires=datetime_from_cookie_format(
                            b"Fri, 17 Aug 2018 20:55:04 GMT"
                        ),
                    )
                )
            }
        }
    }

    list(jar._get_cookies_checking_exp("https", jar._host_only_cookies["foo.org"]["/"]))
    assert jar.get("foo.org", "/", "hello") is None
Example #8
0
def test_cookiejar_invalid_domain(request_url, cookie_domain):
    jar = CookieJar()
    cookie = Cookie(b'Name', b'Value', domain=cookie_domain)

    with pytest.raises(InvalidCookieDomain):
        jar.add(request_url, cookie)
Example #9
0
def test_cookiejar_get_domain(request_url, cookie_domain, expected_domain):
    jar = CookieJar()
    cookie = Cookie(b'Name', b'Value', domain=cookie_domain)
    domain = jar.get_domain(request_url, cookie)
    assert domain == expected_domain
Example #10
0
def test_cookie_jar_throws_for_url_without_host():
    jar = CookieJar()

    with pytest.raises(MissingSchemeInURL):
        jar.get_cookies_for_url(URL(b"/"))
Example #11
0
def test_cookie_jar_remove_does_not_throw_key_error():
    jar = CookieJar()

    assert jar.remove(b"foo", b"foo", b"foo") is False
Example #12
0
def test_cookie_jar_get_cookie_default_path(value, expected_result):
    assert CookieJar.get_cookie_default_path(URL(value)) == expected_result
Example #13
0
def test_cookie_path_match(request_path: bytes, cookie_path: bytes,
                           is_match: bool):
    assert (CookieJar.path_match(request_path, cookie_path) is is_match
            ), f"{request_path.decode()} {cookie_path.decode()} != {is_match}"
Example #14
0
def test_cookie_domain_match(domain: bytes, value: bytes, is_match: bool):
    assert (CookieJar.domain_match(domain, value) is
            is_match), f"{domain.decode()} {value.decode()} != {is_match}"
Example #15
0
def test_cookie_domain_match(domain: str, value: str, is_match: bool):
    assert (
        CookieJar.domain_match(domain, value) is is_match
    ), f"{domain} {value} != {is_match}"