Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

103 lines · 2.9 KiB · Python

Last changed by

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–14why, one step back
1import pytest
23import httpcore456def test_host_header():7 request = httpcore.Request("GET", "http://example.org")8 request.prepare()9 assert request.headers == httpcore.Headers(10 [(b"host", b"example.org"), (b"accept-encoding", b"deflate, gzip, br")]11 )121314def test_content_length_header():

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 15why, one step back
15 request = httpcore.Request("POST", "http://example.org", content=b"test 123")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 16–29why, one step back
16 request.prepare()
17 assert request.headers == httpcore.Headers(18 [19 (b"host", b"example.org"),20 (b"content-length", b"8"),21 (b"accept-encoding", b"deflate, gzip, br"),22 ]23 )242526def test_transfer_encoding_header():27 async def streaming_body(data):28 yield data # pragma: nocover29

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 30why, one step back
30 content = streaming_body(b"test 123")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 31why, one step back
31

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 32why, one step back
32 request = httpcore.Request("POST", "http://example.org", content=content)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 33–66why, one step back
33 request.prepare()
34 assert request.headers == httpcore.Headers(35 [36 (b"host", b"example.org"),37 (b"transfer-encoding", b"chunked"),38 (b"accept-encoding", b"deflate, gzip, br"),39 ]40 )414243def test_override_host_header():44 headers = [(b"host", b"1.2.3.4:80")]4546 request = httpcore.Request("GET", "http://example.org", headers=headers)47 request.prepare()48 assert request.headers == httpcore.Headers(49 [(b"accept-encoding", b"deflate, gzip, br"), (b"host", b"1.2.3.4:80")]50 )515253def test_override_accept_encoding_header():54 headers = [(b"accept-encoding", b"identity")]5556 request = httpcore.Request("GET", "http://example.org", headers=headers)57 request.prepare()58 assert request.headers == httpcore.Headers(59 [(b"host", b"example.org"), (b"accept-encoding", b"identity")]60 )616263def test_override_content_length_header():64 async def streaming_body(data):65 yield data # pragma: nocover66

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 67why, one step back
67 content = streaming_body(b"test 123")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 68–69why, one step back
68 headers = [(b"content-length", b"8")]
69

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

lines 70–72why, one step back
70 request = httpcore.Request(
71 "POST", "http://example.org", content=content, headers=headers72 )

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 73–95why, one step back
73 request.prepare()
74 assert request.headers == httpcore.Headers(75 [76 (b"host", b"example.org"),77 (b"accept-encoding", b"deflate, gzip, br"),78 (b"content-length", b"8"),79 ]80 )818283def test_url():84 request = httpcore.Request("GET", "http://example.org")85 assert request.url.scheme == "http"86 assert request.url.port == 8087 assert request.url.full_path == "/"8889 request = httpcore.Request("GET", "https://example.org/abc?foo=bar")90 assert request.url.scheme == "https"91 assert request.url.port == 44392 assert request.url.full_path == "/abc?foo=bar"939495def test_invalid_urls():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 96why, one step back
96 with pytest.raises(httpcore.InvalidURL):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 97–98why, one step back
97 httpcore.Request("GET", "example.org")
98

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 99why, one step back
99 with pytest.raises(httpcore.InvalidURL):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 100–101why, one step back
100 httpcore.Request("GET", "invalid://example.org")
101

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 102why, one step back
102 with pytest.raises(httpcore.InvalidURL):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 103why, one step back
103 httpcore.Request("GET", "http:///foo")