fix(websockets): add websockets dep so that uvicorn works

This commit is contained in:
Houkime 2024-06-17 11:34:23 +00:00 committed by Inex Code
parent 41f6d8b6d2
commit 9accf861c5
2 changed files with 40 additions and 0 deletions

View file

@ -18,6 +18,7 @@ pythonPackages.buildPythonPackage rec {
strawberry-graphql
typing-extensions
uvicorn
websockets
];
pythonImportsCheck = [ "selfprivacy_api" ];
doCheck = false;

View file

@ -0,0 +1,39 @@
import pytest
from fastapi import FastAPI, WebSocket
import uvicorn
# import subprocess
from multiprocessing import Process
import asyncio
from time import sleep
from websockets import client
app = FastAPI()
@app.websocket("/")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"You sent: {data}")
def run_uvicorn():
uvicorn.run(app, port=5000)
return True
@pytest.mark.asyncio
async def test_uvcorn_ws_works_in_prod():
proc = Process(target=run_uvicorn)
proc.start()
sleep(2)
ws = await client.connect("ws://127.0.0.1:5000")
await ws.send("hohoho")
message = await ws.read_message()
assert message == "You sent: hohoho"
await ws.close()
proc.kill()