| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from aiohttp_sse_client2 import client as sse_client |
| 7 |
|
| 8 |
from .const import WPT_SERVER |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.asyncio |
| 12 |
async def test_rquest_accept(): |
| 13 |
"""Test EventSource: Accept header. |
| 14 |
|
| 15 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 16 |
eventsource/request-accept.htm |
| 17 |
""" |
| 18 |
source = sse_client.EventSource( |
| 19 |
WPT_SERVER + 'resources/accept.event_stream?pipe=sub') |
| 20 |
await source.connect() |
| 21 |
async for e in source: |
| 22 |
assert e.data == "text/event-stream" |
| 23 |
break |
| 24 |
await source.close() |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.asyncio |
| 28 |
async def test_rquest_cache_control(): |
| 29 |
"""Test EventSource: Cache-Control. |
| 30 |
|
| 31 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 32 |
eventsource/request-cache-control.htm |
| 33 |
""" |
| 34 |
source = sse_client.EventSource( |
| 35 |
WPT_SERVER + 'resources/cache-control.event_stream?pipe=sub') |
| 36 |
await source.connect() |
| 37 |
async for e in source: |
| 38 |
assert e.data == "no-cache" |
| 39 |
break |
| 40 |
await source.close() |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.asyncio |
| 44 |
async def test_request_redirect(): |
| 45 |
"""Test EventSource: redirect. |
| 46 |
|
| 47 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 48 |
eventsource/request-redirect.htm |
| 49 |
""" |
| 50 |
async def test(status): |
| 51 |
def on_error(): |
| 52 |
assert False |
| 53 |
|
| 54 |
def on_open(): |
| 55 |
assert source.ready_state == sse_client.READY_STATE_OPEN |
| 56 |
|
| 57 |
source = sse_client.EventSource( |
| 58 |
WPT_SERVER.replace('eventsource', 'common/redirect.py?' |
| 59 |
'location=/eventsource/resources/message.py&status=' |
| 60 |
+ str(status)), |
| 61 |
on_open=on_open, |
| 62 |
on_error=on_error) |
| 63 |
await source.connect() |
| 64 |
await source.close() |
| 65 |
|
| 66 |
await test(301) |
| 67 |
await test(302) |
| 68 |
await test(303) |
| 69 |
await test(307) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.asyncio |
| 73 |
async def test_request_status_error(): |
| 74 |
"""Test EventSource: redirect. |
| 75 |
|
| 76 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 77 |
eventsource/request-status-error.htm |
| 78 |
""" |
| 79 |
async def test(status): |
| 80 |
def on_error(): |
| 81 |
assert source.ready_state == sse_client.READY_STATE_CLOSED |
| 82 |
|
| 83 |
def on_message(): |
| 84 |
assert source.ready_state == sse_client.READY_STATE_OPEN |
| 85 |
|
| 86 |
source = sse_client.EventSource( |
| 87 |
WPT_SERVER + 'resources/status-error.py?status=' + str(status), |
| 88 |
on_message=on_message, |
| 89 |
on_error=on_error) |
| 90 |
with pytest.raises(ConnectionError): |
| 91 |
await source.connect() |
| 92 |
|
| 93 |
await test(204) |
| 94 |
await test(205) |
| 95 |
await test(210) |
| 96 |
await test(299) |
| 97 |
await test(404) |
| 98 |
await test(410) |
| 99 |
await test(503) |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.asyncio |
| 103 |
async def test_request_post_to_connect(): |
| 104 |
"""Test EventSource option method for connection. |
| 105 |
""" |
| 106 |
source = sse_client.EventSource( |
| 107 |
WPT_SERVER + 'resources/message.py', |
| 108 |
option={'method': "POST"} |
| 109 |
) |
| 110 |
await source.connect() |
| 111 |
async for e in source: |
| 112 |
assert e.data == "data" |
| 113 |
break |
| 114 |
await source.close() |