def test_request_scope_interface(): """ A Request can be instantiated with a scope, and presents a `Mapping` interface. """ request = Request({"type": "http", "method": "GET", "path": "/abc/"}) assert request["method"] == "GET" assert dict(request) == {"type": "http", "method": "GET", "path": "/abc/"} assert len(request) == 3 # test eq assert request == Request({ "type": "http", "method": "GET", "path": "/abc/" }) assert request != Request({ "type": "http", "method": "GET", "path": "/abc/", "query_params": {} }) async def mock_receive() -> Message: ... # pragma: no cover assert request != Request( { "type": "http", "method": "GET", "path": "/abc/" }, mock_receive) assert request != dict({"type": "http", "method": "GET", "path": "/abc/"})
async def app(scope, receive, send): request = Request(scope, receive) body = b"" async for chunk in request.stream(): body += chunk response = JSONResponse({"body": body.decode()}) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) chunks = b"" async for chunk in request.stream(): chunks += chunk try: body = await request.body except RuntimeError: body = b"<stream consumed>" response = JSONResponse({"body": body.decode(), "stream": chunks.decode()}) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) response = JSONResponse({ "host": request.client.host, "port": request.client.port }) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) response = PlainTextResponse("Hello, world!", media_type="text/plain") if request.cookies.get("mycookie"): response.delete_cookie("mycookie") else: response.set_cookie("mycookie", "myvalue") await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope) try: data = await request.json except NotImplementedError: data = "Receive channel not available" response = JSONResponse({"json": data}) await response(scope, receive, send)
async def app(scope, receive, send): nonlocal disconnected_after_response request = Request(scope, receive) await request.body disconnected = await request.is_disconnected() response = JSONResponse({"disconnected": disconnected}) await response(scope, receive, send) disconnected_after_response = await request.is_disconnected()
async def app(scope, receive, send): request = Request(scope, receive) form = await request.form file = form["file-key"] assert isinstance(file, UploadFile) assert await file.aread() == b"temporary file" response = JSONResponse({"file": file.filename}) await response(scope, receive, send) await request.close()
async def app(scope, receive, send): request = Request(scope, receive) mycookie = request.cookies.get("mycookie") if mycookie: response = PlainTextResponse(mycookie) else: response = PlainTextResponse("Hello, world!") response.set_cookie("mycookie", "Hello, cookies!") await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) response = JSONResponse({"cookies": request.cookies}) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) await request.body
async def app(scope, receive, send): request = Request(scope, receive) form = await request.form response = JSONResponse({"form": dict(form)}) await response(scope, receive, send) await request.close()
async def app(scope, receive, send): request = Request(scope, receive) body = await request.body response = JSONResponse({"body": body.decode()}) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) headers = dict(request.headers) headers.pop("user-agent") # this is httpx version, delete it response = JSONResponse({"headers": headers}) await response(scope, receive, send)
async def root(request: Request) -> Response: return PlainTextResponse(request.get("root_path", ""))
async def app(scope, receive, send): request = Request(scope, receive) data = {"method": request.method, "url": str(request.url)} response = JSONResponse(data) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) params = dict(request.query_params) response = JSONResponse({"params": params}) await response(scope, receive, send)
async def app(scope, receive, send): request = Request(scope, receive) data = await request.json response = JSONResponse({"json": data}) await response(scope, receive, send)