Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

108 lines · 3.1 KiB · Python

Last changed by

Increase test coverageTom Christiebefore adoptione99ffbd

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():

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 21–34why, 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 )293031def test_transfer_encoding_header():32 async def streaming_body(data):33 yield data # pragma: nocover34

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 36why, one step back
36

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 38–71why, one step back
38 request.prepare()
39 assert request.headers == httpcore.Headers(40 [41 (b"host", b"example.org"),42 (b"transfer-encoding", b"chunked"),43 (b"accept-encoding", b"deflate, gzip, br"),44 ]45 )464748def test_override_host_header():49 headers = [(b"host", b"1.2.3.4:80")]5051 request = httpcore.Request("GET", "http://example.org", headers=headers)52 request.prepare()53 assert request.headers == httpcore.Headers(54 [(b"accept-encoding", b"deflate, gzip, br"), (b"host", b"1.2.3.4:80")]55 )565758def test_override_accept_encoding_header():59 headers = [(b"accept-encoding", b"identity")]6061 request = httpcore.Request("GET", "http://example.org", headers=headers)62 request.prepare()63 assert request.headers == httpcore.Headers(64 [(b"host", b"example.org"), (b"accept-encoding", b"identity")]65 )666768def test_override_content_length_header():69 async def streaming_body(data):70 yield data # pragma: nocover71

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

lines 75–77why, one step back
75 request = httpcore.Request(
76 "POST", "http://example.org", content=content, headers=headers77 )

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 78–100why, one step back
78 request.prepare()
79 assert request.headers == httpcore.Headers(80 [81 (b"host", b"example.org"),82 (b"accept-encoding", b"deflate, gzip, br"),83 (b"content-length", b"8"),84 ]85 )868788def test_url():89 request = httpcore.Request("GET", "http://example.org")90 assert request.url.scheme == "http"91 assert request.url.port == 8092 assert request.url.full_path == "/"9394 request = httpcore.Request("GET", "https://example.org/abc?foo=bar")95 assert request.url.scheme == "https"96 assert request.url.port == 44397 assert request.url.full_path == "/abc?foo=bar"9899100def test_invalid_urls():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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