Skip to content

Commit 07b72d2

Browse files
committed
start tightening up the code
1 parent fae18d8 commit 07b72d2

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

tests/test_mig_wsgi-bin_migwsgi.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,25 @@ def _import_migwsgi():
8282
migwsgi = _import_migwsgi()
8383

8484

85+
def _is_return_value(return_value):
86+
defined_return_values = returnvalues.__dict__.values()
87+
return return_value in defined_return_values
88+
89+
90+
def create_instrumented_retrieve_handler(output_objects=None, return_value=None):
91+
if not output_objects:
92+
output_objects = []
93+
94+
assert isinstance(output_objects, list)
95+
assert _is_return_value(return_value), "return value must be present in returnvalues"
96+
97+
def _instrumented_retrieve_handler(*args):
98+
return [], return_value
99+
return _instrumented_retrieve_handler
100+
101+
85102
def create_instrumented_format_output(arranged):
86-
def instrumented_format_output(
103+
def _instrumented_format_output(
87104
configuration,
88105
backend,
89106
ret_val,
@@ -94,8 +111,7 @@ def instrumented_format_output(
94111
# record the call args
95112
call_args_out_obj = list(out_obj) # capture the original before altering it
96113
call_args = (configuration, backend, ret_val, ret_msg, call_args_out_obj, outputformat,)
97-
instrumented_format_output.calls.append({ 'args': call_args })
98-
114+
_instrumented_format_output.calls.append({ 'args': call_args })
99115

100116
# FIXME: the following is a workaround for a bug that exists between the WSGI wrapper
101117
# and the output formatter - specifically, the latter adds default header and
@@ -130,16 +146,16 @@ def instrumented_format_output(
130146
out_obj,
131147
outputformat,
132148
)
133-
instrumented_format_output.calls = []
134-
return instrumented_format_output
149+
_instrumented_format_output.calls = []
150+
return _instrumented_format_output
135151

136152

137-
def create_wsgi_environ(config_file, wsgi_input=None, env_http_host=None, env_path_info=None):
153+
def create_wsgi_environ(config_file, wsgi_variables):
138154
environ = {}
139155
environ['wsgi.input'] = ()
140156
environ['MIG_CONF'] = config_file
141-
environ['HTTP_HOST'] = env_http_host
142-
environ['PATH_INFO'] = env_path_info
157+
environ['HTTP_HOST'] = wsgi_variables.get('http_host')
158+
environ['PATH_INFO'] = wsgi_variables.get('path_info')
143159
environ['SCRIPT_URI'] = ''.join(('http://', environ['HTTP_HOST'], environ['PATH_INFO']))
144160
return environ
145161

@@ -160,9 +176,6 @@ def test_xxx(self):
160176
config = _assert_local_config()
161177
config_global_values = _assert_local_config_global_values(config)
162178

163-
def fake_handler(*args):
164-
return [], returnvalues.OK
165-
166179
def fake_start_response(status, headers, exc=None):
167180
fake_start_response.calls.append((status, headers, exc))
168181
fake_start_response.calls = []
@@ -171,18 +184,18 @@ def fake_set_environ(value):
171184
fake_set_environ.value = value
172185
fake_set_environ.value = None
173186

174-
wsgi_environ = create_wsgi_environ(
175-
_TEST_CONF_FILE,
176-
env_http_host='localhost',
177-
env_path_info='/'
178-
)
187+
wsgi_environ = create_wsgi_environ(_TEST_CONF_FILE, wsgi_variables=dict(
188+
http_host='localhost',
189+
path_info='/',
190+
))
179191

180192
instrumented_format_output = create_instrumented_format_output('HELLO WORLD')
193+
instrumented_retrieve_handler = create_instrumented_retrieve_handler(None, returnvalues.OK)
181194

182195
yielder = migwsgi._application(wsgi_environ, fake_start_response,
183196
_format_output=instrumented_format_output,
197+
_retrieve_handler=instrumented_retrieve_handler,
184198
_set_environ=fake_set_environ,
185-
_retrieve_handler=lambda _: fake_handler,
186199
_wrap_wsgi_errors=noop,
187200
_config_file=_TEST_CONF_FILE,
188201
_skip_log=True,

0 commit comments

Comments
 (0)