2018-08-30 12:53:37 +00:00
|
|
|
import pytest
|
2018-09-05 09:29:04 +00:00
|
|
|
from starlette.responses import PlainTextResponse
|
2018-09-04 10:52:29 +00:00
|
|
|
from starlette.routing import Router, Path
|
2018-08-30 12:53:37 +00:00
|
|
|
from starlette.testclient import TestClient
|
2018-10-02 10:26:19 +00:00
|
|
|
from starlette.endpoints import HTTPEndpoint, WebSocketEndpoint
|
2018-08-30 12:53:37 +00:00
|
|
|
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
class Homepage(HTTPEndpoint):
|
2018-08-30 12:53:37 +00:00
|
|
|
async def get(self, request, username=None):
|
|
|
|
if username is None:
|
|
|
|
return PlainTextResponse("Hello, world!")
|
|
|
|
return PlainTextResponse(f"Hello, {username}!")
|
|
|
|
|
|
|
|
|
2018-09-04 10:52:29 +00:00
|
|
|
app = Router(routes=[Path("/", Homepage), Path("/{username}", Homepage)])
|
|
|
|
|
2018-08-30 12:53:37 +00:00
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
|
2018-10-02 10:26:19 +00:00
|
|
|
def test_http_endpoint_route():
|
2018-08-30 12:53:37 +00:00
|
|
|
response = client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "Hello, world!"
|
|
|
|
|
|
|
|
|
2018-10-02 10:26:19 +00:00
|
|
|
def test_http_endpoint_route_kwargs():
|
2018-08-30 12:53:37 +00:00
|
|
|
response = client.get("/tomchristie")
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "Hello, tomchristie!"
|
|
|
|
|
|
|
|
|
2018-10-02 10:26:19 +00:00
|
|
|
def test_http_endpoint_route_method():
|
2018-08-30 12:53:37 +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-10-02 10:26:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_connect():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
async def on_connect(self, websocket, **kwargs):
|
|
|
|
assert websocket["subprotocols"] == ["soap", "wamp"]
|
|
|
|
await websocket.accept(subprotocol="wamp")
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws", subprotocols=["soap", "wamp"]) as websocket:
|
|
|
|
assert websocket.accepted_subprotocol == "wamp"
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_receive_bytes():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
encoding = "bytes"
|
|
|
|
|
|
|
|
async def on_receive(self, websocket, data):
|
|
|
|
await websocket.send_bytes(b"Message bytes was: " + data)
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_bytes(b"Hello, world!")
|
|
|
|
_bytes = websocket.receive_bytes()
|
|
|
|
assert _bytes == b"Message bytes was: Hello, world!"
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_text("Hello world")
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_receive_json():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
encoding = "json"
|
|
|
|
|
|
|
|
async def on_receive(self, websocket, data):
|
|
|
|
await websocket.send_json({"message": data})
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_json({"hello": "world"})
|
|
|
|
data = websocket.receive_json()
|
|
|
|
assert data == {"message": {"hello": "world"}}
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_text("Hello world")
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_receive_text():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
encoding = "text"
|
|
|
|
|
|
|
|
async def on_receive(self, websocket, data):
|
|
|
|
await websocket.send_text(f"Message text was: {data}")
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_text("Hello, world!")
|
|
|
|
_text = websocket.receive_text()
|
|
|
|
assert _text == "Message text was: Hello, world!"
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_bytes(b"Hello world")
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_default():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
encoding = None
|
|
|
|
|
|
|
|
async def on_receive(self, websocket, data):
|
|
|
|
await websocket.send_text(f"Message text was: {data}")
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.send_text("Hello, world!")
|
|
|
|
_text = websocket.receive_text()
|
|
|
|
assert _text == "Message text was: Hello, world!"
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_endpoint_on_disconnect():
|
|
|
|
class WebSocketApp(WebSocketEndpoint):
|
|
|
|
async def on_disconnect(self, websocket, close_code):
|
|
|
|
assert close_code == 1001
|
|
|
|
await websocket.close(code=close_code)
|
|
|
|
|
|
|
|
client = TestClient(WebSocketApp)
|
|
|
|
with client.websocket_connect("/ws") as websocket:
|
|
|
|
websocket.close(code=1001)
|