Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

118 lines · 3.3 KiB · Python

Last changed by

Drop Host header for HTTP/2 (#82)Tom Christiebefore adoption76bc6c6

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

Drop Host header for HTTP/2 (#82)Tom Christiebefore adoption76bc6c6

line 11why, one step back
11def test_no_content():

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 12–14why, one step back
12 request = httpcore.Request("GET", "http://example.org")
13 request.prepare()14 assert request.headers == httpcore.Headers(

Drop Host header for HTTP/2 (#82)Tom Christiebefore adoption76bc6c6

line 15why, one step back
15 [(b"accept-encoding", b"deflate, gzip, br")]

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 16–19why, one step back
16 )
171819def test_content_length_header():

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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–29why, one step back
21 request.prepare()
22 assert request.headers == httpcore.Headers(23 [24 (b"content-length", b"8"),25 (b"accept-encoding", b"deflate, gzip, br"),26 ]27 )2829

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 43–46why, one step back
43def test_transfer_encoding_header():
44 async def streaming_body(data):45 yield data # pragma: nocover46

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 48why, one step back
48

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 50–74why, one step back
50 request.prepare()
51 assert request.headers == httpcore.Headers(52 [53 (b"transfer-encoding", b"chunked"),54 (b"accept-encoding", b"deflate, gzip, br"),55 ]56 )575859def test_override_host_header():60 headers = [(b"host", b"1.2.3.4:80")]6162 request = httpcore.Request("GET", "http://example.org", headers=headers)63 request.prepare()64 assert request.headers == httpcore.Headers(65 [(b"accept-encoding", b"deflate, gzip, br"), (b"host", b"1.2.3.4:80")]66 )676869def test_override_accept_encoding_header():70 headers = [(b"accept-encoding", b"identity")]7172 request = httpcore.Request("GET", "http://example.org", headers=headers)73 request.prepare()74 assert request.headers == httpcore.Headers(

Drop Host header for HTTP/2 (#82)Tom Christiebefore adoption76bc6c6

line 75why, one step back
75 [(b"accept-encoding", b"identity")]

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 76–82why, one step back
76 )
777879def test_override_content_length_header():80 async def streaming_body(data):81 yield data # pragma: nocover82

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 87–96why, one step back
87 request.prepare()
88 assert request.headers == httpcore.Headers(89 [90 (b"accept-encoding", b"deflate, gzip, br"),91 (b"content-length", b"8"),92 ]93 )949596def test_url():

Include URL.__hash__ in test (#64)Yeray Diaz Diazbefore adoption2e9ab2e

lines 97–98why, one step back
97 url = "http://example.org"
98 request = httpcore.Request("GET", url)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 99–102why, one step back
99 assert request.url.scheme == "http"
100 assert request.url.port == 80101 assert request.url.full_path == "/"102

Include URL.__hash__ in test (#64)Yeray Diaz Diazbefore adoption2e9ab2e

lines 103–104why, one step back
103 url = "https://example.org/abc?foo=bar"
104 request = httpcore.Request("GET", url)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 105–110why, one step back
105 assert request.url.scheme == "https"
106 assert request.url.port == 443107 assert request.url.full_path == "/abc?foo=bar"108109110def test_invalid_urls():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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