Files
chat/tests/test_websocket.py
Andrey K. Choi 3050e084fa
All checks were successful
continuous-integration/drone/push Build is passing
main functions commit
2025-10-19 19:50:00 +09:00

52 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Простой тест WebSocket соединения для Emergency Service
"""
import asyncio
import json
import websockets
import sys
async def test_websocket_connection():
"""Тест подключения к WebSocket эндпоинту"""
# Используем фиктивный токен для тестирования
test_token = "test_token_123"
user_id = "current_user_id"
uri = f"ws://localhost:8002/api/v1/emergency/ws/{user_id}?token={test_token}"
print(f"Попытка подключения к: {uri}")
try:
async with websockets.connect(uri) as websocket:
print("✅ WebSocket подключение установлено!")
# Отправляем ping
await websocket.send(json.dumps({"type": "ping"}))
print("📤 Отправлен ping")
# Ждем ответ
response = await asyncio.wait_for(websocket.recv(), timeout=5.0)
print(f"📥 Получен ответ: {response}")
# Ждем еще немного для других сообщений
try:
while True:
message = await asyncio.wait_for(websocket.recv(), timeout=2.0)
print(f"📥 Дополнительное сообщение: {message}")
except asyncio.TimeoutError:
print("⏱️ Таймаут - больше сообщений нет")
except websockets.exceptions.ConnectionClosedError as e:
if e.code == 1008:
print("❌ Подключение отклонено (403 Forbidden) - проблема с аутентификацией")
else:
print(f"❌ Подключение закрыто с кодом {e.code}: {e}")
except ConnectionRefusedError:
print("❌ Соединение отклонено - сервер не запущен или порт неправильный")
except Exception as e:
print(f"❌ Ошибка: {type(e).__name__}: {e}")
if __name__ == "__main__":
asyncio.run(test_websocket_connection())