2018-07-12 13:09:05 +00:00
|
|
|
from starlette import Response, TestClient
|
2018-09-04 10:52:29 +00:00
|
|
|
from starlette.exceptions import ExceptionMiddleware
|
2018-08-17 15:36:48 +00:00
|
|
|
from starlette.routing import Path, PathPrefix, Router, ProtocolRouter
|
2018-08-28 13:34:18 +00:00
|
|
|
from starlette.websockets import WebSocketSession, WebSocketDisconnect
|
|
|
|
import pytest
|
2018-06-25 21:23:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def homepage(scope):
|
2018-06-25 21:26:01 +00:00
|
|
|
return Response("Hello, world", media_type="text/plain")
|
2018-06-25 21:23:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def users(scope):
|
2018-06-25 21:26:01 +00:00
|
|
|
return Response("All users", media_type="text/plain")
|
2018-06-25 21:23:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def user(scope):
|
2018-06-25 21:26:01 +00:00
|
|
|
content = "User " + scope["kwargs"]["username"]
|
|
|
|
return Response(content, media_type="text/plain")
|
2018-06-25 21:23:40 +00:00
|
|
|
|
|
|
|
|
2018-06-26 09:09:35 +00:00
|
|
|
def staticfiles(scope):
|
|
|
|
return Response("xxxxx", media_type="image/png")
|
|
|
|
|
|
|
|
|
2018-06-25 21:26:01 +00:00
|
|
|
app = Router(
|
|
|
|
[
|
2018-06-26 09:09:59 +00:00
|
|
|
Path("/", app=homepage, methods=["GET"]),
|
2018-06-25 21:26:01 +00:00
|
|
|
PathPrefix(
|
|
|
|
"/users", app=Router([Path("", app=users), Path("/{username}", app=user)])
|
|
|
|
),
|
2018-06-26 09:09:59 +00:00
|
|
|
PathPrefix("/static", app=staticfiles, methods=["GET"]),
|
2018-06-25 21:26:01 +00:00
|
|
|
]
|
|
|
|
)
|
2018-06-25 21:23:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_router():
|
|
|
|
client = TestClient(app)
|
|
|
|
|
2018-06-25 21:26:01 +00:00
|
|
|
response = client.get("/")
|
2018-06-25 21:23:40 +00:00
|
|
|
assert response.status_code == 200
|
2018-06-25 21:26:01 +00:00
|
|
|
assert response.text == "Hello, world"
|
2018-06-25 21:23:40 +00:00
|
|
|
|
2018-06-26 09:09:35 +00:00
|
|
|
response = client.post("/")
|
2018-08-30 13:42:39 +00:00
|
|
|
assert response.status_code == 405
|
2018-09-04 10:52:29 +00:00
|
|
|
assert response.text == "Method Not Allowed"
|
2018-06-26 09:09:35 +00:00
|
|
|
|
2018-06-25 21:26:01 +00:00
|
|
|
response = client.get("/foo")
|
2018-06-25 21:23:40 +00:00
|
|
|
assert response.status_code == 404
|
2018-09-04 10:52:29 +00:00
|
|
|
assert response.text == "Not Found"
|
2018-06-25 21:23:40 +00:00
|
|
|
|
2018-06-25 21:26:01 +00:00
|
|
|
response = client.get("/users")
|
2018-06-25 21:23:40 +00:00
|
|
|
assert response.status_code == 200
|
2018-06-25 21:26:01 +00:00
|
|
|
assert response.text == "All users"
|
2018-06-25 21:23:40 +00:00
|
|
|
|
2018-06-25 21:26:01 +00:00
|
|
|
response = client.get("/users/tomchristie")
|
2018-06-25 21:23:40 +00:00
|
|
|
assert response.status_code == 200
|
2018-06-25 21:26:01 +00:00
|
|
|
assert response.text == "User tomchristie"
|
2018-06-26 09:09:35 +00:00
|
|
|
|
|
|
|
response = client.get("/static/123")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "xxxxx"
|
|
|
|
|
|
|
|
response = client.post("/static/123")
|
2018-08-30 13:42:39 +00:00
|
|
|
assert response.status_code == 405
|
2018-09-04 10:52:29 +00:00
|
|
|
assert response.text == "Method Not Allowed"
|
2018-08-17 15:36:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def http_endpoint(scope):
|
|
|
|
return Response("Hello, world", media_type="text/plain")
|
|
|
|
|
|
|
|
|
|
|
|
def websocket_endpoint(scope):
|
|
|
|
async def asgi(receive, send):
|
|
|
|
session = WebSocketSession(scope, receive, send)
|
|
|
|
await session.accept()
|
|
|
|
await session.send_json({"hello": "world"})
|
|
|
|
await session.close()
|
|
|
|
|
|
|
|
return asgi
|
|
|
|
|
|
|
|
|
|
|
|
mixed_protocol_app = ProtocolRouter(
|
2018-08-28 13:34:18 +00:00
|
|
|
{
|
|
|
|
"http": Router([Path("/", app=http_endpoint)]),
|
|
|
|
"websocket": Router([Path("/", app=websocket_endpoint)]),
|
|
|
|
}
|
2018-08-17 15:36:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_protocol_switch():
|
|
|
|
client = TestClient(mixed_protocol_app)
|
|
|
|
|
|
|
|
response = client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "Hello, world"
|
|
|
|
|
2018-08-28 13:45:06 +00:00
|
|
|
with client.websocket_connect("/") as session:
|
2018-08-17 15:36:48 +00:00
|
|
|
assert session.receive_json() == {"hello": "world"}
|
2018-08-28 13:34:18 +00:00
|
|
|
|
|
|
|
with pytest.raises(WebSocketDisconnect):
|
2018-08-28 13:45:06 +00:00
|
|
|
client.websocket_connect("/404")
|