Debian Codegraph @

tests/web_platform_tests/test_format_field_id.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 from datetime import datetime, timedelta
4
5 import pytest
6 from pytest import fail
7
8 from aiohttp_sse_client2 import client as sse_client
9
10 from .const import WPT_SERVER
11
12
13 @pytest.mark.asyncio
14 async def test_format_field_id():
15 """Test EventSource: Last-Event-ID.
16
17 ..seealso: https://github.com/web-platform-tests/wpt/blob/master/
18 eventsource/format-field-id.htm
19 """
20 seen_hello = False
21
22 async with sse_client.EventSource(
23 WPT_SERVER + 'resources/last-event-id.py',
24 ) as source:
25 async for e in source:
26 if not seen_hello:
27 assert e.data == "hello"
28 seen_hello = True
29 # default last event id is Unicode U+2026
30 assert e.last_event_id == "…"
31 last_id = e.last_event_id
32 else:
33 assert e.data == last_id
34 assert e.last_event_id == last_id
35 break
36
37
38 @pytest.mark.asyncio
39 async def test_format_field_id_2():
40 """Test EventSource: Last-Event-ID (2).
41
42 ..seealso: https://github.com/web-platform-tests/wpt/blob/master/
43 eventsource/format-field-id-2.htm
44 """
45 counter = 0
46
47 async with sse_client.EventSource(
48 WPT_SERVER + 'resources/last-event-id.py',
49 ) as source:
50 async for e in source:
51 if counter == 0:
52 counter += 1
53 assert e.data == "hello"
54 # default last event id is Unicode U+2026
55 assert e.last_event_id == "…"
56 last_id = e.last_event_id
57 elif counter in (1, 2):
58 counter += 1
59 assert e.data == last_id
60 assert e.last_event_id == last_id
61 break
62 else:
63 fail("Unexpected counter {}".format(counter))
64
65
66 @pytest.mark.asyncio
67 async def test_format_field_id_null():
68 """Test EventSource: U+0000 in id field.
69
70 ..seealso: https://github.com/web-platform-tests/wpt/blob/master/
71 eventsource/format-field-id-null.htm
72 """
73 seen_hello = False
74
75 async with sse_client.EventSource(
76 WPT_SERVER + 'resources/last-event-id.py?idvalue=%00%00',
77 ) as source:
78 async for e in source:
79 if not seen_hello:
80 assert e.data == "hello"
81 seen_hello = True
82 # Unicode U+0000 will be ignored as Event ID
83 assert e.last_event_id == ""
84 else:
85 assert e.data == "hello"
86 assert e.last_event_id == ""
87 break