Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 async def app(scope, receive, send):
     request = Request(scope)
     try:
         data = await request.json()
     except RuntimeError:
         data = "Receive channel not available"
     response = JSONResponse({"json": data})
     await response(scope, receive, send)
Esempio n. 5
0
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
Esempio n. 6
0
    async def app(scope, receive, send):
        # the server is push-enabled
        scope["extensions"]["http.response.push"] = {}

        request = Request(scope, receive, send)
        await request.send_push_promise("/style.css")

        response = JSONResponse({"json": "OK"})
        await response(scope, receive, send)
Esempio n. 7
0
    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()
Esempio n. 8
0
    async def app(scope, receive, send):
        request = Request(scope, receive)
        mycookie = request.cookies.get("mycookie")
        if mycookie:
            response = Response(mycookie, media_type="text/plain")
        else:
            response = Response("Hello, world!", media_type="text/plain")
            response.set_cookie("mycookie", "Hello, cookies!")

        await response(scope, receive, send)
Esempio n. 9
0
    async def app(scope, receive, send):
        # the server is push-enabled
        scope["extensions"]["http.response.push"] = {}

        data = "OK"
        request = Request(scope)
        try:
            await request.send_push_promise("/style.css")
        except RuntimeError:
            data = "Send channel not available"
        response = JSONResponse({"json": data})
        await response(scope, receive, send)
Esempio n. 10
0
    async def app(scope, receive, send):
        request = Request(scope)
        await request.send_push_promise("/style.css")

        response = JSONResponse({"json": "OK"})
        await response(scope, receive, send)
Esempio n. 11
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     headers = dict(request.headers)
     response = JSONResponse({"headers": headers})
     await response(scope, receive, send)
Esempio n. 12
0
 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)
Esempio n. 13
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     response = JSONResponse({"cookies": request.cookies})
     await response(scope, receive, send)
Esempio n. 14
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     params = dict(request.query_params)
     response = JSONResponse({"params": params})
     await response(scope, receive, send)
Esempio n. 15
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     form = await request.form()
     response = JSONResponse({"form": dict(form)})
     await response(scope, receive, send)
Esempio n. 16
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     data = await request.json()
     response = JSONResponse({"json": data})
     await response(scope, receive, send)
Esempio n. 17
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     await request.body()
Esempio n. 18
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     body = await request.body()
     response = JSONResponse({"body": body.decode()})
     await response(scope, receive, send)
Esempio n. 19
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     request.state.example = 123
     response = JSONResponse({"state.example": request.state.example})
     await response(scope, receive, send)
Esempio n. 20
0
 def debug_response(self, request: Request, exc: Exception) -> Response:
     if request.accepts("text/html"):
         content = self.generate_html(exc)
         return HTMLResponse(content, status_code=500)
     content = self.generate_plain_text(exc)
     return PlainTextResponse(content, status_code=500)
Esempio n. 21
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     assert request.content_type.options == {"charset": "utf-8"}
     response = JSONResponse({"content-type": str(request.content_type)})
     await response(scope, receive, send)