Пример #1
0
async def test_same_domain_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/cross_domain"
    headers = {"Authorization": "abc"}
    response = await client.get(url, headers=headers)
    assert response.url == URL("https://example.org/cross_domain_target")
    assert response.json()["headers"]["authorization"] == "abc"
Пример #2
0
async def test_body_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/redirect_body"
    data = b"Example request body"
    response = await client.post(url, data=data)
    assert response.url == URL("https://example.org/redirect_body_target")
    assert response.json() == {"body": "Example request body"}
Пример #3
0
async def test_fragment_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.get(
        "https://example.org/relative_redirect#fragment")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/#fragment")
    assert len(response.history) == 1
Пример #4
0
async def test_redirect_loop_calling_next():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/redirect_loop"
    response = await client.get(url, allow_redirects=False)
    with pytest.raises(RedirectLoop):
        while response.is_redirect:
            response = await response.next()
Пример #5
0
async def test_too_many_redirects_calling_next():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/multiple_redirects?count=21"
    response = await client.get(url, allow_redirects=False)
    with pytest.raises(TooManyRedirects):
        while response.is_redirect:
            response = await response.next()
Пример #6
0
async def test_multiple_redirects():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.get(
        "https://example.org/multiple_redirects?count=20")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/multiple_redirects")
    assert len(response.history) == 20
Пример #7
0
async def test_connect_timeout(server):
    timeout = TimeoutConfig(connect_timeout=0.000001)

    async with AsyncClient(timeout=timeout) as client:
        with pytest.raises(ConnectTimeout):
            # See https://stackoverflow.com/questions/100841/
            await client.get("http://10.255.255.1/")
Пример #8
0
async def test_write_timeout(server):
    timeout = TimeoutConfig(write_timeout=0.000001)

    async with AsyncClient(timeout=timeout) as client:
        with pytest.raises(WriteTimeout):
            data = b"*" * 1024 * 1024 * 100
            await client.put("http://127.0.0.1:8000/slow_response", data=data)
Пример #9
0
async def test_cannot_redirect_streaming_body():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/redirect_body"

    async def streaming_body():
        yield b"Example request body"

    with pytest.raises(RedirectBodyUnavailable):
        await client.post(url, data=streaming_body())
Пример #10
0
async def test_pool_timeout(server):
    pool_limits = PoolLimits(hard_limit=1, pool_timeout=0.000001)

    async with AsyncClient(pool_limits=pool_limits) as client:
        response = await client.get("http://127.0.0.1:8000/", stream=True)

        with pytest.raises(PoolTimeout):
            await client.get("http://localhost:8000/")

        await response.read()
Пример #11
0
async def test_disallow_redirects():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.post("https://example.org/redirect_303",
                                 allow_redirects=False)
    assert response.status_code == codes.SEE_OTHER
    assert response.url == URL("https://example.org/redirect_303")
    assert len(response.history) == 0

    response = await response.next()
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1
Пример #12
0
async def test_redirect_301():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.post("https://example.org/redirect_301")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1
Пример #13
0
async def test_redirect_loop():
    client = AsyncClient(dispatch=MockDispatch())
    with pytest.raises(RedirectLoop):
        await client.get("https://example.org/redirect_loop")
Пример #14
0
async def test_too_many_redirects():
    client = AsyncClient(dispatch=MockDispatch())
    with pytest.raises(TooManyRedirects):
        await client.get("https://example.org/multiple_redirects?count=21")
Пример #15
0
async def test_read_timeout(server):
    timeout = TimeoutConfig(read_timeout=0.000001)

    async with AsyncClient(timeout=timeout) as client:
        with pytest.raises(ReadTimeout):
            await client.get("http://127.0.0.1:8000/slow_response")