-
-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathtest_http11.py
426 lines (376 loc) · 15.2 KB
/
test_http11.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from websockets.datastructures import Headers
from websockets.exceptions import SecurityError
from websockets.http11 import *
from websockets.http11 import parse_headers
from websockets.streams import StreamReader
from .utils import GeneratorTestCase
class RequestTests(GeneratorTestCase):
def setUp(self):
super().setUp()
self.reader = StreamReader()
def parse(self):
return Request.parse(self.reader.read_line)
def test_parse(self):
# Example from the protocol overview in RFC 6455
self.reader.feed_data(
b"GET /chat HTTP/1.1\r\n"
b"Host: server.example.com\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
b"Origin: http://example.com\r\n"
b"Sec-WebSocket-Protocol: chat, superchat\r\n"
b"Sec-WebSocket-Version: 13\r\n"
b"\r\n"
)
request = self.assertGeneratorReturns(self.parse())
self.assertEqual(request.path, "/chat")
self.assertEqual(request.headers["Upgrade"], "websocket")
def test_parse_empty(self):
self.reader.feed_eof()
with self.assertRaises(EOFError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"connection closed while reading HTTP request line",
)
def test_parse_invalid_request_line(self):
self.reader.feed_data(b"GET /\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid HTTP request line: GET /",
)
def test_parse_unsupported_protocol(self):
self.reader.feed_data(b"GET /chat HTTP/1.0\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"unsupported protocol; expected HTTP/1.1: GET /chat HTTP/1.0",
)
def test_parse_unsupported_method(self):
self.reader.feed_data(b"OPTIONS * HTTP/1.1\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"unsupported HTTP method; expected GET; got OPTIONS",
)
def test_parse_invalid_header(self):
self.reader.feed_data(b"GET /chat HTTP/1.1\r\nOops\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid HTTP header line: Oops",
)
def test_parse_body(self):
self.reader.feed_data(b"GET / HTTP/1.1\r\nContent-Length: 3\r\n\r\nYo\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"unsupported request body",
)
def test_parse_body_content_length_zero(self):
self.reader.feed_data(b"GET / HTTP/1.1\r\nContent-Length: 0\r\n\r\n")
request = self.assertGeneratorReturns(self.parse())
self.assertEqual(request.headers["Content-Length"], "0")
def test_parse_body_with_transfer_encoding(self):
self.reader.feed_data(b"GET / HTTP/1.1\r\nTransfer-Encoding: compress\r\n\r\n")
with self.assertRaises(NotImplementedError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"transfer codings aren't supported",
)
def test_serialize(self):
# Example from the protocol overview in RFC 6455
request = Request(
"/chat",
Headers(
[
("Host", "server.example.com"),
("Upgrade", "websocket"),
("Connection", "Upgrade"),
("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="),
("Origin", "http://example.com"),
("Sec-WebSocket-Protocol", "chat, superchat"),
("Sec-WebSocket-Version", "13"),
]
),
)
self.assertEqual(
request.serialize(),
b"GET /chat HTTP/1.1\r\n"
b"Host: server.example.com\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
b"Origin: http://example.com\r\n"
b"Sec-WebSocket-Protocol: chat, superchat\r\n"
b"Sec-WebSocket-Version: 13\r\n"
b"\r\n",
)
class ResponseTests(GeneratorTestCase):
def setUp(self):
super().setUp()
self.reader = StreamReader()
def parse(self, **kwargs):
return Response.parse(
self.reader.read_line,
self.reader.read_exact,
self.reader.read_to_eof,
**kwargs,
)
def test_parse(self):
# Example from the protocol overview in RFC 6455
self.reader.feed_data(
b"HTTP/1.1 101 Switching Protocols\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
b"Sec-WebSocket-Protocol: chat\r\n"
b"\r\n"
)
response = self.assertGeneratorReturns(self.parse())
self.assertEqual(response.status_code, 101)
self.assertEqual(response.reason_phrase, "Switching Protocols")
self.assertEqual(response.headers["Upgrade"], "websocket")
self.assertEqual(response.body, b"")
def test_parse_empty(self):
self.reader.feed_eof()
with self.assertRaises(EOFError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"connection closed while reading HTTP status line",
)
def test_parse_invalid_status_line(self):
self.reader.feed_data(b"Hello!\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid HTTP status line: Hello!",
)
def test_parse_unsupported_protocol(self):
self.reader.feed_data(b"HTTP/1.0 400 Bad Request\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"unsupported protocol; expected HTTP/1.1: HTTP/1.0 400 Bad Request",
)
def test_parse_non_integer_status(self):
self.reader.feed_data(b"HTTP/1.1 OMG WTF\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid status code; expected integer; got OMG",
)
def test_parse_non_three_digit_status(self):
self.reader.feed_data(b"HTTP/1.1 007 My name is Bond\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception), "invalid status code; expected 100–599; got 007"
)
def test_parse_invalid_reason(self):
self.reader.feed_data(b"HTTP/1.1 200 \x7f\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid HTTP reason phrase: \x7f",
)
def test_parse_invalid_header(self):
self.reader.feed_data(b"HTTP/1.1 500 Internal Server Error\r\nOops\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"invalid HTTP header line: Oops",
)
def test_parse_body(self):
self.reader.feed_data(b"HTTP/1.1 200 OK\r\n\r\nHello world!\n")
gen = self.parse()
self.assertGeneratorRunning(gen)
self.reader.feed_eof()
response = self.assertGeneratorReturns(gen)
self.assertEqual(response.body, b"Hello world!\n")
def test_parse_body_too_large(self):
self.reader.feed_data(b"HTTP/1.1 200 OK\r\n\r\n" + b"a" * 1048577)
with self.assertRaises(SecurityError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"body too large: over 1048576 bytes",
)
def test_parse_body_with_content_length(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello world!\n"
)
response = self.assertGeneratorReturns(self.parse())
self.assertEqual(response.body, b"Hello world!\n")
def test_parse_body_with_content_length_and_body_too_large(self):
self.reader.feed_data(b"HTTP/1.1 200 OK\r\nContent-Length: 1048577\r\n\r\n")
with self.assertRaises(SecurityError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"body too large: 1048577 bytes",
)
def test_parse_body_with_content_length_and_body_way_too_large(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nContent-Length: 1234567890123456789\r\n\r\n"
)
with self.assertRaises(SecurityError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"body too large: 1234567890123456789 bytes",
)
def test_parse_body_with_chunked_transfer_encoding(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
b"6\r\nHello \r\n7\r\nworld!\n\r\n0\r\n\r\n"
)
response = self.assertGeneratorReturns(self.parse())
self.assertEqual(response.body, b"Hello world!\n")
def test_parse_body_with_chunked_transfer_encoding_and_chunk_without_crlf(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
b"6\r\nHello 7\r\nworld!\n0\r\n"
)
with self.assertRaises(ValueError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"chunk without CRLF",
)
def test_parse_body_with_chunked_transfer_encoding_and_chunk_too_large(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
b"100000\r\n" + b"a" * 1048576 + b"\r\n1\r\na\r\n0\r\n\r\n"
)
with self.assertRaises(SecurityError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"chunk too large: 1 bytes after 1048576 bytes",
)
def test_parse_body_with_chunked_transfer_encoding_and_chunk_way_too_large(self):
self.reader.feed_data(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
b"1234567890ABCDEF\r\n\r\n"
)
with self.assertRaises(SecurityError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"chunk too large: 0x1234567890ABCDEF bytes",
)
def test_parse_body_with_unsupported_transfer_encoding(self):
self.reader.feed_data(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: compress\r\n\r\n")
with self.assertRaises(NotImplementedError) as raised:
next(self.parse())
self.assertEqual(
str(raised.exception),
"transfer coding compress isn't supported",
)
def test_parse_body_no_content(self):
self.reader.feed_data(b"HTTP/1.1 204 No Content\r\n\r\n")
response = self.assertGeneratorReturns(self.parse())
self.assertEqual(response.body, b"")
def test_parse_body_not_modified(self):
self.reader.feed_data(b"HTTP/1.1 304 Not Modified\r\n\r\n")
response = self.assertGeneratorReturns(self.parse())
self.assertEqual(response.body, b"")
def test_parse_proxy_response_does_not_read_body(self):
self.reader.feed_data(b"HTTP/1.1 200 Connection Established\r\n\r\n")
response = self.assertGeneratorReturns(self.parse(proxy=True))
self.assertEqual(response.body, b"")
def test_parse_proxy_http10(self):
self.reader.feed_data(b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n")
response = self.assertGeneratorReturns(self.parse(proxy=True))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.reason_phrase, "OK")
self.assertEqual(response.body, b"")
def test_parse_proxy_unsupported_protocol(self):
self.reader.feed_data(b"HTTP/1.2 400 Bad Request\r\n\r\n")
with self.assertRaises(ValueError) as raised:
next(self.parse(proxy=True))
self.assertEqual(
str(raised.exception),
"unsupported protocol; expected HTTP/1.1 or HTTP/1.0: "
"HTTP/1.2 400 Bad Request",
)
def test_serialize(self):
# Example from the protocol overview in RFC 6455
response = Response(
101,
"Switching Protocols",
Headers(
[
("Upgrade", "websocket"),
("Connection", "Upgrade"),
("Sec-WebSocket-Accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="),
("Sec-WebSocket-Protocol", "chat"),
]
),
)
self.assertEqual(
response.serialize(),
b"HTTP/1.1 101 Switching Protocols\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
b"Sec-WebSocket-Protocol: chat\r\n"
b"\r\n",
)
def test_serialize_with_body(self):
response = Response(
200,
"OK",
Headers([("Content-Length", "13"), ("Content-Type", "text/plain")]),
b"Hello world!\n",
)
self.assertEqual(
response.serialize(),
b"HTTP/1.1 200 OK\r\n"
b"Content-Length: 13\r\n"
b"Content-Type: text/plain\r\n"
b"\r\n"
b"Hello world!\n",
)
class HeadersTests(GeneratorTestCase):
def setUp(self):
super().setUp()
self.reader = StreamReader()
def parse_headers(self):
return parse_headers(self.reader.read_line)
def test_parse_invalid_name(self):
self.reader.feed_data(b"foo bar: baz qux\r\n\r\n")
with self.assertRaises(ValueError):
next(self.parse_headers())
def test_parse_invalid_value(self):
self.reader.feed_data(b"foo: \x00\x00\x0f\r\n\r\n")
with self.assertRaises(ValueError):
next(self.parse_headers())
def test_parse_too_long_value(self):
self.reader.feed_data(b"foo: bar\r\n" * 129 + b"\r\n")
with self.assertRaises(SecurityError):
next(self.parse_headers())
def test_parse_too_long_line(self):
# Header line contains 5 + 8186 + 2 = 8193 bytes.
self.reader.feed_data(b"foo: " + b"a" * 8186 + b"\r\n\r\n")
with self.assertRaises(SecurityError):
next(self.parse_headers())
def test_parse_invalid_line_ending(self):
self.reader.feed_data(b"foo: bar\n\n")
with self.assertRaises(EOFError):
next(self.parse_headers())