Beispiel #1
0
def test_stored_cookie_max_age_precedence():
    cookie = Cookie(b'X-Foo', b'Foo')
    cookie.set_max_age(0)
    cookie.expiration = datetime.utcnow() + timedelta(days=2)

    stored_cookie = StoredCookie(cookie)
    assert stored_cookie.is_expired()
Beispiel #2
0
async def test_remove_cookie_with_expiration():
    expire_cookie = Cookie(b'X-Foo', b'Foo')
    expire_cookie.expiration = datetime.utcnow() + timedelta(days=-2)
    fake_pools = FakePools([
        Response(
            200,
            Headers([
                Header(b'Set-Cookie',
                       write_response_cookie(Cookie(b'X-Foo', b'Foo')))
            ]), TextContent('Hello, World!')),
        Response(200, Headers(), TextContent('Hello!')),
        Response(
            200,
            Headers(
                [Header(b'Set-Cookie', write_response_cookie(expire_cookie))]),
            TextContent('Hello, World!')),
        Response(200, Headers(), TextContent('Hello!'))
    ])
    expect_cookie = False

    async def middleware_for_assertions(request, next_handler):
        cookie = request.cookies.get(b'X-Foo')
        if expect_cookie:
            assert cookie is not None, 'X-Foo cookie must be configured'
        else:
            assert cookie is None

        return await next_handler(request)

    async with ClientSession(url=b'https://bezkitu.org',
                             pools=fake_pools,
                             middlewares=[middleware_for_assertions
                                          ]) as client:
        await client.get(b'/')  # <-- cookie set here
        expect_cookie = True
        await client.get(b'/')  # <-- expect cookie in request
        expect_cookie = True
        await client.get(
            b'/')  # <-- expect cookie in request; it gets removed here
        expect_cookie = False
        await client.get(
            b'/'
        )  # <-- expect missing cookie; was deleted by previous response
async def test_remove_cookie_with_expiration():
    expire_cookie = Cookie(b"X-Foo", b"Foo")
    expire_cookie.expiration = datetime.utcnow() + timedelta(days=-2)
    fake_pools = FakePools([
        Response(200, [
            (b"Set-Cookie", write_response_cookie(Cookie(b"X-Foo", b"Foo")))
        ]).with_content(TextContent("Hello, World!")),
        Response(200, None, TextContent("Hello!")),
        Response(200, [(b"Set-Cookie", write_response_cookie(expire_cookie))
                       ]).with_content(TextContent("Hello, World!")),
        Response(200, None, TextContent("Hello!")),
    ])
    expect_cookie = False

    async def middleware_for_assertions(request, next_handler):
        cookie = request.cookies.get("X-Foo")
        if expect_cookie:
            assert cookie is not None, "X-Foo cookie must be configured"
        else:
            assert cookie is None

        return await next_handler(request)

    async with ClientSession(
            base_url=b"https://bezkitu.org",
            pools=fake_pools,
            middlewares=[middleware_for_assertions],
    ) as client:
        await client.get(b"/")  # <-- cookie set here
        expect_cookie = True
        await client.get(b"/")  # <-- expect cookie in request
        expect_cookie = True
        await client.get(
            b"/")  # <-- expect cookie in request; it gets removed here
        expect_cookie = False
        await client.get(
            b"/"
        )  # <-- expect missing cookie; was deleted by previous response