test_requests.py
This file
Path
tests/test_requests.py
95 lines · 2.7 KiB · Python
Last changed by
Rollin'Tom Christiebefore adoption8a29a0a
History of this fileTake it
1import pytest23import httpcore456def test_host_header():7 request = httpcore.Request("GET", "http://example.org")8 assert request.headers == httpcore.Headers(9 [(b"host", b"example.org"), (b"accept-encoding", b"deflate, gzip, br")]10 )111213def test_content_length_header():14 request = httpcore.Request("POST", "http://example.org", body=b"test 123")15 assert request.headers == httpcore.Headers(16 [17 (b"host", b"example.org"),18 (b"content-length", b"8"),19 (b"accept-encoding", b"deflate, gzip, br"),20 ]21 )222324def test_transfer_encoding_header():25 async def streaming_body(data):26 yield data # pragma: nocover2728 body = streaming_body(b"test 123")2930 request = httpcore.Request("POST", "http://example.org", body=body)31 assert request.headers == httpcore.Headers(32 [33 (b"host", b"example.org"),34 (b"transfer-encoding", b"chunked"),35 (b"accept-encoding", b"deflate, gzip, br"),36 ]37 )383940def test_override_host_header():41 headers = [(b"host", b"1.2.3.4:80")]4243 request = httpcore.Request("GET", "http://example.org", headers=headers)44 assert request.headers == httpcore.Headers(45 [(b"accept-encoding", b"deflate, gzip, br"), (b"host", b"1.2.3.4:80")]46 )474849def test_override_accept_encoding_header():50 headers = [(b"accept-encoding", b"identity")]5152 request = httpcore.Request("GET", "http://example.org", headers=headers)53 assert request.headers == httpcore.Headers(54 [(b"host", b"example.org"), (b"accept-encoding", b"identity")]55 )565758def test_override_content_length_header():59 async def streaming_body(data):60 yield data # pragma: nocover6162 body = streaming_body(b"test 123")63 headers = [(b"content-length", b"8")]6465 request = httpcore.Request("POST", "http://example.org", body=body, headers=headers)66 assert request.headers == httpcore.Headers(67 [68 (b"host", b"example.org"),69 (b"accept-encoding", b"deflate, gzip, br"),70 (b"content-length", b"8"),71 ]72 )737475def test_url():76 request = httpcore.Request("GET", "http://example.org")77 assert request.url.scheme == "http"78 assert request.url.port == 8079 assert request.url.full_path == "/"8081 request = httpcore.Request("GET", "https://example.org/abc?foo=bar")82 assert request.url.scheme == "https"83 assert request.url.port == 44384 assert request.url.full_path == "/abc?foo=bar"858687def test_invalid_urls():88 with pytest.raises(ValueError):89 httpcore.Request("GET", "example.org")9091 with pytest.raises(ValueError):92 httpcore.Request("GET", "invalid://example.org")9394 with pytest.raises(ValueError):95 httpcore.Request("GET", "http:///foo")