Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

155 lines · 4.4 KiB · Python

Last changed by

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

History of this file

Take it

The walk stopped after 200 commits: lines older than 586acdd point at it.

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 1–7why, one step back
1from datetime import timedelta
23import pytest45import httpx67

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 8why, one step back
8def test_get(server):

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 9why, one step back
9 url = server.url

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 10–11why, one step back
10 with httpx.Client() as http:
11 response = http.get(url)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 12–24why, one step back
12 assert response.status_code == 200
13 assert response.url == url14 assert response.content == b"Hello, world!"15 assert response.text == "Hello, world!"16 assert response.http_version == "HTTP/1.1"17 assert response.encoding == "iso-8859-1"18 assert response.request.url == url19 assert response.headers20 assert response.is_redirect is False21 assert repr(response) == "<Response [200 OK]>"22 assert response.elapsed > timedelta(0)2324

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 25why, one step back
25def test_build_request(server):

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 26–28why, one step back
26 url = server.url.copy_with(path="/echo_headers")
27 headers = {"Custom-header": "value"}28

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 29why, one step back
29 with httpx.Client() as client:

Drop sync (#544)Tom Christiebefore adoption206c537

line 30why, one step back
30 request = client.build_request("GET", url)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 31why, one step back
31 request.headers.update(headers)

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 32why, one step back
32 response = client.send(request)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 33–39why, one step back
33
34 assert response.status_code == 20035 assert response.url == url3637 assert response.json()["Custom-header"] == "value"3839

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 40–42why, one step back
40def test_post(server):
41 with httpx.Client() as client:42 response = client.post(server.url, data=b"Hello, world!")

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 43–46why, one step back
43 assert response.status_code == 200
44 assert response.reason_phrase == "OK"4546

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 47–49why, one step back
47def test_post_json(server):
48 with httpx.Client() as client:49 response = client.post(server.url, json={"text": "Hello, world!"})

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 50–53why, one step back
50 assert response.status_code == 200
51 assert response.reason_phrase == "OK"5253

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 54–57why, one step back
54def test_stream_response(server):
55 with httpx.Client() as client:56 with client.stream("GET", server.url) as response:57 content = response.read()

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 58–61why, one step back
58 assert response.status_code == 200
59 assert content == b"Hello, world!"6061

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 62why, one step back
62def test_stream_iterator(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 63–64why, one step back
63 body = b""
64

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 65–67why, one step back
65 with httpx.Client() as client:
66 with client.stream("GET", server.url) as response:67 for chunk in response.iter_bytes():

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 68–69why, one step back
68 body += chunk
69

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 70–73why, one step back
70 assert response.status_code == 200
71 assert body == b"Hello, world!"7273

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 74why, one step back
74def test_raw_iterator(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 75–76why, one step back
75 body = b""
76

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 77–79why, one step back
77 with httpx.Client() as client:
78 with client.stream("GET", server.url) as response:79 for chunk in response.iter_raw():

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 80–81why, one step back
80 body += chunk
81

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 82–85why, one step back
82 assert response.status_code == 200
83 assert body == b"Hello, world!"8485

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 86–87why, one step back
86def test_raise_for_status(server):
87 with httpx.Client() as client:

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 88why, one step back
88 for status_code in (200, 400, 404, 500, 505):

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 89why, one step back
89 response = client.request(

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 90–99why, one step back
90 "GET", server.url.copy_with(path="/status/{}".format(status_code))
91 )92 if 400 <= status_code < 600:93 with pytest.raises(httpx.HTTPError) as exc_info:94 response.raise_for_status()95 assert exc_info.value.response == response96 else:97 assert response.raise_for_status() is None9899

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 100–102why, one step back
100def test_options(server):
101 with httpx.Client() as client:102 response = client.options(server.url)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 103–106why, one step back
103 assert response.status_code == 200
104 assert response.reason_phrase == "OK"105106

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 107–109why, one step back
107def test_head(server):
108 with httpx.Client() as client:109 response = client.head(server.url)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 110–113why, one step back
110 assert response.status_code == 200
111 assert response.reason_phrase == "OK"112113

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 114–116why, one step back
114def test_put(server):
115 with httpx.Client() as client:116 response = client.put(server.url, data=b"Hello, world!")

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 117–120why, one step back
117 assert response.status_code == 200
118 assert response.reason_phrase == "OK"119120

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 121–123why, one step back
121def test_patch(server):
122 with httpx.Client() as client:123 response = client.patch(server.url, data=b"Hello, world!")

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 124–127why, one step back
124 assert response.status_code == 200
125 assert response.reason_phrase == "OK"126127

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 128–130why, one step back
128def test_delete(server):
129 with httpx.Client() as client:130 response = client.delete(server.url)

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 131–134why, one step back
131 assert response.status_code == 200
132 assert response.reason_phrase == "OK"133134

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 135why, one step back
135def test_base_url(server):

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

line 136why, one step back
136 base_url = server.url

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 137–138why, one step back
137 with httpx.Client(base_url=base_url) as client:
138 response = client.get("/")

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 139–143why, one step back
139 assert response.status_code == 200
140 assert response.url == base_url141142143def test_merge_url():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 144why, one step back
144 client = httpx.Client(base_url="https://www.paypal.com/")

Improve robustness of live HTTP/2 test (#512)Florimond Mancabefore adoption586acdd

lines 145–150why, one step back
145 url = client.merge_url("http://www.paypal.com")
146147 assert url.scheme == "https"148 assert url.is_ssl149150

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 151why, one step back
151def test_elapsed_delay(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

line 152why, one step back
152 url = server.url.copy_with(path="/slow_response/100")

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 153–154why, one step back
153 with httpx.Client() as client:
154 response = client.get(url)

Trivial-case test for response.elapsedTom Christiebefore adoption02d16bf

line 155why, one step back
155 assert response.elapsed.total_seconds() > 0.0