(
*,
app: ASGIApplication,
port: int,
http_protocol_cls: type[H11Protocol | HttpToolsProtocol],
reset_contextvars: bool = False,
)
| 154 | |
| 155 | @contextlib.asynccontextmanager |
| 156 | async def _raw_server( |
| 157 | *, |
| 158 | app: ASGIApplication, |
| 159 | port: int, |
| 160 | http_protocol_cls: type[H11Protocol | HttpToolsProtocol], |
| 161 | reset_contextvars: bool = False, |
| 162 | ): |
| 163 | config = Config(app=app, port=port, loop=class="st">"asyncio", http=http_protocol_cls, reset_contextvars=reset_contextvars) |
| 164 | server = Server(config=config) |
| 165 | task = asyncio.create_task(server.serve()) |
| 166 | |
| 167 | while not server.started: |
| 168 | await asyncio.sleep(0.01) |
| 169 | |
| 170 | reader, writer = await asyncio.open_connection(class="st">"127.0.0.1", port) |
| 171 | |
| 172 | async def extract_json_body(request: bytes): |
| 173 | writer.write(request) |
| 174 | await writer.drain() |
| 175 | |
| 176 | status, *headers = (await reader.readuntil(bclass="st">"\r\n\r\n")).split(bclass="st">"\r\n")[:-2] |
| 177 | assert status == bclass="st">"HTTP/1.1 200 OK" |
| 178 | |
| 179 | content_length = next(int(h.split(bclass="st">":", 1)[1]) for h in headers if h.lower().startswith(bclass="st">"content-length:")) |
| 180 | return json.loads(await reader.readexactly(content_length)) |
| 181 | |
| 182 | try: |
| 183 | yield extract_json_body |
| 184 | finally: |
| 185 | writer.close() |
| 186 | await writer.wait_closed() |
| 187 | server.should_exit = True |
| 188 | await task |
| 189 | |
| 190 | |
| 191 | async def test_contextvars_preserved_by_default( |
no test coverage detected