Exemplo n.º 1
0
def session_alt(event_loop):
    session = ClientSession(
        loop=event_loop,
        default_headers=[(b"X-Default-One", b"AAA"),
                         (b"X-Default-Two", b"BBB")],
    )
    yield session
    event_loop.run_until_complete(session.close())
Exemplo n.º 2
0
def test_session_raises_for_redirect_without_location():
    """
    If a server returns a redirect status without location response header,
    the client raises an exception.
    """

    with pytest.raises(MissingLocationForRedirect):
        ClientSession.extract_redirect_location(Response(http.HTTPStatus.FOUND.value))
Exemplo n.º 3
0
def test_check_permanent_redirects():
    client = ClientSession()
    client._permanent_redirects_urls._cache[b"/foo"] = URL(b"https://somewhere.org")

    request = Request("GET", b"/foo", None)
    assert request.url == URL(b"/foo")

    client.check_permanent_redirects(request)
    assert request.url == URL(b"https://somewhere.org")
Exemplo n.º 4
0
def session(server_host, server_port, event_loop):
    # It is important to pass the instance of ClientConnectionPools,
    # to ensure that the connections are reused and closed
    session = ClientSession(
        loop=event_loop,
        base_url=f"http://{server_host}:{server_port}",
        pools=ClientConnectionPools(event_loop),
    )
    yield session
    asyncio.run(session.close())
Exemplo n.º 5
0
def test_update_request_for_redirect_raises_for_urn_redirects():
    client = ClientSession()

    with pytest.raises(UnsupportedRedirect) as redirect_exception:
        client.update_request_for_redirect(
            Request("GET", b"/foo", None),
            Response(302, [
                (b"Location", b"urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66")
            ]),
        )

    assert (redirect_exception.value.redirect_url ==
            b"urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66")
Exemplo n.º 6
0
async def test_client_session_validate_url_for_relative_urls_with_base_url():
    async with ClientSession(base_url=b"https://foo.org") as client:
        request = Request("GET", b"/home", None)

        client._validate_request_url(request)

        assert request.url == URL(b"https://foo.org/home")
Exemplo n.º 7
0
async def test_cookies_jar_single_cookie():
    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!'))
    ])
    check_cookie = False

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

        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'/'
        )  # the first request doesn't have any cookie because the response will set;
        check_cookie = True
        await client.get(b'/')
Exemplo n.º 8
0
async def test_maximum_number_of_redirects_detection(responses, maximum_redirects, pools_factory):

    async with ClientSession(url=b'http://localhost:8080', pools=pools_factory(responses)) as client:
        client.maximum_redirects = maximum_redirects

        with pytest.raises(MaximumRedirectsExceededError):
            await client.get(b'/')
Exemplo n.º 9
0
async def test_multiple_middleware():
    fake_pools = FakePools([Response(200, None, TextContent("Hello, World!"))])

    steps = []

    async def middleware_one(request, next_handler):
        steps.append(1)
        response = await next_handler(request)
        steps.append(2)
        return response

    async def middleware_two(request, next_handler):
        steps.append(3)
        response = await next_handler(request)
        steps.append(4)
        return response

    async def middleware_three(request, next_handler):
        steps.append(5)
        response = await next_handler(request)
        steps.append(6)
        return response

    async with ClientSession(
            base_url=b"http://localhost:8080",
            pools=fake_pools,
            middlewares=[middleware_one, middleware_two, middleware_three],
    ) as client:
        response = await client.get(b"/")

        assert steps == [1, 3, 5, 6, 4, 2]
        assert response.status == 200
        text = await response.text()
        assert text == "Hello, World!"
Exemplo n.º 10
0
async def test_cookies_jar_single_cookie():
    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!")),
    ])
    check_cookie = False

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

        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"/"
        )  # the first request doesn't have any cookie because the response will set;
        check_cookie = True
        await client.get(b"/")
Exemplo n.º 11
0
async def test_cookies_jar(first_request_url, second_request_url, set_cookies,
                           expected_cookies):
    fake_pools = FakePools([
        Response(200, Headers(set_cookies), TextContent('Hello, World!')),
        Response(200, Headers(), TextContent('Hello!'))
    ])
    check_cookie = False

    async def middleware_for_assertions(request, next_handler):
        if check_cookie:
            if not expected_cookies:
                assert not request.cookies

            for expected_cookie in expected_cookies:
                cookie = request.cookies.get(expected_cookie)
                assert cookie is not None, f'{cookie.name.decode()} cookie must be configured for following requests'

        return await next_handler(request)

    async with ClientSession(
            pools=fake_pools,
            middlewares=[middleware_for_assertions],
    ) as client:
        await client.get(first_request_url)
        check_cookie = True
        await client.get(second_request_url)
