示例#1
0
def test_set_cookie_with_cookiejar():
    """
    Send a request including a cookie, using a `CookieJar` instance.
    """

    url = "http://example.org/echo_cookies"
    cookies = CookieJar()
    cookie = Cookie(
        version=0,
        name="example-name",
        value="example-value",
        port=None,
        port_specified=False,
        domain="",
        domain_specified=False,
        domain_initial_dot=False,
        path="/",
        path_specified=True,
        secure=False,
        expires=None,
        discard=True,
        comment=None,
        comment_url=None,
        rest={"HttpOnly": None},
        rfc2109=False,
    )
    cookies.set_cookie(cookie)

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url, cookies=cookies)

    assert response.status_code == 200
    assert response.json() == {"cookies": "example-name=example-value"}
示例#2
0
async def test_connect_timeout(server):
    timeout = TimeoutConfig(connect_timeout=0.0001)

    async with Client(timeout=timeout) as client:
        with pytest.raises(ConnectTimeout):
            # See https://stackoverflow.com/questions/100841/
            await client.get("http://10.255.255.1/")
示例#3
0
def test_http2_multiple_requests():
    backend = MockHTTP2Backend(app=app)

    with Client(backend=backend) as client:
        response_1 = client.get("http://example.org/1")
        response_2 = client.get("http://example.org/2")
        response_3 = client.get("http://example.org/3")

    assert response_1.status_code == 200
    assert json.loads(response_1.content) == {
        "method": "GET",
        "path": "/1",
        "body": ""
    }

    assert response_2.status_code == 200
    assert json.loads(response_2.content) == {
        "method": "GET",
        "path": "/2",
        "body": ""
    }

    assert response_3.status_code == 200
    assert json.loads(response_3.content) == {
        "method": "GET",
        "path": "/3",
        "body": ""
    }
示例#4
0
def test_get_cookie():
    url = "http://example.org/set_cookie"

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url)

    assert response.status_code == 200
    assert response.cookies["example-name"] == "example-value"
    assert client.cookies["example-name"] == "example-value"
示例#5
0
def test_basic_auth_in_url():
    url = "https://*****:*****@example.org/"

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url)

    assert response.status_code == 200
    assert response.json() == {
        "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="
    }
示例#6
0
async def test_pool_timeout(server):
    pool_limits = PoolLimits(hard_limit=1, pool_timeout=0.0001)

    async with Client(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()
示例#7
0
def test_basic_auth_on_session():
    url = "https://example.org/"
    auth = ("tomchristie", "password123")

    with Client(dispatch=MockDispatch(), auth=auth) as client:
        response = client.get(url)

    assert response.status_code == 200
    assert response.json() == {
        "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="
    }
示例#8
0
def test_custom_auth():
    url = "https://example.org/"

    def auth(request):
        request.headers["Authorization"] = "Token 123"
        return request

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url, auth=auth)

    assert response.status_code == 200
    assert response.json() == {"auth": "Token 123"}
示例#9
0
def test_set_cookie():
    """
    Send a request including a cookie.
    """
    url = "http://example.org/echo_cookies"
    cookies = {"example-name": "example-value"}

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url, cookies=cookies)

    assert response.status_code == 200
    assert response.json() == {"cookies": "example-name=example-value"}
示例#10
0
def test_http2_post_request():
    backend = MockHTTP2Backend(app=app)

    with Client(backend=backend) as client:
        response = client.post("http://example.org", data=b"<data>")

    assert response.status_code == 200
    assert json.loads(response.content) == {
        "method": "POST",
        "path": "/",
        "body": "<data>",
    }
示例#11
0
def test_set_cookie_with_cookies_model():
    """
    Send a request including a cookie, using a `Cookies` instance.
    """

    url = "http://example.org/echo_cookies"
    cookies = Cookies()
    cookies["example-name"] = "example-value"

    with Client(dispatch=MockDispatch()) as client:
        response = client.get(url, cookies=cookies)

    assert response.status_code == 200
    assert response.json() == {"cookies": "example-name=example-value"}
示例#12
0
def test_cookie_persistence():
    """
    Ensure that Client instances persist cookies between requests.
    """
    with Client(dispatch=MockDispatch()) as client:
        response = client.get("http://example.org/echo_cookies")
        assert response.status_code == 200
        assert response.json() == {"cookies": None}

        response = client.get("http://example.org/set_cookie")
        assert response.status_code == 200
        assert response.cookies["example-name"] == "example-value"
        assert client.cookies["example-name"] == "example-value"

        response = client.get("http://example.org/echo_cookies")
        assert response.status_code == 200
        assert response.json() == {"cookies": "example-name=example-value"}
示例#13
0
async def test_read_timeout(server):
    timeout = TimeoutConfig(read_timeout=0.0001)

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