26
26
27
27
"""Unit tests for the MiG WSGI glue"""
28
28
29
+ import codecs
29
30
from configparser import ConfigParser
30
31
import importlib
31
32
import os
32
33
import stat
33
34
import sys
34
35
35
36
from tests .support import MIG_BASE , MigTestCase , testmain
37
+ from mig .shared .output import format_output
36
38
import mig .shared .returnvalues as returnvalues
37
39
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
-
56
40
57
41
from tests .support import PY2 , is_path_within
58
42
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):
90
74
return config_global_values
91
75
92
76
93
- _WSGI_BIN = os .path .join (MIG_BASE , 'mig/wsgi-bin' )
94
-
95
77
def _import_migwsgi ():
96
- sys .path .append (_WSGI_BIN )
78
+ sys .path .append (os . path . join ( MIG_BASE , 'mig/wsgi-bin' ) )
97
79
migwsgi = importlib .import_module ('migwsgi' )
98
80
sys .path .pop (- 1 )
99
81
return migwsgi
82
+ migwsgi = _import_migwsgi ()
100
83
101
84
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
103
153
104
154
105
155
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
+
106
163
def test_xxx (self ):
107
164
config = _assert_local_config ()
108
165
config_global_values = _assert_local_config_global_values (config )
@@ -124,9 +181,10 @@ def fake_set_environ(value):
124
181
env_path_info = '/'
125
182
)
126
183
127
- test_output_returner = create_output_returner ('HELLO WORLD' )
184
+ instrumented_format_output = create_instrumented_format_output ('HELLO WORLD' )
128
185
129
186
yielder = migwsgi ._application (wsgi_environ , fake_start_response ,
187
+ _format_output = instrumented_format_output ,
130
188
_set_environ = fake_set_environ ,
131
189
_retrieve_handler = lambda _ : fake_handler ,
132
190
_wrap_wsgi_errors = noop ,
@@ -136,8 +194,8 @@ def fake_set_environ(value):
136
194
chunks = list (yielder )
137
195
138
196
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 )
141
199
142
200
143
201
if __name__ == '__main__' :
0 commit comments