Skip to content
Myr

test_http2.py

This file

Path

tests/dispatch/test_http2.py

80 lines · 2.3 KiB · Python

Last changed by

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

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–4why, one step back
1import json
23import pytest4

http3 (#86)Tom Christiebefore adoptionc9747aa

line 5why, one step back
5from http3 import Client, Response

LintingTom Christiebefore adoption8f4abe0

line 6why, one step back
6

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 8–9why, one step back
8
9

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

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

LintingTom Christiebefore adoption8f4abe0

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

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 20–21why, one step back
20
21

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 24why, one step back
24

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 27–28why, one step back
27
28 assert response.status_code == 200

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 30–31why, one step back
30
31

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

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

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

line 34why, one step back
34

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

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

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

line 37why, one step back
37

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 40–45why, one step back
40 "method": "POST",
41 "path": "/",42 "body": "<data>",43 }4445

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 48why, one step back
48

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

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

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

line 53why, one step back
53

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 56–57why, one step back
56
57 assert response_2.status_code == 200

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 59–60why, one step back
59
60 assert response_3.status_code == 200

Response.contentTom Christiebefore adoption4c55113

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

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

lines 62–80why, one step back
62
6364def test_http2_reconnect():65 """66 If a connection has been dropped between requests, then we should67 be seemlessly reconnected.68 """69 backend = MockHTTP2Backend(app=app)7071 with Client(backend=backend) as client:72 response_1 = client.get("http://example.org/1")73 backend.server.raise_disconnect = True74 response_2 = client.get("http://example.org/2")7576 assert response_1.status_code == 20077 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": ""}