Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

90 lines · 2.7 KiB · Python

Last changed by

Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–2why, one step back
1import pytest
2

http3 (#86)Tom Christiebefore adoptionc9747aa

line 3why, one step back
3import http3

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 4–5why, one step back
4
5

Increase test coverageTom Christiebefore adoptione99ffbd

line 6why, one step back
6def test_request_repr():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 7why, one step back
7 request = http3.Request("GET", "http://example.org")

Increase test coverageTom Christiebefore adoptione99ffbd

lines 8–10why, one step back
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():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 12why, one step back
12 request = http3.Request("GET", "http://example.org")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 13why, one step back
13 assert "Content-Length" not in request.headers

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 14–16why, one step back
14
1516def test_content_length_header():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 18why, one step back
18 assert request.headers["Content-Length"] == "8"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 19–20why, one step back
19
20

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

line 21why, one step back
21def test_url_encoded_data():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 22why, one step back
22 for RequestClass in (http3.Request, http3.AsyncRequest):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 23–25why, one step back
23 request = RequestClass("POST", "http://example.org", data={"test": "123"})
24 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"25 assert request.content == b"test=123"

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

lines 26–27why, one step back
26
27

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 28why, one step back
28def test_json_encoded_data():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 29why, one step back
29 for RequestClass in (http3.Request, http3.AsyncRequest):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 30–34why, one step back
30 request = RequestClass("POST", "http://example.org", json={"test": 123})
31 assert request.headers["Content-Type"] == "application/json"32 assert request.content == b'{"test": 123}'3334

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 35why, one step back
35def test_transfer_encoding_header():

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 36why, one step back
36 def streaming_body(data):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 37–38why, one step back
37 yield data # pragma: nocover
38

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 40why, one step back
40

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 42–43why, one step back
42 assert "Content-Length" not in request.headers
43 assert request.headers["Transfer-Encoding"] == "chunked"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 44–46why, one step back
44
4546def test_override_host_header():

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 47why, one step back
47 headers = {"host": "1.2.3.4:80"}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 48why, one step back
48

http3 (#86)Tom Christiebefore adoptionc9747aa

line 49why, one step back
49 request = http3.Request("GET", "http://example.org", headers=headers)

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 50why, one step back
50 assert request.headers["Host"] == "1.2.3.4:80"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 51–53why, one step back
51
5253def test_override_accept_encoding_header():

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 54why, one step back
54 headers = {"Accept-Encoding": "identity"}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 55why, one step back
55

http3 (#86)Tom Christiebefore adoptionc9747aa

line 56why, one step back
56 request = http3.Request("GET", "http://example.org", headers=headers)

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 57why, one step back
57 assert request.headers["Accept-Encoding"] == "identity"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 58–60why, one step back
58
5960def test_override_content_length_header():

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 61why, one step back
61 def streaming_body(data):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 62–63why, one step back
62 yield data # pragma: nocover
63

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 65why, one step back
65 headers = {"Content-Length": "8"}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 66why, one step back
66

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 68why, one step back
68 assert request.headers["Content-Length"] == "8"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 69–71why, one step back
69
7071def test_url():

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

line 72why, one step back
72 url = "http://example.org"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 73why, one step back
73 request = http3.Request("GET", url)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 74–77why, one step back
74 assert request.url.scheme == "http"
75 assert request.url.port == 8076 assert request.url.full_path == "/"77

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

line 78why, one step back
78 url = "https://example.org/abc?foo=bar"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 79why, one step back
79 request = http3.Request("GET", url)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 80–85why, one step back
80 assert request.url.scheme == "https"
81 assert request.url.port == 44382 assert request.url.full_path == "/abc?foo=bar"838485def test_invalid_urls():

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 86–87why, one step back
86 with pytest.raises(http3.InvalidURL):
87 http3.Request("GET", "example.org")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 88why, one step back
88

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 89–90why, one step back
89 with pytest.raises(http3.InvalidURL):
90 http3.Request("GET", "http:///foo")