Exemplo n.º 12
0
async def test_circular_redirect_detection(responses, expected_error_message, pools_factory):

    async with ClientSession(url=b'http://localhost:8080', pools=pools_factory(responses)) as client:

        with pytest.raises(CircularRedirectError) as error:
            await client.get(b'/')

        assert str(error.value) == expected_error_message
Exemplo n.º 13
0
async def test_non_url_redirect(responses, expected_status, expected_location, pools_factory):

    async with ClientSession(url=b'http://localhost:8080', pools=pools_factory(responses)) as client:
        response = await client.get(b'/')

        assert response is not None
        assert response.status == expected_status

        location_header = response.headers.get_single(b'Location')
        assert location_header.value == expected_location
Exemplo n.º 14
0
async def test_connection_timeout():
    fake_pools = FakePools([])
    fake_pools.pool.sleep_for = 5  # wait for 5 seconds before returning a connection; to test timeout handling

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             connection_timeout=0.002  # 2ms - not realistic, but ok for this test
                             ) as client:
        with pytest.raises(ConnectionTimeout):
            await client.get(b'/')
Exemplo n.º 15
0
async def test_request_timeout():
    fake_pools = FakePools([])
    fake_pools.pool.connection.sleep_for = 5  # wait for 5 seconds before returning a response;

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             request_timeout=0.002  # 2ms - not realistic, but ok for this test
                             ) as client:
        with pytest.raises(RequestTimeout):
            await client.get(b'/')
Exemplo n.º 16
0
async def test_good_redirect(responses, expected_response_body, pools_factory):

    async with ClientSession(url=b'http://localhost:8080', pools=pools_factory(responses)) as client:
        response = await client.get(b'/')

        assert response is not None
        assert response.status == 200

        content = await response.text()
        assert content == expected_response_body
Exemplo n.º 17
0
async def test_query_params_concatenation(request_url, params, expected_query):
    fake_pools = FakePools([Response(200, None, TextContent('Hello, World!'))])

    async def middleware_for_assertions(request, next_handler):
        assert expected_query == request.url.query
        return await next_handler(request)

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_for_assertions
                                          ]) as client:
        await client.get(request_url, params=params)
Exemplo n.º 18
0
async def test_not_follow_redirect(responses, expected_location, pools_factory):

    async with ClientSession(url=b'http://localhost:8080',
                             pools=pools_factory(responses),
                             follow_redirects=False) as client:
        response = await client.get(b'/')

        assert response.status == 302

        location = response.headers[b'location']
        assert location
        assert location[0].value == expected_location
Exemplo n.º 19
0
async def test_not_follow_redirect(responses, expected_location, pools_factory):

    async with ClientSession(
        base_url=b"http://localhost:8080",
        pools=pools_factory(responses),
        follow_redirects=False,
    ) as client:
        response = await client.get(b"/")

        assert response.status == 302

        location = response.headers[b"location"]
        assert location
        assert location[0] == expected_location
Exemplo n.º 20
0
async def test_client_session_without_middlewares_and_cookiejar():
    called = False

    async def monkey_send_core(request):
        nonlocal called
        called = True

    async with ClientSession(cookie_jar=False) as client:
        assert client._handler is None
        assert not client.middlewares

        client._send_core = monkey_send_core  # type: ignore

        await client.send(Request("GET", b"https://somewhere.org", None))

        assert called is True
Exemplo n.º 21
0
async def test_request_headers():
    fake_pools = FakePools([Response(200, [], TextContent('Hello, World!'))])

    async def middleware_for_assertions(request, next_handler):
        assert b'Hello' in request.headers
        assert request.headers.get_single(b'Hello') == b'World'

        return await next_handler(request)

    async with ClientSession(base_url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_for_assertions
                                          ]) as client:
        await client.get(b'/', headers=[(b'Hello', b'World')])
        await client.post(b'/', headers=[(b'Hello', b'World')])
        await client.put(b'/', headers=[(b'Hello', b'World')])
        await client.delete(b'/', headers=[(b'Hello', b'World')])
Exemplo n.º 22
0
async def test_request_headers():
    fake_pools = FakePools([Response(200, [], TextContent("Hello, World!"))])

    async def middleware_for_assertions(request, next_handler):
        assert b"Hello" in request.headers
        assert request.headers.get_single(b"Hello") == b"World"

        return await next_handler(request)

    async with ClientSession(
        base_url=b"http://localhost:8080",
        pools=fake_pools,
        middlewares=[middleware_for_assertions],
    ) as client:
        await client.get(b"/", headers=[(b"Hello", b"World")])
        await client.post(b"/", headers=[(b"Hello", b"World")])
        await client.put(b"/", headers=[(b"Hello", b"World")])
        await client.delete(b"/", headers=[(b"Hello", b"World")])
