MCPcopy
hub / github.com/encode/uvicorn / _raw_server

Function _raw_server

tests/test_server.py:156–188  ·  tests/test_server.py::_raw_server
(
    *,
    app: ASGIApplication,
    port: int,
    http_protocol_cls: type[H11Protocol | HttpToolsProtocol],
    reset_contextvars: bool = False,
)

Source from the content-addressed store, hash-verified

154
155@contextlib.asynccontextmanager
156async 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
191async def test_contextvars_preserved_by_default(

Calls 5

serveMethod · 0.95
ConfigClass · 0.90
ServerClass · 0.90
create_taskMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected