Skip to content

Commit ec89bfd

Browse files
committed
fixup
1 parent 2c5f458 commit ec89bfd

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

mig/wsgi-bin/migwsgi.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
from mig.shared.scriptinput import fieldstorage_to_dict
4646

4747
if PY2:
48-
pass
48+
def _ensure_wsgi_chunk(chunk):
49+
return chunk
4950
else:
5051
def _ensure_wsgi_chunk(chunk):
5152
return codecs.encode(chunk, 'utf8')
@@ -213,7 +214,7 @@ def _set_os_environ(value):
213214

214215
return _application(environ, start_response, _set_environ=_set_os_environ, _wrap_wsgi_errors=wrap_wsgi_errors)
215216

216-
def _application(environ, start_response, _set_environ, _retrieve_handler=_import_backend, _wrap_wsgi_errors=True, _config_file=None, _skip_log=False):
217+
def _application(environ, start_response, _set_environ, _format_output=format_output, _retrieve_handler=_import_backend, _wrap_wsgi_errors=True, _config_file=None, _skip_log=False):
217218

218219
# NOTE: pass app environ including apache and query args on to sub handlers
219220
# through the usual 'os.environ' channel expected in functionality
@@ -383,7 +384,7 @@ def _application(environ, start_response, _set_environ, _retrieve_handler=_impor
383384
output_objs.append(wsgi_entry)
384385

385386
_logger.debug("call format %r output to %s" % (backend, output_format))
386-
output = format_output(configuration, backend, ret_code, ret_msg,
387+
output = _format_output(configuration, backend, ret_code, ret_msg,
387388
output_objs, output_format)
388389
# _logger.debug("formatted %s output to %s" % (backend, output_format))
389390
# _logger.debug("output:\n%s" % [output])
@@ -392,7 +393,7 @@ def _application(environ, start_response, _set_environ, _retrieve_handler=_impor
392393
_logger.error(
393394
"Formatted output is NOT on default str coding: %s" % [output[:100]])
394395
err_mark = '__****__'
395-
output = format_output(configuration, backend, ret_code, ret_msg,
396+
output = _format_output(configuration, backend, ret_code, ret_msg,
396397
force_default_str_coding_rec(
397398
output_objs, highlight=err_mark),
398399
output_format)

tests/test_mig_wsgi-bin_migwsgi.py

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,17 @@
2626

2727
"""Unit tests for the MiG WSGI glue"""
2828

29+
import codecs
2930
from configparser import ConfigParser
3031
import importlib
3132
import os
3233
import stat
3334
import sys
3435

3536
from tests.support import MIG_BASE, MigTestCase, testmain
37+
from mig.shared.output import format_output
3638
import mig.shared.returnvalues as returnvalues
3739

38-
def create_output_returner(arranged=None):
39-
def test_format_output(*args):
40-
return arranged
41-
return test_format_output
42-
43-
def create_wsgi_environ(config_file, wsgi_input=None, env_http_host=None, env_path_info=None):
44-
environ = {}
45-
environ['wsgi.input'] = ()
46-
environ['MIG_CONF'] = config_file
47-
environ['HTTP_HOST'] = env_http_host
48-
environ['PATH_INFO'] = env_path_info
49-
environ['SCRIPT_URI'] = ''.join(('http://', environ['HTTP_HOST'], environ['PATH_INFO']))
50-
return environ
51-
52-
53-
def noop(*args):
54-
pass
55-
5640

5741
from tests.support import PY2, is_path_within
5842
from mig.shared.base import client_id_dir, client_dir_id, get_short_id, \
@@ -90,19 +74,92 @@ def _assert_local_config_global_values(config):
9074
return config_global_values
9175

9276

93-
_WSGI_BIN = os.path.join(MIG_BASE, 'mig/wsgi-bin')
94-
9577
def _import_migwsgi():
96-
sys.path.append(_WSGI_BIN)
78+
sys.path.append(os.path.join(MIG_BASE, 'mig/wsgi-bin'))
9779
migwsgi = importlib.import_module('migwsgi')
9880
sys.path.pop(-1)
9981
return migwsgi
82+
migwsgi = _import_migwsgi()
10083

10184

102-
migwsgi = _import_migwsgi()
85+
def create_instrumented_format_output(arranged):
86+
def instrumented_format_output(
87+
configuration,
88+
backend,
89+
ret_val,
90+
ret_msg,
91+
out_obj,
92+
outputformat,
93+
):
94+
# record the call args
95+
call_args_out_obj = list(out_obj) # capture the original before altering it
96+
call_args = (configuration, backend, ret_val, ret_msg, call_args_out_obj, outputformat,)
97+
instrumented_format_output.calls.append({ 'args': call_args })
98+
99+
100+
# FIXME: the following is a workaround for a bug that exists between the WSGI wrapper
101+
# and the output formatter - specifically, the latter adds default header and
102+
# title if start does not exist, but the former ensures that start always exists
103+
# meaning that a default response under WSGI is missing half the HTML.
104+
start_obj_idx = next((i for i, obj in enumerate(out_obj) if obj['object_type'] == 'start'))
105+
insertion_idx = start_obj_idx
106+
107+
insertion_idx += 1
108+
out_obj.insert(insertion_idx, {
109+
'object_type': 'title',
110+
'text': arranged,
111+
'meta': '',
112+
'style': {},
113+
'script': {},
114+
})
115+
116+
# FIXME: format_output() will write the header _before_ the preamble unless there some
117+
# other non-special output object prior to it.
118+
# insertion_idx += 1
119+
# out_obj.insert(insertion_idx, {
120+
# 'object_type': '__FORCEPREAMBLE__',
121+
# })
122+
123+
insertion_idx += 1
124+
out_obj.insert(insertion_idx, {
125+
'object_type': 'header',
126+
'text': arranged
127+
})
128+
129+
return format_output(
130+
configuration,
131+
backend,
132+
ret_val,
133+
ret_msg,
134+
out_obj,
135+
outputformat,
136+
)
137+
instrumented_format_output.calls = []
138+
return instrumented_format_output
139+
140+
141+
def create_wsgi_environ(config_file, wsgi_input=None, env_http_host=None, env_path_info=None):
142+
environ = {}
143+
environ['wsgi.input'] = ()
144+
environ['MIG_CONF'] = config_file
145+
environ['HTTP_HOST'] = env_http_host
146+
environ['PATH_INFO'] = env_path_info
147+
environ['SCRIPT_URI'] = ''.join(('http://', environ['HTTP_HOST'], environ['PATH_INFO']))
148+
return environ
149+
150+
151+
def noop(*args):
152+
pass
103153

104154

105155
class MigSharedConfiguration(MigTestCase):
156+
def assertHtmlBasics(self, value):
157+
assert isinstance(value, type(u""))
158+
assert value.startswith("<!DOCTYPE")
159+
end_html_tag_idx = value.rfind('</html>')
160+
maybe_document_end = value[end_html_tag_idx:].rstrip()
161+
self.assertEqual(maybe_document_end, '</html>')
162+
106163
def test_xxx(self):
107164
config = _assert_local_config()
108165
config_global_values = _assert_local_config_global_values(config)
@@ -124,9 +181,10 @@ def fake_set_environ(value):
124181
env_path_info='/'
125182
)
126183

127-
test_output_returner = create_output_returner('HELLO WORLD')
184+
instrumented_format_output = create_instrumented_format_output('HELLO WORLD')
128185

129186
yielder = migwsgi._application(wsgi_environ, fake_start_response,
187+
_format_output=instrumented_format_output,
130188
_set_environ=fake_set_environ,
131189
_retrieve_handler=lambda _: fake_handler,
132190
_wrap_wsgi_errors=noop,
@@ -136,8 +194,8 @@ def fake_set_environ(value):
136194
chunks = list(yielder)
137195

138196
self.assertGreater(len(chunks), 0)
139-
import codecs
140-
print(codecs.decode(chunks[0], 'utf8'))
197+
value = codecs.decode(chunks[0], 'utf8')
198+
self.assertHtmlBasics(value)
141199

142200

143201
if __name__ == '__main__':

0 commit comments

Comments
 (0)