File: test_asgi_ws.py

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (62 lines) | stat: -rw-r--r-- 1,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from copy import copy

import pytest
from ws_tutorial.app import app
from ws_tutorial.app import AuthMiddleware

from falcon import errors
from falcon import testing


@pytest.mark.asyncio
async def test_websocket_echo():
    async with testing.ASGIConductor(app) as conn:
        async with conn.simulate_ws('/echo') as ws:
            await ws.send_text('Hello, World!')
            response = await ws.receive_json()

            assert response['message'] == 'Hello, World!'


@pytest.mark.asyncio
async def test_resetting_auth_middleware():
    local_app = copy(app)
    local_app._middleware = None
    local_app.add_middleware(AuthMiddleware())

    async with testing.ASGIConductor(local_app) as conn:
        async with conn.simulate_ws('/reports') as ws:
            with pytest.raises(errors.WebSocketDisconnected):
                await ws.send_text('report1')
                await ws.receive_json()


@pytest.mark.asyncio
async def test_websocket_reports():
    async with testing.ASGIConductor(app) as conn:
        async with conn.simulate_ws('/reports') as ws:
            await ws.send_text('very secure token')
            await ws.send_text('report1')
            response = await ws.receive_json()

            assert response['report'] == 'Report 1'


@pytest.mark.asyncio
async def test_websocket_report_not_found():
    async with testing.ASGIConductor(app) as conn:
        async with conn.simulate_ws('/reports') as ws:
            await ws.send_text('very secure token')
            await ws.send_text('report10')
            response = await ws.receive_json()

            assert response['error'] == 'report not found'


@pytest.mark.asyncio
async def test_websocket_not_authenticated():
    async with testing.ASGIConductor(app) as conn:
        async with conn.simulate_ws('/reports') as ws:
            with pytest.raises(errors.WebSocketDisconnected):
                await ws.send_text('report1')
                await ws.receive_json()