Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

139 lines · 4.5 KiB · Python

Last changed by

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

History of this file

Take it

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 3–4why, one step back
3from httpx import AsyncClient, Client, Response
4

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

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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")

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 25–29why, one step back
25
26 assert response.status_code == 20027 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}2829

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 32–39why, one step back
32
33 async with AsyncClient(backend=backend) as client:34 response = await client.get("http://example.org")3536 assert response.status_code == 20037 assert json.loads(response.content) == {"method": "GET", "path": "/", "body": ""}3839

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

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

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

line 42why, one step back
42

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

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

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

line 45why, one step back
45

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 46–53why, one step back
46 assert response.status_code == 200
47 assert json.loads(response.content) == {48 "method": "POST",49 "path": "/",50 "body": "<data>",51 }5253

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 54–55why, one step back
54async def test_async_http2_post_request(backend):
55 backend = MockHTTP2Backend(app=app, backend=backend)

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 56–67why, one step back
56
57 async with AsyncClient(backend=backend) as client:58 response = await client.post("http://example.org", data=b"<data>")5960 assert response.status_code == 20061 assert json.loads(response.content) == {62 "method": "POST",63 "path": "/",64 "body": "<data>",65 }6667

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

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

line 70why, one step back
70

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

lines 71–74why, one step back
71 with Client(backend=backend) as client:
72 response_1 = client.get("http://example.org/1")73 response_2 = client.get("http://example.org/2")74 response_3 = client.get("http://example.org/3")

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

line 75why, one step back
75

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 76–83why, one step back
76 assert response_1.status_code == 200
77 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}7879 assert response_2.status_code == 20080 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}8182 assert response_3.status_code == 20083 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}

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

lines 84–85why, one step back
84
85

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 86–87why, one step back
86async def test_async_http2_multiple_requests(backend):
87 backend = MockHTTP2Backend(app=app, backend=backend)

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 88–103why, one step back
88
89 async with AsyncClient(backend=backend) as client:90 response_1 = await client.get("http://example.org/1")91 response_2 = await client.get("http://example.org/2")92 response_3 = await client.get("http://example.org/3")9394 assert response_1.status_code == 20095 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}9697 assert response_2.status_code == 20098 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}99100 assert response_3.status_code == 200101 assert json.loads(response_3.content) == {"method": "GET", "path": "/3", "body": ""}102103

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

lines 104–112why, one step back
104def test_http2_reconnect():
105 """106 If a connection has been dropped between requests, then we should107 be seemlessly reconnected.108 """109 backend = MockHTTP2Backend(app=app)110111 with Client(backend=backend) as client:112 response_1 = client.get("http://example.org/1")

Check disconnections on connection reacquiry (#145)Tom Christiebefore adoptionec365c0

line 113why, one step back
113 backend.server.close_connection = True

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

line 114why, one step back
114 response_2 = client.get("http://example.org/2")

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 115–122why, one step back
115
116 assert response_1.status_code == 200117 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}118119 assert response_2.status_code == 200120 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}121122

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 123why, one step back
123async def test_async_http2_reconnect(backend):

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 124–127why, one step back
124 """
125 If a connection has been dropped between requests, then we should126 be seemlessly reconnected.127 """

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 128why, one step back
128 backend = MockHTTP2Backend(app=app, backend=backend)

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 129–133why, one step back
129
130 async with AsyncClient(backend=backend) as client:131 response_1 = await client.get("http://example.org/1")132 backend.server.close_connection = True133 response_2 = await client.get("http://example.org/2")

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

lines 134–139why, one step back
134
135 assert response_1.status_code == 200136 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}137138 assert response_2.status_code == 200139 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}