mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-26 11:35:32 +08:00
Co-authored-by: Yurii Motov <yurii.motov.monte@gmail.com> Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
27 lines
827 B
Python
27 lines
827 B
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from fastapi.websockets import WebSocketDisconnect
|
|
|
|
from docs_src.websockets.tutorial001_py39 import app
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_main():
|
|
response = client.get("/")
|
|
assert response.status_code == 200, response.text
|
|
assert b"<!DOCTYPE html>" in response.content
|
|
|
|
|
|
def test_websocket():
|
|
with pytest.raises(WebSocketDisconnect):
|
|
with client.websocket_connect("/ws") as websocket:
|
|
message = "Message one"
|
|
websocket.send_text(message)
|
|
data = websocket.receive_text()
|
|
assert data == f"Message text was: {message}"
|
|
message = "Message two"
|
|
websocket.send_text(message)
|
|
data = websocket.receive_text()
|
|
assert data == f"Message text was: {message}"
|