Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

143 lines · 4.2 KiB · Python

Last changed by

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

History of this file

Take it

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3import httpx

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 4–5why, one step back
4
5

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

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

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 7why, one step back
7 url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 8why, one step back
8 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 9why, one step back
9 response = http.get(url)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 10why, one step back
10 assert response.status_code == 200

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 11why, one step back
11 assert response.url == httpx.URL(url)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 12why, one step back
12 assert response.content == b"Hello, world!"

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 13why, one step back
13 assert response.text == "Hello, world!"

Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3

line 14why, one step back
14 assert response.http_version == "HTTP/1.1"

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 15why, one step back
15 assert response.encoding == "iso-8859-1"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 16why, one step back
16 assert response.request.url == httpx.URL(url)

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 17–18why, one step back
17 assert response.headers
18 assert response.is_redirect is False

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

line 19why, one step back
19 assert repr(response) == "<Response [200 OK]>"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 20–21why, one step back
20
21

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 22why, one step back
22def test_post(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 23why, one step back
23 with httpx.Client() as http:

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 24why, one step back
24 response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 25why, one step back
25 assert response.status_code == 200

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 26why, one step back
26 assert response.reason_phrase == "OK"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 27–28why, one step back
27
28

Add json interfacesTom Christiebefore adoption5a63d31

line 29why, one step back
29def test_post_json(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 30why, one step back
30 with httpx.Client() as http:

Add json interfacesTom Christiebefore adoption5a63d31

lines 31–35why, one step back
31 response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})
32 assert response.status_code == 20033 assert response.reason_phrase == "OK"3435

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 36why, one step back
36def test_stream_response(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 37why, one step back
37 with httpx.Client() as http:

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 38why, one step back
38 response = http.get("http://127.0.0.1:8000/", stream=True)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 39why, one step back
39 assert response.status_code == 200

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 40–41why, one step back
40 content = response.read()
41 assert content == b"Hello, world!"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 42–43why, one step back
42
43

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 45why, one step back
45 with httpx.Client() as http:

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 46–51why, one step back
46 response = http.get("http://127.0.0.1:8000/", stream=True)
47 assert response.status_code == 20048 body = b""49 for chunk in response.stream():50 body += chunk51 assert body == b"Hello, world!"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 52why, one step back
52

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 53–54why, one step back
53
54def test_raw_iterator(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 55why, one step back
55 with httpx.Client() as http:

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 56–61why, one step back
56 response = http.get("http://127.0.0.1:8000/", stream=True)
57 assert response.status_code == 20058 body = b""59 for chunk in response.raw():60 body += chunk61 assert body == b"Hello, world!"

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 62–65why, one step back
62 response.close() # TODO: should Response be available as context managers?
636465def test_raise_for_status(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 67–71why, one step back
67 for status_code in (200, 400, 404, 500, 505):
68 response = client.request(69 "GET", "http://127.0.0.1:8000/status/{}".format(status_code)70 )71 if 400 <= status_code < 600:

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 72why, one step back
72 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 73why, one step back
73 response.raise_for_status()

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 74why, one step back
74 assert exc_info.value.response == response

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 75–79why, one step back
75 else:
76 assert response.raise_for_status() is None777879def test_options(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 80why, one step back
80 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 81–86why, one step back
81 response = http.options("http://127.0.0.1:8000/")
82 assert response.status_code == 20083 assert response.reason_phrase == "OK"848586def test_head(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 87why, one step back
87 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 88–93why, one step back
88 response = http.head("http://127.0.0.1:8000/")
89 assert response.status_code == 20090 assert response.reason_phrase == "OK"919293def test_put(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 94why, one step back
94 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 95–100why, one step back
95 response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")
96 assert response.status_code == 20097 assert response.reason_phrase == "OK"9899100def test_patch(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 101why, one step back
101 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 102–107why, one step back
102 response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")
103 assert response.status_code == 200104 assert response.reason_phrase == "OK"105106107def test_delete(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 108why, one step back
108 with httpx.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 109–111why, one step back
109 response = http.delete("http://127.0.0.1:8000/")
110 assert response.status_code == 200111 assert response.reason_phrase == "OK"

Support Client(base_url=...)Tom Christiebefore adoption9d5aaff

lines 112–115why, one step back
112
113114def test_base_url(server):115 base_url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 116why, one step back
116 with httpx.Client(base_url=base_url) as http:

Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15

line 117why, one step back
117 response = http.get("/")

Support Client(base_url=...)Tom Christiebefore adoption9d5aaff

lines 118–119why, one step back
118 assert response.status_code == 200
119 assert str(response.url) == base_url

Move HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89

lines 120–127why, one step back
120
121122def test_merge_url():123 client = httpx.Client(base_url="https://www.paypal.com/")124 url = client.merge_url("http://www.paypal.com")125126 assert url.scheme == "https"127 assert url.is_ssl

Enforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c

lines 128–143why, one step back
128
129130class DerivedFromAsyncioBackend(httpx.AsyncioBackend):131 pass132133134class AnyBackend:135 pass136137138def test_client_backend_must_be_asyncio_based():139 httpx.Client(backend=httpx.AsyncioBackend())140 httpx.Client(backend=DerivedFromAsyncioBackend())141142 with pytest.raises(ValueError):143 httpx.Client(backend=AnyBackend())