コード例 #1
0
 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)
コード例 #2
0
    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)
コード例 #3
0
async def test_set_cookie():
    response = PlainTextResponse("Hello, world!", media_type="text/plain")
    response.set_cookie(
        "mycookie",
        "myvalue",
        max_age=10,
        expires=10,
        path="/",
        domain="localhost",
        secure=True,
        httponly=True,
        samesite="none",
    )

    async with httpx.AsyncClient(app=response, base_url="http://testServer/") as client:
        response = await client.get("/")
        assert response.text == "Hello, world!"