Skip to content

Commit

Permalink
Use PlainTextResponse in FastAPI/Starlette examples and tests
Browse files Browse the repository at this point in the history
This sets the content type properly.
  • Loading branch information
agronholm committed May 3, 2022
1 parent 6c3c66e commit 206bc65
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
9 changes: 5 additions & 4 deletions examples/fastapi/dynamic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from asphalt.core import Component, Context, inject, resource
from fastapi import FastAPI, Response
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse


async def root() -> Response:
return Response("Hello, world!")
async def root() -> str:
return "Hello, world!"


class WebRootComponent(Component):
@inject
async def start(self, ctx: Context, app: FastAPI = resource()) -> None:
app.add_api_route("/", root)
app.add_api_route("/", root, response_class=PlainTextResponse)
9 changes: 5 additions & 4 deletions examples/fastapi/static.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from fastapi import FastAPI, Response
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

application = FastAPI()


@application.get("/")
async def root() -> Response:
return Response("Hello, world!")
@application.get("/", response_class=PlainTextResponse)
async def root() -> str:
return "Hello, world!"
4 changes: 2 additions & 2 deletions examples/starlette/dynamic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from asphalt.core import Component, Context, inject, resource
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response
from starlette.responses import PlainTextResponse, Response


async def root(request: Request) -> Response:
return Response("Hello, world!")
return PlainTextResponse("Hello, world!")


class WebRootComponent(Component):
Expand Down
4 changes: 2 additions & 2 deletions examples/starlette/static.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response
from starlette.responses import PlainTextResponse, Response

application = Starlette()


@application.route("/")
async def root(request: Request) -> Response:
return Response("Hello, world!")
return PlainTextResponse("Hello, world!")
4 changes: 2 additions & 2 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from fastapi import FastAPI
from httpx import AsyncClient
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.responses import JSONResponse, PlainTextResponse, Response
from starlette.websockets import WebSocket

from asphalt.web.fastapi import AsphaltDepends, FastAPIComponent
Expand Down Expand Up @@ -160,7 +160,7 @@ async def test_middleware(unused_tcp_port: int, method: str):
]

async def root(request: Request) -> Response:
return Response("Hello World")
return PlainTextResponse("Hello World")

application = FastAPI()
application.add_api_route("/", root)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from httpx import AsyncClient
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.responses import JSONResponse, PlainTextResponse, Response
from starlette.websockets import WebSocket

from asphalt.web.starlette import StarletteComponent
Expand Down Expand Up @@ -147,7 +147,7 @@ async def test_middleware(unused_tcp_port: int, method: str):
]

async def root(request: Request) -> Response:
return Response("Hello World")
return PlainTextResponse("Hello World")

application = Starlette()
application.add_route("/", root)
Expand Down

0 comments on commit 206bc65

Please sign in to comment.