forked from niwinz/sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
113 lines (88 loc) · 2.93 KB
/
tests.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
import sys
def to_unicode(data):
if sys.version_info[0] >= 3: # Python 3
return str(data)
return unicode(data)
from sse import Sse
class ServerSentEventsProtocolTests(unittest.TestCase):
def test_constructor(self):
self.assertEqual(list(Sse()), ['retry: 2000\n\n'])
self.assertEqual(list(Sse(default_retry=1000)), ['retry: 1000\n\n'])
def test_add_message__simple_text(self):
sse = Sse()
sse.add_message("foo", "foo-message")
sse.add_message("bar", "bar-message")
self.assertEqual(to_unicode(sse), "retry: 2000\n\nevent: foo\ndata: "
"foo-message\n\nevent: bar\ndata: "
"bar-message\n\n")
self.assertEqual(list(sse), [
'retry: 2000\n\n',
'event: foo\n',
'data: foo-message\n',
'\n',
'event: bar\n',
'data: bar-message\n',
'\n'
])
def test_add_message__simple_text_split(self):
sse = Sse()
sse.add_message("foo", "foo\nmessage")
sse.add_message("bar", "bar\nmessage")
self.assertEqual(list(sse), [
'retry: 2000\n\n',
'event: foo\n',
'data: foo\n',
'data: message\n',
'\n',
'event: bar\n',
'data: bar\n',
'data: message\n',
'\n'
])
def test_add_message__list(self):
sse = Sse()
sse.add_message("foo", ["foo-message"])
sse.add_message("bar", ["bar-message"])
self.assertEqual(list(sse), [
'retry: 2000\n\n',
'event: foo\n',
'data: foo-message\n',
'\n',
'event: bar\n',
'data: bar-message\n',
'\n'
])
def test_add_message__list_split(self):
sse = Sse()
sse.add_message("foo", ["foo\nmessage"])
sse.add_message("bar", ["bar\nmessage"])
self.assertEqual(list(sse), [
'retry: 2000\n\n',
'event: foo\n',
'data: foo\n',
'data: message\n',
'\n',
'event: bar\n',
'data: bar\n',
'data: message\n',
'\n'
])
def test_dinamic_methods(self):
sse = Sse()
sse.add_event_foo(text="bar")
self.assertEqual(list(sse), ['retry: 2000\n\n', 'event: foo\n', 'data: bar\n', '\n'])
def test_flush(self):
sse = Sse()
sse.add_message("foo", "bar")
sse.flush()
self.assertEqual(len(sse._buffer), 0)
def test_flush_on_iter(self):
sse = Sse()
sse.add_message("foo", "bar")
self.assertEqual(list(sse), ['retry: 2000\n\n', 'event: foo\n', 'data: bar\n', '\n'])
self.assertEqual(list(sse), [])
if __name__ == "__main__":
unittest.main()