|
| 1 | +import unittest |
| 2 | + |
| 3 | +import tulip |
| 4 | +import tulip.test_utils |
| 5 | + |
| 6 | +from .http import * |
| 7 | + |
| 8 | + |
| 9 | +class HTTPTests(tulip.test_utils.LogTrackingTestCase, unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + super().setUp() |
| 13 | + self.loop = tulip.new_event_loop() |
| 14 | + tulip.set_event_loop(self.loop) |
| 15 | + self.stream = tulip.StreamReader() |
| 16 | + |
| 17 | + def tearDown(self): |
| 18 | + self.loop.close() |
| 19 | + super().tearDown() |
| 20 | + |
| 21 | + def test_read_request(self): |
| 22 | + # Example from the protocol overview in RFC 6455 |
| 23 | + self.stream.feed_data( |
| 24 | + b'GET /chat HTTP/1.1\r\n' |
| 25 | + b'Host: server.example.com\r\n' |
| 26 | + b'Upgrade: websocket\r\n' |
| 27 | + b'Connection: Upgrade\r\n' |
| 28 | + b'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n' |
| 29 | + b'Origin: http://example.com\r\n' |
| 30 | + b'Sec-WebSocket-Protocol: chat, superchat\r\n' |
| 31 | + b'Sec-WebSocket-Version: 13\r\n' |
| 32 | + b'\r\n' |
| 33 | + ) |
| 34 | + uri, hdrs = self.loop.run_until_complete(read_request(self.stream)) |
| 35 | + self.assertEqual(uri, '/chat') |
| 36 | + self.assertEqual(hdrs['Upgrade'], 'websocket') |
| 37 | + |
| 38 | + def test_read_response(self): |
| 39 | + # Example from the protocol overview in RFC 6455 |
| 40 | + self.stream.feed_data( |
| 41 | + b'HTTP/1.1 101 Switching Protocols\r\n' |
| 42 | + b'Upgrade: websocket\r\n' |
| 43 | + b'Connection: Upgrade\r\n' |
| 44 | + b'Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n' |
| 45 | + b'Sec-WebSocket-Protocol: chat\r\n' |
| 46 | + b'\r\n' |
| 47 | + ) |
| 48 | + status, hdrs = self.loop.run_until_complete(read_response(self.stream)) |
| 49 | + self.assertEqual(status, 101) |
| 50 | + self.assertEqual(hdrs['Upgrade'], 'websocket') |
| 51 | + |
| 52 | + def test_method(self): |
| 53 | + self.suppress_log_errors() |
| 54 | + self.stream.feed_data(b'OPTIONS * HTTP/1.1\r\n\r\n') |
| 55 | + with self.assertRaises(ValueError): |
| 56 | + self.loop.run_until_complete(read_request(self.stream)) |
| 57 | + |
| 58 | + def test_version(self): |
| 59 | + self.suppress_log_errors() |
| 60 | + self.stream.feed_data(b'GET /chat HTTP/1.0\r\n\r\n') |
| 61 | + with self.assertRaises(ValueError): |
| 62 | + self.loop.run_until_complete(read_request(self.stream)) |
| 63 | + self.stream.feed_data(b'HTTP/1.0 400 Bad Request\r\n\r\n') |
| 64 | + with self.assertRaises(ValueError): |
| 65 | + self.loop.run_until_complete(read_response(self.stream)) |
| 66 | + |
| 67 | + def test_headers_limit(self): |
| 68 | + self.suppress_log_errors() |
| 69 | + self.stream.feed_data(b'foo: bar\r\n' * 500 + b'\r\n') |
| 70 | + with self.assertRaises(ValueError): |
| 71 | + self.loop.run_until_complete(read_message(self.stream)) |
| 72 | + |
| 73 | + def test_line_limit(self): |
| 74 | + self.suppress_log_errors() |
| 75 | + self.stream.feed_data(b'a' * 5000 + b'\r\n\r\n') |
| 76 | + with self.assertRaises(ValueError): |
| 77 | + self.loop.run_until_complete(read_message(self.stream)) |
| 78 | + |
| 79 | + def test_line_ending(self): |
| 80 | + self.suppress_log_errors() |
| 81 | + self.stream.feed_data(b'GET / HTTP/1.1\n\n') |
| 82 | + with self.assertRaises(ValueError): |
| 83 | + self.loop.run_until_complete(read_message(self.stream)) |
0 commit comments