Exemplo n.º 23
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
Exemplo n.º 24
0
async def test_remove_cookie_with_max_age():
    expire_cookie = Cookie("X-Foo", "Foo")
    expire_cookie.max_age = 0
    fake_pools = FakePools(
        [
            Response(
                200,
                [(b"Set-Cookie", write_response_cookie(Cookie("X-Foo", "Foo")))],
                TextContent("Hello, World!"),
            ),
            Response(200, None, TextContent("Hello!")),
            Response(
                200,
                [(b"Set-Cookie", write_response_cookie(expire_cookie))],
                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
Exemplo n.º 25
0
async def test_middlewares_can_be_applied_multiple_times_without_changing():
    fake_pools = FakePools(
        [Response(200, Headers(), TextContent('Hello, World!'))])

    steps = []

    async def middleware_one(request, next_handler):
        steps.append(1)
        response = await next_handler(request)
        steps.append(2)
        return response

    async def middleware_two(request, next_handler):
        steps.append(3)
        response = await next_handler(request)
        steps.append(4)
        return response

    async def middleware_three(request, next_handler):
        steps.append(5)
        response = await next_handler(request)
        steps.append(6)
        return response

    async with ClientSession(url=b'http://localhost:8080',
                             pools=fake_pools) as client:
        client.add_middlewares([middleware_one])
        client.add_middlewares([middleware_two])
        client.add_middlewares([middleware_three])

        assert middleware_one in client._middlewares
        assert middleware_two in client._middlewares
        assert middleware_three in client._middlewares

        client._build_middlewares_chain()

        response = await client.get(b'/')

        assert steps == [1, 3, 5, 6, 4, 2]
        assert response.status == 200
        text = await response.text()
        assert text == 'Hello, World!'
Exemplo n.º 26
0
async def test_query_params(params, expected_query):
    fake_pools = FakePools([Response(200, None, TextContent("Hello, World!"))])

    async def middleware_for_assertions(request, next_handler):
        assert expected_query == request.url.query
        return await next_handler(request)

    async with ClientSession(
            base_url=b"http://localhost:8080",
            pools=fake_pools,
            middlewares=[middleware_for_assertions],
    ) as client:
        await client.get(b"/", params=params)
        await client.head(b"/", params=params)
        await client.post(b"/", params=params)
        await client.put(b"/", params=params)
        await client.patch(b"/", params=params)
        await client.delete(b"/", params=params)
        await client.options(b"/", params=params)
        await client.trace(b"/", params=params)
Exemplo n.º 27
0
async def test_default_headers():
    fake_pools = FakePools(
        [Response(200, Headers(), TextContent('Hello, World!'))])

    async def middleware_for_assertions(request, next_handler):
        assert b'hello' in request.headers
        assert request.headers.get_single(b'hello').value == b'World'

        assert b'Foo' in request.headers
        assert request.headers.get_single(b'Foo').value == b'Power'

        return await next_handler(request)

    async with ClientSession(url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_for_assertions],
                             default_headers=[
                                 Header(b'Hello', b'World'),
                                 Header(b'Foo', b'Power')
                             ]) as client:
        await client.get(b'/')
Exemplo n.º 28
0
async def test_single_middleware():
    fake_pools = FakePools(
        [Response(200, Headers(), TextContent('Hello, World!'))])

    steps = []

    async def middleware_one(request, next_handler):
        steps.append(1)
        response = await next_handler(request)
        steps.append(2)
        return response

    async with ClientSession(url=b'http://localhost:8080',
                             pools=fake_pools,
                             middlewares=[middleware_one]) as client:
        response = await client.get(b'/')

        assert steps == [1, 2]
        assert response.status == 200
        text = await response.text()
        assert text == 'Hello, World!'
Exemplo n.º 29
0
async def test_falsy_middleware():
    fake_pools = FakePools([Response(200, None, TextContent("Hello, World!"))])

    steps = []

    async def middleware_one(request, next_handler):
        steps.append(1)
        response = await next_handler(request)
        steps.append(2)
        return response

    async with ClientSession(
            base_url=b"http://localhost:8080",
            pools=fake_pools,
            middlewares=[middleware_one, None, False],  # type: ignore
    ) as client:
        response = await client.get(b"/")

        assert steps == [1, 2]
        assert response.status == 200
        text = await response.text()
        assert text == "Hello, World!"
Exemplo n.º 30
0
def session_alt(event_loop):
    session = ClientSession(loop=event_loop,
                            default_headers=[(b'X-Default-One', b'AAA'),
                                             (b'X-Default-Two', b'BBB')])
    yield session
    event_loop.run_until_complete(session.close())