Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

120 lines · 3.5 KiB · Python

Last changed by

Support url-encoded data in requestsTom Christiebefore adoptione91c69d

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–5why, one step back
1import pytest
23import httpcore45

Increase test coverageTom Christiebefore adoptione99ffbd

lines 6–10why, one step back
6def test_request_repr():
7 request = httpcore.Request("GET", "http://example.org")8 assert repr(request) == "<Request('GET', 'http://example.org')>"910

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 11–19why, one step back
11def test_host_header():
12 request = httpcore.Request("GET", "http://example.org")13 request.prepare()14 assert request.headers == httpcore.Headers(15 [(b"host", b"example.org"), (b"accept-encoding", b"deflate, gzip, br")]16 )171819def test_content_length_header():

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 21–30why, one step back
21 request.prepare()
22 assert request.headers == httpcore.Headers(23 [24 (b"host", b"example.org"),25 (b"content-length", b"8"),26 (b"accept-encoding", b"deflate, gzip, br"),27 ]28 )2930

Support url-encoded data in requestsTom Christiebefore adoptione91c69d

lines 31–44why, one step back
31def test_url_encoded_data():
32 request = httpcore.Request("POST", "http://example.org", data={"test": "123"})33 request.prepare()34 assert request.headers == httpcore.Headers(35 [36 (b"host", b"example.org"),37 (b"content-length", b"8"),38 (b"accept-encoding", b"deflate, gzip, br"),39 (b"content-type", b"application/x-www-form-urlencoded"),40 ]41 )42 assert request.content == b"test=123"4344

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 45–48why, one step back
45def test_transfer_encoding_header():
46 async def streaming_body(data):47 yield data # pragma: nocover48

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 49why, one step back
49 data = streaming_body(b"test 123")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 50why, one step back
50

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 52–85why, one step back
52 request.prepare()
53 assert request.headers == httpcore.Headers(54 [55 (b"host", b"example.org"),56 (b"transfer-encoding", b"chunked"),57 (b"accept-encoding", b"deflate, gzip, br"),58 ]59 )606162def test_override_host_header():63 headers = [(b"host", b"1.2.3.4:80")]6465 request = httpcore.Request("GET", "http://example.org", headers=headers)66 request.prepare()67 assert request.headers == httpcore.Headers(68 [(b"accept-encoding", b"deflate, gzip, br"), (b"host", b"1.2.3.4:80")]69 )707172def test_override_accept_encoding_header():73 headers = [(b"accept-encoding", b"identity")]7475 request = httpcore.Request("GET", "http://example.org", headers=headers)76 request.prepare()77 assert request.headers == httpcore.Headers(78 [(b"host", b"example.org"), (b"accept-encoding", b"identity")]79 )808182def test_override_content_length_header():83 async def streaming_body(data):84 yield data # pragma: nocover85

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 86why, one step back
86 data = streaming_body(b"test 123")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Support url-encoded data in requestsTom Christiebefore adoptione91c69d

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 90–112why, one step back
90 request.prepare()
91 assert request.headers == httpcore.Headers(92 [93 (b"host", b"example.org"),94 (b"accept-encoding", b"deflate, gzip, br"),95 (b"content-length", b"8"),96 ]97 )9899100def test_url():101 request = httpcore.Request("GET", "http://example.org")102 assert request.url.scheme == "http"103 assert request.url.port == 80104 assert request.url.full_path == "/"105106 request = httpcore.Request("GET", "https://example.org/abc?foo=bar")107 assert request.url.scheme == "https"108 assert request.url.port == 443109 assert request.url.full_path == "/abc?foo=bar"110111112def test_invalid_urls():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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