##// END OF EJS Templates
tests: fixed some tests after recent changes
marcink -
r3938:70acf9f6 default
parent child Browse files
Show More
@@ -1,331 +1,332 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2019 RhodeCode GmbH
3 # Copyright (C) 2010-2019 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import json
21 import json
22 import logging
22 import logging
23 from StringIO import StringIO
23 from StringIO import StringIO
24
24
25 import mock
25 import mock
26 import pytest
26 import pytest
27
27
28 from rhodecode.lib import hooks_daemon
28 from rhodecode.lib import hooks_daemon
29 from rhodecode.tests.utils import assert_message_in_log
29 from rhodecode.tests.utils import assert_message_in_log
30
30
31
31
32 class TestDummyHooksCallbackDaemon(object):
32 class TestDummyHooksCallbackDaemon(object):
33 def test_hooks_module_path_set_properly(self):
33 def test_hooks_module_path_set_properly(self):
34 daemon = hooks_daemon.DummyHooksCallbackDaemon()
34 daemon = hooks_daemon.DummyHooksCallbackDaemon()
35 assert daemon.hooks_module == 'rhodecode.lib.hooks_daemon'
35 assert daemon.hooks_module == 'rhodecode.lib.hooks_daemon'
36
36
37 def test_logs_entering_the_hook(self):
37 def test_logs_entering_the_hook(self):
38 daemon = hooks_daemon.DummyHooksCallbackDaemon()
38 daemon = hooks_daemon.DummyHooksCallbackDaemon()
39 with mock.patch.object(hooks_daemon.log, 'debug') as log_mock:
39 with mock.patch.object(hooks_daemon.log, 'debug') as log_mock:
40 with daemon as return_value:
40 with daemon as return_value:
41 log_mock.assert_called_once_with(
41 log_mock.assert_called_once_with(
42 'Running dummy hooks callback daemon')
42 'Running `%s` callback daemon', 'DummyHooksCallbackDaemon')
43 assert return_value == daemon
43 assert return_value == daemon
44
44
45 def test_logs_exiting_the_hook(self):
45 def test_logs_exiting_the_hook(self):
46 daemon = hooks_daemon.DummyHooksCallbackDaemon()
46 daemon = hooks_daemon.DummyHooksCallbackDaemon()
47 with mock.patch.object(hooks_daemon.log, 'debug') as log_mock:
47 with mock.patch.object(hooks_daemon.log, 'debug') as log_mock:
48 with daemon:
48 with daemon:
49 pass
49 pass
50 log_mock.assert_called_with('Exiting dummy hooks callback daemon')
50 log_mock.assert_called_with(
51 'Exiting `%s` callback daemon', 'DummyHooksCallbackDaemon')
51
52
52
53
53 class TestHooks(object):
54 class TestHooks(object):
54 def test_hooks_can_be_used_as_a_context_processor(self):
55 def test_hooks_can_be_used_as_a_context_processor(self):
55 hooks = hooks_daemon.Hooks()
56 hooks = hooks_daemon.Hooks()
56 with hooks as return_value:
57 with hooks as return_value:
57 pass
58 pass
58 assert hooks == return_value
59 assert hooks == return_value
59
60
60
61
61 class TestHooksHttpHandler(object):
62 class TestHooksHttpHandler(object):
62 def test_read_request_parses_method_name_and_arguments(self):
63 def test_read_request_parses_method_name_and_arguments(self):
63 data = {
64 data = {
64 'method': 'test',
65 'method': 'test',
65 'extras': {
66 'extras': {
66 'param1': 1,
67 'param1': 1,
67 'param2': 'a'
68 'param2': 'a'
68 }
69 }
69 }
70 }
70 request = self._generate_post_request(data)
71 request = self._generate_post_request(data)
71 hooks_patcher = mock.patch.object(
72 hooks_patcher = mock.patch.object(
72 hooks_daemon.Hooks, data['method'], create=True, return_value=1)
73 hooks_daemon.Hooks, data['method'], create=True, return_value=1)
73
74
74 with hooks_patcher as hooks_mock:
75 with hooks_patcher as hooks_mock:
75 MockServer(hooks_daemon.HooksHttpHandler, request)
76 MockServer(hooks_daemon.HooksHttpHandler, request)
76
77
77 hooks_mock.assert_called_once_with(data['extras'])
78 hooks_mock.assert_called_once_with(data['extras'])
78
79
79 def test_hooks_serialized_result_is_returned(self):
80 def test_hooks_serialized_result_is_returned(self):
80 request = self._generate_post_request({})
81 request = self._generate_post_request({})
81 rpc_method = 'test'
82 rpc_method = 'test'
82 hook_result = {
83 hook_result = {
83 'first': 'one',
84 'first': 'one',
84 'second': 2
85 'second': 2
85 }
86 }
86 read_patcher = mock.patch.object(
87 read_patcher = mock.patch.object(
87 hooks_daemon.HooksHttpHandler, '_read_request',
88 hooks_daemon.HooksHttpHandler, '_read_request',
88 return_value=(rpc_method, {}))
89 return_value=(rpc_method, {}))
89 hooks_patcher = mock.patch.object(
90 hooks_patcher = mock.patch.object(
90 hooks_daemon.Hooks, rpc_method, create=True,
91 hooks_daemon.Hooks, rpc_method, create=True,
91 return_value=hook_result)
92 return_value=hook_result)
92
93
93 with read_patcher, hooks_patcher:
94 with read_patcher, hooks_patcher:
94 server = MockServer(hooks_daemon.HooksHttpHandler, request)
95 server = MockServer(hooks_daemon.HooksHttpHandler, request)
95
96
96 expected_result = json.dumps(hook_result)
97 expected_result = json.dumps(hook_result)
97 assert server.request.output_stream.buflist[-1] == expected_result
98 assert server.request.output_stream.buflist[-1] == expected_result
98
99
99 def test_exception_is_returned_in_response(self):
100 def test_exception_is_returned_in_response(self):
100 request = self._generate_post_request({})
101 request = self._generate_post_request({})
101 rpc_method = 'test'
102 rpc_method = 'test'
102 read_patcher = mock.patch.object(
103 read_patcher = mock.patch.object(
103 hooks_daemon.HooksHttpHandler, '_read_request',
104 hooks_daemon.HooksHttpHandler, '_read_request',
104 return_value=(rpc_method, {}))
105 return_value=(rpc_method, {}))
105 hooks_patcher = mock.patch.object(
106 hooks_patcher = mock.patch.object(
106 hooks_daemon.Hooks, rpc_method, create=True,
107 hooks_daemon.Hooks, rpc_method, create=True,
107 side_effect=Exception('Test exception'))
108 side_effect=Exception('Test exception'))
108
109
109 with read_patcher, hooks_patcher:
110 with read_patcher, hooks_patcher:
110 server = MockServer(hooks_daemon.HooksHttpHandler, request)
111 server = MockServer(hooks_daemon.HooksHttpHandler, request)
111
112
112 org_exc = json.loads(server.request.output_stream.buflist[-1])
113 org_exc = json.loads(server.request.output_stream.buflist[-1])
113 expected_result = {
114 expected_result = {
114 'exception': 'Exception',
115 'exception': 'Exception',
115 'exception_traceback': org_exc['exception_traceback'],
116 'exception_traceback': org_exc['exception_traceback'],
116 'exception_args': ['Test exception']
117 'exception_args': ['Test exception']
117 }
118 }
118 assert org_exc == expected_result
119 assert org_exc == expected_result
119
120
120 def test_log_message_writes_to_debug_log(self, caplog):
121 def test_log_message_writes_to_debug_log(self, caplog):
121 ip_port = ('0.0.0.0', 8888)
122 ip_port = ('0.0.0.0', 8888)
122 handler = hooks_daemon.HooksHttpHandler(
123 handler = hooks_daemon.HooksHttpHandler(
123 MockRequest('POST /'), ip_port, mock.Mock())
124 MockRequest('POST /'), ip_port, mock.Mock())
124 fake_date = '1/Nov/2015 00:00:00'
125 fake_date = '1/Nov/2015 00:00:00'
125 date_patcher = mock.patch.object(
126 date_patcher = mock.patch.object(
126 handler, 'log_date_time_string', return_value=fake_date)
127 handler, 'log_date_time_string', return_value=fake_date)
127 with date_patcher, caplog.at_level(logging.DEBUG):
128 with date_patcher, caplog.at_level(logging.DEBUG):
128 handler.log_message('Some message %d, %s', 123, 'string')
129 handler.log_message('Some message %d, %s', 123, 'string')
129
130
130 expected_message = '{} - - [{}] Some message 123, string'.format(
131 expected_message = '{} - - [{}] Some message 123, string'.format(
131 ip_port[0], fake_date)
132 ip_port[0], fake_date)
132 assert_message_in_log(
133 assert_message_in_log(
133 caplog.records, expected_message,
134 caplog.records, expected_message,
134 levelno=logging.DEBUG, module='hooks_daemon')
135 levelno=logging.DEBUG, module='hooks_daemon')
135
136
136 def _generate_post_request(self, data):
137 def _generate_post_request(self, data):
137 payload = json.dumps(data)
138 payload = json.dumps(data)
138 return 'POST / HTTP/1.0\nContent-Length: {}\n\n{}'.format(
139 return 'POST / HTTP/1.0\nContent-Length: {}\n\n{}'.format(
139 len(payload), payload)
140 len(payload), payload)
140
141
141
142
142 class ThreadedHookCallbackDaemon(object):
143 class ThreadedHookCallbackDaemon(object):
143 def test_constructor_calls_prepare(self):
144 def test_constructor_calls_prepare(self):
144 prepare_daemon_patcher = mock.patch.object(
145 prepare_daemon_patcher = mock.patch.object(
145 hooks_daemon.ThreadedHookCallbackDaemon, '_prepare')
146 hooks_daemon.ThreadedHookCallbackDaemon, '_prepare')
146 with prepare_daemon_patcher as prepare_daemon_mock:
147 with prepare_daemon_patcher as prepare_daemon_mock:
147 hooks_daemon.ThreadedHookCallbackDaemon()
148 hooks_daemon.ThreadedHookCallbackDaemon()
148 prepare_daemon_mock.assert_called_once_with()
149 prepare_daemon_mock.assert_called_once_with()
149
150
150 def test_run_is_called_on_context_start(self):
151 def test_run_is_called_on_context_start(self):
151 patchers = mock.patch.multiple(
152 patchers = mock.patch.multiple(
152 hooks_daemon.ThreadedHookCallbackDaemon,
153 hooks_daemon.ThreadedHookCallbackDaemon,
153 _run=mock.DEFAULT, _prepare=mock.DEFAULT, __exit__=mock.DEFAULT)
154 _run=mock.DEFAULT, _prepare=mock.DEFAULT, __exit__=mock.DEFAULT)
154
155
155 with patchers as mocks:
156 with patchers as mocks:
156 daemon = hooks_daemon.ThreadedHookCallbackDaemon()
157 daemon = hooks_daemon.ThreadedHookCallbackDaemon()
157 with daemon as daemon_context:
158 with daemon as daemon_context:
158 pass
159 pass
159 mocks['_run'].assert_called_once_with()
160 mocks['_run'].assert_called_once_with()
160 assert daemon_context == daemon
161 assert daemon_context == daemon
161
162
162 def test_stop_is_called_on_context_exit(self):
163 def test_stop_is_called_on_context_exit(self):
163 patchers = mock.patch.multiple(
164 patchers = mock.patch.multiple(
164 hooks_daemon.ThreadedHookCallbackDaemon,
165 hooks_daemon.ThreadedHookCallbackDaemon,
165 _run=mock.DEFAULT, _prepare=mock.DEFAULT, _stop=mock.DEFAULT)
166 _run=mock.DEFAULT, _prepare=mock.DEFAULT, _stop=mock.DEFAULT)
166
167
167 with patchers as mocks:
168 with patchers as mocks:
168 daemon = hooks_daemon.ThreadedHookCallbackDaemon()
169 daemon = hooks_daemon.ThreadedHookCallbackDaemon()
169 with daemon as daemon_context:
170 with daemon as daemon_context:
170 assert mocks['_stop'].call_count == 0
171 assert mocks['_stop'].call_count == 0
171
172
172 mocks['_stop'].assert_called_once_with()
173 mocks['_stop'].assert_called_once_with()
173 assert daemon_context == daemon
174 assert daemon_context == daemon
174
175
175
176
176 class TestHttpHooksCallbackDaemon(object):
177 class TestHttpHooksCallbackDaemon(object):
177 def test_prepare_inits_daemon_variable(self, tcp_server, caplog):
178 def test_prepare_inits_daemon_variable(self, tcp_server, caplog):
178 with self._tcp_patcher(tcp_server), caplog.at_level(logging.DEBUG):
179 with self._tcp_patcher(tcp_server), caplog.at_level(logging.DEBUG):
179 daemon = hooks_daemon.HttpHooksCallbackDaemon()
180 daemon = hooks_daemon.HttpHooksCallbackDaemon()
180 assert daemon._daemon == tcp_server
181 assert daemon._daemon == tcp_server
181
182
182 _, port = tcp_server.server_address
183 _, port = tcp_server.server_address
183 expected_uri = '{}:{}'.format('127.0.0.1', port)
184 expected_uri = '{}:{}'.format('127.0.0.1', port)
184 msg = 'Preparing HTTP callback daemon at `{}` and ' \
185 msg = 'Preparing HTTP callback daemon at `{}` and ' \
185 'registering hook object'.format(expected_uri)
186 'registering hook object'.format(expected_uri)
186 assert_message_in_log(
187 assert_message_in_log(
187 caplog.records, msg, levelno=logging.DEBUG, module='hooks_daemon')
188 caplog.records, msg, levelno=logging.DEBUG, module='hooks_daemon')
188
189
189 def test_prepare_inits_hooks_uri_and_logs_it(
190 def test_prepare_inits_hooks_uri_and_logs_it(
190 self, tcp_server, caplog):
191 self, tcp_server, caplog):
191 with self._tcp_patcher(tcp_server), caplog.at_level(logging.DEBUG):
192 with self._tcp_patcher(tcp_server), caplog.at_level(logging.DEBUG):
192 daemon = hooks_daemon.HttpHooksCallbackDaemon()
193 daemon = hooks_daemon.HttpHooksCallbackDaemon()
193
194
194 _, port = tcp_server.server_address
195 _, port = tcp_server.server_address
195 expected_uri = '{}:{}'.format('127.0.0.1', port)
196 expected_uri = '{}:{}'.format('127.0.0.1', port)
196 assert daemon.hooks_uri == expected_uri
197 assert daemon.hooks_uri == expected_uri
197
198
198 msg = 'Preparing HTTP callback daemon at `{}` and ' \
199 msg = 'Preparing HTTP callback daemon at `{}` and ' \
199 'registering hook object'.format(expected_uri)
200 'registering hook object'.format(expected_uri)
200 assert_message_in_log(
201 assert_message_in_log(
201 caplog.records, msg,
202 caplog.records, msg,
202 levelno=logging.DEBUG, module='hooks_daemon')
203 levelno=logging.DEBUG, module='hooks_daemon')
203
204
204 def test_run_creates_a_thread(self, tcp_server):
205 def test_run_creates_a_thread(self, tcp_server):
205 thread = mock.Mock()
206 thread = mock.Mock()
206
207
207 with self._tcp_patcher(tcp_server):
208 with self._tcp_patcher(tcp_server):
208 daemon = hooks_daemon.HttpHooksCallbackDaemon()
209 daemon = hooks_daemon.HttpHooksCallbackDaemon()
209
210
210 with self._thread_patcher(thread) as thread_mock:
211 with self._thread_patcher(thread) as thread_mock:
211 daemon._run()
212 daemon._run()
212
213
213 thread_mock.assert_called_once_with(
214 thread_mock.assert_called_once_with(
214 target=tcp_server.serve_forever,
215 target=tcp_server.serve_forever,
215 kwargs={'poll_interval': daemon.POLL_INTERVAL})
216 kwargs={'poll_interval': daemon.POLL_INTERVAL})
216 assert thread.daemon is True
217 assert thread.daemon is True
217 thread.start.assert_called_once_with()
218 thread.start.assert_called_once_with()
218
219
219 def test_run_logs(self, tcp_server, caplog):
220 def test_run_logs(self, tcp_server, caplog):
220
221
221 with self._tcp_patcher(tcp_server):
222 with self._tcp_patcher(tcp_server):
222 daemon = hooks_daemon.HttpHooksCallbackDaemon()
223 daemon = hooks_daemon.HttpHooksCallbackDaemon()
223
224
224 with self._thread_patcher(mock.Mock()), caplog.at_level(logging.DEBUG):
225 with self._thread_patcher(mock.Mock()), caplog.at_level(logging.DEBUG):
225 daemon._run()
226 daemon._run()
226
227
227 assert_message_in_log(
228 assert_message_in_log(
228 caplog.records,
229 caplog.records,
229 'Running event loop of callback daemon in background thread',
230 'Running event loop of callback daemon in background thread',
230 levelno=logging.DEBUG, module='hooks_daemon')
231 levelno=logging.DEBUG, module='hooks_daemon')
231
232
232 def test_stop_cleans_up_the_connection(self, tcp_server, caplog):
233 def test_stop_cleans_up_the_connection(self, tcp_server, caplog):
233 thread = mock.Mock()
234 thread = mock.Mock()
234
235
235 with self._tcp_patcher(tcp_server):
236 with self._tcp_patcher(tcp_server):
236 daemon = hooks_daemon.HttpHooksCallbackDaemon()
237 daemon = hooks_daemon.HttpHooksCallbackDaemon()
237
238
238 with self._thread_patcher(thread), caplog.at_level(logging.DEBUG):
239 with self._thread_patcher(thread), caplog.at_level(logging.DEBUG):
239 with daemon:
240 with daemon:
240 assert daemon._daemon == tcp_server
241 assert daemon._daemon == tcp_server
241 assert daemon._callback_thread == thread
242 assert daemon._callback_thread == thread
242
243
243 assert daemon._daemon is None
244 assert daemon._daemon is None
244 assert daemon._callback_thread is None
245 assert daemon._callback_thread is None
245 tcp_server.shutdown.assert_called_with()
246 tcp_server.shutdown.assert_called_with()
246 thread.join.assert_called_once_with()
247 thread.join.assert_called_once_with()
247
248
248 assert_message_in_log(
249 assert_message_in_log(
249 caplog.records, 'Waiting for background thread to finish.',
250 caplog.records, 'Waiting for background thread to finish.',
250 levelno=logging.DEBUG, module='hooks_daemon')
251 levelno=logging.DEBUG, module='hooks_daemon')
251
252
252 def _tcp_patcher(self, tcp_server):
253 def _tcp_patcher(self, tcp_server):
253 return mock.patch.object(
254 return mock.patch.object(
254 hooks_daemon, 'TCPServer', return_value=tcp_server)
255 hooks_daemon, 'TCPServer', return_value=tcp_server)
255
256
256 def _thread_patcher(self, thread):
257 def _thread_patcher(self, thread):
257 return mock.patch.object(
258 return mock.patch.object(
258 hooks_daemon.threading, 'Thread', return_value=thread)
259 hooks_daemon.threading, 'Thread', return_value=thread)
259
260
260
261
261 class TestPrepareHooksDaemon(object):
262 class TestPrepareHooksDaemon(object):
262 @pytest.mark.parametrize('protocol', ('http',))
263 @pytest.mark.parametrize('protocol', ('http',))
263 def test_returns_dummy_hooks_callback_daemon_when_using_direct_calls(
264 def test_returns_dummy_hooks_callback_daemon_when_using_direct_calls(
264 self, protocol):
265 self, protocol):
265 expected_extras = {'extra1': 'value1'}
266 expected_extras = {'extra1': 'value1'}
266 callback, extras = hooks_daemon.prepare_callback_daemon(
267 callback, extras = hooks_daemon.prepare_callback_daemon(
267 expected_extras.copy(), protocol=protocol,
268 expected_extras.copy(), protocol=protocol,
268 host='127.0.0.1', use_direct_calls=True)
269 host='127.0.0.1', use_direct_calls=True)
269 assert isinstance(callback, hooks_daemon.DummyHooksCallbackDaemon)
270 assert isinstance(callback, hooks_daemon.DummyHooksCallbackDaemon)
270 expected_extras['hooks_module'] = 'rhodecode.lib.hooks_daemon'
271 expected_extras['hooks_module'] = 'rhodecode.lib.hooks_daemon'
271 expected_extras['time'] = extras['time']
272 expected_extras['time'] = extras['time']
272 assert 'extra1' in extras
273 assert 'extra1' in extras
273
274
274 @pytest.mark.parametrize('protocol, expected_class', (
275 @pytest.mark.parametrize('protocol, expected_class', (
275 ('http', hooks_daemon.HttpHooksCallbackDaemon),
276 ('http', hooks_daemon.HttpHooksCallbackDaemon),
276 ))
277 ))
277 def test_returns_real_hooks_callback_daemon_when_protocol_is_specified(
278 def test_returns_real_hooks_callback_daemon_when_protocol_is_specified(
278 self, protocol, expected_class):
279 self, protocol, expected_class):
279 expected_extras = {
280 expected_extras = {
280 'extra1': 'value1',
281 'extra1': 'value1',
281 'txn_id': 'txnid2',
282 'txn_id': 'txnid2',
282 'hooks_protocol': protocol.lower()
283 'hooks_protocol': protocol.lower()
283 }
284 }
284 callback, extras = hooks_daemon.prepare_callback_daemon(
285 callback, extras = hooks_daemon.prepare_callback_daemon(
285 expected_extras.copy(), protocol=protocol, host='127.0.0.1',
286 expected_extras.copy(), protocol=protocol, host='127.0.0.1',
286 use_direct_calls=False,
287 use_direct_calls=False,
287 txn_id='txnid2')
288 txn_id='txnid2')
288 assert isinstance(callback, expected_class)
289 assert isinstance(callback, expected_class)
289 extras.pop('hooks_uri')
290 extras.pop('hooks_uri')
290 expected_extras['time'] = extras['time']
291 expected_extras['time'] = extras['time']
291 assert extras == expected_extras
292 assert extras == expected_extras
292
293
293 @pytest.mark.parametrize('protocol', (
294 @pytest.mark.parametrize('protocol', (
294 'invalid',
295 'invalid',
295 'Http',
296 'Http',
296 'HTTP',
297 'HTTP',
297 ))
298 ))
298 def test_raises_on_invalid_protocol(self, protocol):
299 def test_raises_on_invalid_protocol(self, protocol):
299 expected_extras = {
300 expected_extras = {
300 'extra1': 'value1',
301 'extra1': 'value1',
301 'hooks_protocol': protocol.lower()
302 'hooks_protocol': protocol.lower()
302 }
303 }
303 with pytest.raises(Exception):
304 with pytest.raises(Exception):
304 callback, extras = hooks_daemon.prepare_callback_daemon(
305 callback, extras = hooks_daemon.prepare_callback_daemon(
305 expected_extras.copy(),
306 expected_extras.copy(),
306 protocol=protocol, host='127.0.0.1',
307 protocol=protocol, host='127.0.0.1',
307 use_direct_calls=False)
308 use_direct_calls=False)
308
309
309
310
310 class MockRequest(object):
311 class MockRequest(object):
311 def __init__(self, request):
312 def __init__(self, request):
312 self.request = request
313 self.request = request
313 self.input_stream = StringIO(b'{}'.format(self.request))
314 self.input_stream = StringIO(b'{}'.format(self.request))
314 self.output_stream = StringIO()
315 self.output_stream = StringIO()
315
316
316 def makefile(self, mode, *args, **kwargs):
317 def makefile(self, mode, *args, **kwargs):
317 return self.output_stream if mode == 'wb' else self.input_stream
318 return self.output_stream if mode == 'wb' else self.input_stream
318
319
319
320
320 class MockServer(object):
321 class MockServer(object):
321 def __init__(self, Handler, request):
322 def __init__(self, Handler, request):
322 ip_port = ('0.0.0.0', 8888)
323 ip_port = ('0.0.0.0', 8888)
323 self.request = MockRequest(request)
324 self.request = MockRequest(request)
324 self.handler = Handler(self.request, ip_port, self)
325 self.handler = Handler(self.request, ip_port, self)
325
326
326
327
327 @pytest.fixture
328 @pytest.fixture
328 def tcp_server():
329 def tcp_server():
329 server = mock.Mock()
330 server = mock.Mock()
330 server.server_address = ('127.0.0.1', 8881)
331 server.server_address = ('127.0.0.1', 8881)
331 return server
332 return server
General Comments 0
You need to be logged in to leave comments. Login now