-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_sseclient.py
299 lines (235 loc) · 8.53 KB
/
test_sseclient.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
"""Tests for sseclient package."""
#
# Distributed under the terms of the MIT license.
#
from __future__ import unicode_literals
import itertools
import io
import json
try:
from unittest import mock
except ImportError:
import mock
import pytest
import requests
import six
from requests.cookies import RequestsCookieJar
import sseclient
from sseclient import Event as E
# Some tests of parsing a single event string
def test_round_trip_parse():
m1 = E(
data='hi there\nsexy developer',
event='salutation',
id='abcdefg',
retry=10000
)
dumped = m1.dump()
m2 = E.parse(dumped)
assert m1.id == m2.id
assert m1.data == m2.data
assert m1.retry == m2.retry
assert m1.event == m2.event
def test_no_colon():
m = E.parse('data')
assert m.data == ''
def test_no_space():
m = E.parse('data:hi')
assert m.data == 'hi'
def test_with_space():
m = E.parse('data: hi')
assert m.data == 'hi'
def test_with_leading_space():
m = E.parse('data: hi')
assert m.data == ' hi'
def test_comment():
raw = ":this is a comment\ndata: this is some data"
m = E.parse(raw)
assert m.data == 'this is some data'
def test_comment_only():
raw = ":this is a comment"
m = E.parse(raw)
assert m.data == ''
def test_retry_is_integer():
m = E.parse('data: hi\nretry: 4000')
assert m.retry == 4000
def test_default_event():
m = E.parse('data: blah')
assert m.event == 'message'
def test_eols():
for eol in ('\r\n', '\r', '\n'):
m = E.parse('event: hello%sdata: eol%s' % (eol, eol))
assert m.event == 'hello'
assert m.data == 'eol'
class FakeResponse(object):
def __init__(self, status_code, content, headers=None, encoding="utf-8"):
self.status_code = status_code
self.encoding = encoding
self.apparent_encoding = "utf-8"
if not isinstance(content, six.text_type):
content = content.decode("utf-8")
self.stream = content
self.headers = headers or None
self.raw = io.BytesIO(content.encode())
def raise_for_status(self):
pass
def iter_content(self, chunk_size=1024):
return self.raw
def join_events(*events):
"""
Given a bunch of Event objects, dump them all to strings and join them
together.
"""
return ''.join(e.dump() for e in events)
# Tests of parsing a multi event stream
@pytest.mark.parametrize("encoding", ["utf-8", None])
def test_last_id_remembered(monkeypatch, encoding):
content = 'data: message 1\nid: abcdef\n\ndata: message 2\n\n'
fake_get = mock.Mock(return_value=FakeResponse(200, content, encoding=encoding))
monkeypatch.setattr(requests, 'get', fake_get)
c = sseclient.SSEClient('http://blah.com')
m1 = next(c)
m2 = next(c)
assert m1.id == 'abcdef'
assert m2.id is None
assert c.last_id == 'abcdef'
def test_retry_remembered(monkeypatch):
content = 'data: message 1\nretry: 5000\n\ndata: message 2\n\n'
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
c = sseclient.SSEClient('http://blah.com')
m1 = next(c)
m2 = next(c)
assert m1.retry == 5000
assert m2.retry is None
assert c.retry == 5000
def test_commentonly_ignored(monkeypatch):
content = ':comment\n\ndata: message after comment\n\n'
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
c = sseclient.SSEClient('http://blah.com')
#the comment only event should be ignored entirely and not emitted
m1 = next(c)
assert m1.data == 'message after comment'
def test_retryonly_ignored(monkeypatch):
content = 'retry: 10000\n\ndata: will be emitted\n\n'
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
c = sseclient.SSEClient('http://blah.com')
#the retry only event should be processed but not emitted
m1 = next(c)
assert m1.data == ' will be emitted'
def test_extra_newlines_after_event(monkeypatch):
"""
This makes sure that extra newlines after an event don't
cause the event parser to break as it did in
https://github.com/btubbs/sseclient/issues/5.
"""
content = """event: hello
data: hello1
event: hello
data: hello2
event: hello
data: hello3
"""
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
c = sseclient.SSEClient('http://blah.com')
m1 = next(c)
m2 = next(c)
m3 = next(c)
assert m1.event == 'hello'
assert m1.data == 'hello1'
assert m2.event == 'hello'
assert m2.data == 'hello2'
assert m3.data == 'hello3'
assert m3.event == 'hello'
@pytest.fixture
def multiple_responses(monkeypatch):
content = join_events(
E(data='message 1', id='first', retry='2000', event='blah'),
E(data='message 2', id='second', retry='4000', event='blerg'),
E(data='message 3\nhas two lines', id='third'),
)
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
yield
fake_get.assert_called_once_with(
'http://blah.com',
headers={'Accept': 'text/event-stream', 'Cache-Control': 'no-cache'},
stream=True)
def assert_multiple_messages(m1, m2, m3):
assert m1.data == 'message 1'
assert m1.id == 'first'
assert m1.retry == 2000
assert m1.event == 'blah'
assert m2.data == 'message 2'
assert m2.id == 'second'
assert m2.retry == 4000
assert m2.event == 'blerg'
assert m3.data == 'message 3\nhas two lines'
@pytest.mark.usefixtures("multiple_responses")
def test_multiple_messages():
c = sseclient.SSEClient('http://blah.com')
m1 = next(c)
m2 = next(c)
m3 = next(c)
assert_multiple_messages(m1, m2, m3)
assert c.retry == m2.retry
assert c.last_id == m3.id
@pytest.mark.usefixtures("multiple_responses")
def test_simple_iteration():
c = sseclient.SSEClient('http://blah.com')
m1, m2, m3 = itertools.islice(c, 3)
assert_multiple_messages(m1, m2, m3)
def test_client_sends_cookies():
s = requests.Session()
s.cookies = RequestsCookieJar()
s.cookies['foo'] = 'bar'
with mock.patch('sseclient.requests.Session.send') as m:
m.return_value.encoding = "utf-8"
sseclient.SSEClient('http://blah.com', session=s)
prepared_request = m.call_args[0][0]
assert prepared_request.headers['Cookie'] == 'foo=bar'
@pytest.fixture
def unicode_multibyte_responses(monkeypatch):
content = join_events(
E(data='ööööööööööööööööööööööööööööööööööööööööööööööööööööööööö', id='first', retry='2000', event='blah'),
E(data='äääääääääääääääääääääääääääääääääääääääääääääääääääääääää', id='second', retry='4000', event='blerg'),
E(data='üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü', id='third'),
)
fake_get = mock.Mock(return_value=FakeResponse(200, content))
monkeypatch.setattr(requests, 'get', fake_get)
yield
fake_get.assert_called_once_with(
'http://blah.com',
headers={'Accept': 'text/event-stream', 'Cache-Control': 'no-cache'},
stream=True)
@pytest.mark.usefixtures("unicode_multibyte_responses")
def test_multiple_messages():
c = sseclient.SSEClient('http://blah.com',chunk_size=51)
assert next(c).data == 'ööööööööööööööööööööööööööööööööööööööööööööööööööööööööö'
assert next(c).data == 'äääääääääääääääääääääääääääääääääääääääääääääääääääääääää'
assert next(c).data == 'üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü'
def test_event_stream():
"""Check whether event.data can be loaded."""
limit = 50
url = 'https://stream.wikimedia.org/v2/stream/recentchange'
source = sseclient.SSEClient(url)
for n, event in enumerate(source, start=1):
if event.event != 'message' or not event.data:
continue
try:
element = json.loads(event.data)
except ValueError as e:
source.resp.close()
raise e
if n == limit:
break
assert True
if __name__ == '__main__':
try:
pytest.main()
except SystemExit:
pass