summaryrefslogtreecommitdiff
path: root/tests/http_server_test.py
blob: dc55eff9581e6b088cd61f8f107ea1991615e6db (plain)
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
"""Tests for http/server.py"""

import unittest
import unittest.mock

import tulip
from tulip.http import server
from tulip.http import errors
from tulip.test_utils import LogTrackingTestCase


class HttpServerProtocolTests(LogTrackingTestCase):

    def setUp(self):
        super().setUp()
        self.suppress_log_errors()

        self.loop = tulip.new_event_loop()
        tulip.set_event_loop(self.loop)

    def tearDown(self):
        self.loop.close()
        super().tearDown()

    def test_http_status_exception(self):
        exc = errors.HttpStatusException(500, message='Internal error')
        self.assertEqual(exc.code, 500)
        self.assertEqual(exc.message, 'Internal error')

    def test_handle_request(self):
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        rline = unittest.mock.Mock()
        rline.version = (1, 1)
        message = unittest.mock.Mock()
        srv.handle_request(rline, message)

        content = b''.join([c[1][0] for c in list(transport.write.mock_calls)])
        self.assertTrue(content.startswith(b'HTTP/1.1 404 Not Found\r\n'))

    def test_connection_made(self):
        srv = server.ServerHttpProtocol()
        self.assertIsNone(srv._request_handle)

        srv.connection_made(unittest.mock.Mock())
        self.assertIsNotNone(srv._request_handle)

    def test_data_received(self):
        srv = server.ServerHttpProtocol()
        srv.connection_made(unittest.mock.Mock())

        srv.data_received(b'123')
        self.assertEqual(b'123', b''.join(srv.stream.buffer))

        srv.data_received(b'456')
        self.assertEqual(b'123456', b''.join(srv.stream.buffer))

    def test_eof_received(self):
        srv = server.ServerHttpProtocol()
        srv.connection_made(unittest.mock.Mock())
        srv.eof_received()
        self.assertTrue(srv.stream.eof)

    def test_connection_lost(self):
        srv = server.ServerHttpProtocol()
        srv.connection_made(unittest.mock.Mock())
        srv.data_received(b'123')

        handle = srv._request_handle
        srv.connection_lost(None)

        self.assertIsNone(srv._request_handle)
        self.assertTrue(handle.cancelled())

        srv.connection_lost(None)
        self.assertIsNone(srv._request_handle)

    def test_close(self):
        srv = server.ServerHttpProtocol()
        self.assertFalse(srv.closing)

        srv.close()
        self.assertTrue(srv.closing)

    def test_handle_error(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        srv.handle_error(404)
        content = b''.join([c[1][0] for c in list(transport.write.mock_calls)])
        self.assertIn(b'HTTP/1.1 404 Not Found', content)

    @unittest.mock.patch('tulip.http.server.traceback')
    def test_handle_error_traceback_exc(self, m_trace):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol(debug=True)
        srv.connection_made(transport)

        m_trace.format_exc.side_effect = ValueError

        srv.handle_error(500, exc=object())
        content = b''.join([c[1][0] for c in list(transport.write.mock_calls)])
        self.assertTrue(
            content.startswith(b'HTTP/1.1 500 Internal Server Error'))

    def test_handle_error_debug(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.debug = True
        srv.connection_made(transport)

        try:
            raise ValueError()
        except Exception as exc:
            srv.handle_error(999, exc=exc)

        content = b''.join([c[1][0] for c in list(transport.write.mock_calls)])

        self.assertIn(b'HTTP/1.1 500 Internal', content)
        self.assertIn(b'Traceback (most recent call last):', content)

    def test_handle_error_500(self):
        log = unittest.mock.Mock()
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol(log=log)
        srv.connection_made(transport)

        srv.handle_error(500)
        self.assertTrue(log.exception.called)

    def test_handle(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        handle = srv.handle_request = unittest.mock.Mock()

        srv.stream.feed_data(
            b'GET / HTTP/1.0\r\n'
            b'Host: example.com\r\n\r\n')
        srv.close()

        self.loop.run_until_complete(srv._request_handle)
        self.assertTrue(handle.called)
        self.assertIsNone(srv._request_handle)

    def test_handle_coro(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        called = False

        @tulip.coroutine
        def coro(rline, message):
            nonlocal called
            called = True
            yield from []
            srv.eof_received()

        srv.handle_request = coro

        srv.stream.feed_data(
            b'GET / HTTP/1.0\r\n'
            b'Host: example.com\r\n\r\n')
        self.loop.run_until_complete(srv._request_handle)
        self.assertTrue(called)

    def test_handle_close(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        handle = srv.handle_request = unittest.mock.Mock()

        srv.stream.feed_data(
            b'GET / HTTP/1.0\r\n'
            b'Host: example.com\r\n\r\n')
        srv.close()
        self.loop.run_until_complete(srv._request_handle)

        self.assertTrue(handle.called)
        self.assertTrue(transport.close.called)

    def test_handle_cancel(self):
        log = unittest.mock.Mock()
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol(log=log, debug=True)
        srv.connection_made(transport)

        srv.handle_request = unittest.mock.Mock()

        @tulip.task
        def cancel():
            yield from []
            srv._request_handle.cancel()

        srv.close()
        self.loop.run_until_complete(
            tulip.wait([srv._request_handle, cancel()]))
        self.assertTrue(log.debug.called)

    def test_handle_400(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)
        srv.handle_error = unittest.mock.Mock()

        def side_effect(*args):
            srv.close()
        srv.handle_error.side_effect = side_effect

        srv.stream.feed_data(b'GET / HT/asd\r\n')

        self.loop.run_until_complete(srv._request_handle)
        self.assertTrue(srv.handle_error.called)
        self.assertTrue(400, srv.handle_error.call_args[0][0])
        self.assertTrue(transport.close.called)

    def test_handle_500(self):
        transport = unittest.mock.Mock()
        srv = server.ServerHttpProtocol()
        srv.connection_made(transport)

        handle = srv.handle_request = unittest.mock.Mock()
        handle.side_effect = ValueError
        srv.handle_error = unittest.mock.Mock()
        srv.close()

        srv.stream.feed_data(
            b'GET / HTTP/1.0\r\n'
            b'Host: example.com\r\n\r\n')
        self.loop.run_until_complete(srv._request_handle)

        self.assertTrue(srv.handle_error.called)
        self.assertTrue(500, srv.handle_error.call_args[0][0])