Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

78 lines · 2.3 KiB · Python

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3from httpx import Client, Response

LintingTom Christiebefore adoption8f4abe0

line 4why, one step back
4

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 5why, one step back
5from .utils import MockHTTP2Backend

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 6–7why, one step back
6
7

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 8why, one step back
8def app(request):

LintingTom Christiebefore adoption8f4abe0

lines 9–16why, one step back
9 content = json.dumps(
10 {11 "method": request.method,12 "path": request.url.path,13 "body": request.content.decode(),14 }15 ).encode()16 headers = {"Content-Length": str(len(content))}

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 17why, one step back
17 return Response(200, headers=headers, content=content)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 18–19why, one step back
18
19

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 20–21why, one step back
20def test_http2_get_request():
21 backend = MockHTTP2Backend(app=app)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 22why, one step back
22

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 23–24why, one step back
23 with Client(backend=backend) as client:
24 response = client.get("http://example.org")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 25–26why, one step back
25
26 assert response.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 27why, one step back
27 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 28–29why, one step back
28
29

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 30–31why, one step back
30def test_http2_post_request():
31 backend = MockHTTP2Backend(app=app)

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

line 32why, one step back
32

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 33–34why, one step back
33 with Client(backend=backend) as client:
34 response = client.post("http://example.org", data=b"<data>")

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

line 35why, one step back
35

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

line 37why, one step back
37 assert json.loads(response.content) == {

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 38–43why, one step back
38 "method": "POST",
39 "path": "/",40 "body": "<data>",41 }4243

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 44–45why, one step back
44def test_http2_multiple_requests():
45 backend = MockHTTP2Backend(app=app)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 46why, one step back
46

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 47–50why, one step back
47 with Client(backend=backend) as client:
48 response_1 = client.get("http://example.org/1")49 response_2 = client.get("http://example.org/2")50 response_3 = client.get("http://example.org/3")

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

line 51why, one step back
51

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 52why, one step back
52 assert response_1.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 53why, one step back
53 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 54–55why, one step back
54
55 assert response_2.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 56why, one step back
56 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 57–58why, one step back
57
58 assert response_3.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 59why, one step back
59 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}

Check for connection aliveness on pool re-acquiry (#111)Tom Christiebefore adoption025240c

lines 60–78why, one step back
60
6162def test_http2_reconnect():63 """64 If a connection has been dropped between requests, then we should65 be seemlessly reconnected.66 """67 backend = MockHTTP2Backend(app=app)6869 with Client(backend=backend) as client:70 response_1 = client.get("http://example.org/1")71 backend.server.raise_disconnect = True72 response_2 = client.get("http://example.org/2")7374 assert response_1.status_code == 20075 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7677 assert response_2.status_code == 20078 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}