2018-08-30 12:53:37 +00:00
|
|
|
import pytest
|
|
|
|
from starlette import App
|
|
|
|
from starlette.views import View
|
|
|
|
from starlette.response import PlainTextResponse
|
|
|
|
from starlette.testclient import TestClient
|
|
|
|
|
|
|
|
|
|
|
|
app = App()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
@app.route("/{username}")
|
|
|
|
class Homepage(View):
|
|
|
|
async def get(self, request, username=None):
|
|
|
|
if username is None:
|
|
|
|
return PlainTextResponse("Hello, world!")
|
|
|
|
return PlainTextResponse(f"Hello, {username}!")
|
|
|
|
|
|
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route():
|
|
|
|
response = client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "Hello, world!"
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_kwargs():
|
|
|
|
response = client.get("/tomchristie")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "Hello, tomchristie!"
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_method():
|
|
|
|
response = client.post("/")
|
2018-08-30 13:42:39 +00:00
|
|
|
assert response.status_code == 405
|
2018-08-30 12:53:37 +00:00
|
|
|
assert response.text == "Method not allowed"
|