Skip to content
Myr

test_requests.py

This file

Path

tests/test_requests.py

92 lines · 2.5 KiB · Python

Last changed by

Exclude unused code linesTom Christiebefore adoptionee6f42a

History of this file

Take it

1import pytest23import httpcore456def test_host_header():7    request = httpcore.Request("GET", "http://example.org")8    assert request.headers == [9        (b"host", b"example.org"),10        (b"accept-encoding", b"deflate, gzip, br"),11    ]121314def test_content_length_header():15    request = httpcore.Request("POST", "http://example.org", body=b"test 123")16    assert request.headers == [17        (b"host", b"example.org"),18        (b"content-length", b"8"),19        (b"accept-encoding", b"deflate, gzip, br"),20    ]212223def test_transfer_encoding_header():24    async def streaming_body(data):25        yield data  # pragma: nocover2627    body = streaming_body(b"test 123")2829    request = httpcore.Request("POST", "http://example.org", body=body)30    assert request.headers == [31        (b"host", b"example.org"),32        (b"transfer-encoding", b"chunked"),33        (b"accept-encoding", b"deflate, gzip, br"),34    ]353637def test_override_host_header():38    headers = [(b"host", b"1.2.3.4:80")]3940    request = httpcore.Request("GET", "http://example.org", headers=headers)41    assert request.headers == [42        (b"accept-encoding", b"deflate, gzip, br"),43        (b"host", b"1.2.3.4:80"),44    ]454647def test_override_accept_encoding_header():48    headers = [(b"accept-encoding", b"identity")]4950    request = httpcore.Request("GET", "http://example.org", headers=headers)51    assert request.headers == [52        (b"host", b"example.org"),53        (b"accept-encoding", b"identity"),54    ]555657def test_override_content_length_header():58    async def streaming_body(data):59        yield data  # pragma: nocover6061    body = streaming_body(b"test 123")62    headers = [(b"content-length", b"8")]6364    request = httpcore.Request("POST", "http://example.org", body=body, headers=headers)65    assert request.headers == [66        (b"host", b"example.org"),67        (b"accept-encoding", b"deflate, gzip, br"),68        (b"content-length", b"8"),69    ]707172def test_url():73    request = httpcore.Request("GET", "http://example.org")74    assert request.url.scheme == "http"75    assert request.url.port == 8076    assert request.url.target == "/"7778    request = httpcore.Request("GET", "https://example.org/abc?foo=bar")79    assert request.url.scheme == "https"80    assert request.url.port == 44381    assert request.url.target == "/abc?foo=bar"828384def test_invalid_urls():85    with pytest.raises(ValueError):86        httpcore.Request("GET", "example.org")8788    with pytest.raises(ValueError):89        httpcore.Request("GET", "invalid://example.org")9091    with pytest.raises(ValueError):92        httpcore.Request("GET", "http:///foo")