Skip to content
Myr

More HTTP/2 tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent a44190f · Browse files · 2 files+45 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+45 added−7 removed

  • tests:+44 added−6 removed
  • other:+1 added−1 removed

2 files+45 added−7 removed

modified:httpcore/http2.py+1 added-1 removed
142 unmodified lines
143 if self.on_release is not None:144 await self.on_release(self)145146 async def close(self) -> None:147 self.writer.close()147 await self.writer.close()
modified:tests/test_http2.py+44 added-6 removed
1import h2.config2import h2.connection3import h2.events4import pytest5import json67import httpcore89
43 unmodified lines
53 def receive_data(self, data, stream_id):54 self.requests[stream_id][-1]["data"] += data5556 def stream_complete(self, stream_id):56 requests = self.requests[stream_id].pop(0)57 request = self.requests[stream_id].pop(0)58 if not self.requests[stream_id]:59 del self.requests[stream_id]6061 request_headers = dict(request["headers"])62 request_data = request["data"]6364 response_body = json.dumps({65 "method": request_headers[b":method"].decode(),66 "path": request_headers[b":path"].decode(),67 "body": request_data.decode()68 }).encode()6970 response_headers = (71 (b":status", b"200"),72 )63 response_body = b"Hello, world!"73 self.conn.send_headers(stream_id, response_headers)74 self.conn.send_data(stream_id, response_body, end_stream=True)75 self.buffer += self.conn.data_to_send()767778@pytest.mark.asyncio70async def test_http2():79async def test_http2_get_request():80 server = MockServer()81 origin = httpcore.Origin("http://example.org")73 client = httpcore.HTTP2Connection(reader=server, writer=server, origin=origin)74 response = await client.request("GET", "http://example.org")82 async with httpcore.HTTP2Connection(reader=server, writer=server, origin=origin) as client:83 response = await client.request("GET", "http://example.org")84 assert response.status_code == 20076 assert response.body == b"Hello, world!"85 assert json.loads(response.body) == {"method": "GET", "path": "/", "body": ""}868788@pytest.mark.asyncio89async def test_http2_post_request():90 server = MockServer()91 origin = httpcore.Origin("http://example.org")92 async with httpcore.HTTP2Connection(reader=server, writer=server, origin=origin) as client:93 response = await client.request("POST", "http://example.org", body=b"<data>")94 assert response.status_code == 20095 assert json.loads(response.body) == {"method": "POST", "path": "/", "body": "<data>"}969798@pytest.mark.asyncio99async def test_http2_multiple_requests():100 server = MockServer()101 origin = httpcore.Origin("http://example.org")102 async with httpcore.HTTP2Connection(reader=server, writer=server, origin=origin) as client:103 response_1 = await client.request("GET", "http://example.org/1")104 response_2 = await client.request("GET", "http://example.org/2")105 response_3 = await client.request("GET", "http://example.org/3")106107 assert response_1.status_code == 200108 assert json.loads(response_1.body) == {"method": "GET", "path": "/1", "body": ""}109110 assert response_2.status_code == 200111 assert json.loads(response_2.body) == {"method": "GET", "path": "/2", "body": ""}112113 assert response_3.status_code == 200114 assert json.loads(response_3.body) == {"method": "GET", "path": "/3", "body": ""}