|
| 1 | +import contextlib |
| 2 | +import logging |
| 3 | +import socket |
| 4 | + |
| 5 | +import h2.connection |
| 6 | +import h2.events |
1 | 7 | import httpx |
2 | 8 | import pytest |
3 | 9 |
|
| 10 | +import utils |
| 11 | + |
4 | 12 | pytest_plugins = ['pytest_userver.plugins.core'] |
5 | 13 |
|
6 | 14 | DEFAULT_TIMEOUT = 10.0 |
7 | 15 |
|
8 | 16 |
|
| 17 | +@pytest.fixture(name='create_socket') |
| 18 | +async def _create_socket(service_port, asyncio_socket): |
| 19 | + @contextlib.asynccontextmanager |
| 20 | + async def create_socket(): |
| 21 | + logging.debug('Connecting to localhost:%d', service_port) |
| 22 | + try: |
| 23 | + sock = asyncio_socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 24 | + await sock.connect(('localhost', service_port)) |
| 25 | + logging.debug('Connected: %r', sock) |
| 26 | + |
| 27 | + yield sock |
| 28 | + |
| 29 | + finally: |
| 30 | + sock.close() |
| 31 | + |
| 32 | + return create_socket |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(name='create_connection') |
| 36 | +async def _create_connection(create_socket): |
| 37 | + @contextlib.asynccontextmanager |
| 38 | + async def create_connection(): |
| 39 | + conn = h2.connection.H2Connection() |
| 40 | + conn.initiate_connection() |
| 41 | + |
| 42 | + async with create_socket() as sock: |
| 43 | + events = [] |
| 44 | + while len(events) != 2: |
| 45 | + events += await utils.send_and_receive(sock, conn) |
| 46 | + assert isinstance(events[0], h2.events.RemoteSettingsChanged) |
| 47 | + assert utils.MAX_CONCURRENT_STREAMS == events[0].changed_settings[3].new_value |
| 48 | + assert utils.DEFAULT_FRAME_SIZE == events[0].changed_settings[5].new_value |
| 49 | + assert isinstance(events[1], h2.events.SettingsAcknowledged) |
| 50 | + |
| 51 | + logging.debug('Connection successfully created') |
| 52 | + |
| 53 | + yield sock, conn |
| 54 | + |
| 55 | + return create_connection |
| 56 | + |
| 57 | + |
9 | 58 | class Http2Client: |
10 | 59 | def __init__(self, baseurl): |
11 | 60 | self.baseurl = baseurl[:-1] # rm '/' |
|
0 commit comments