38
38
import mig .shared .returnvalues as returnvalues
39
39
40
40
41
- from tests .support import PY2 , is_path_within
41
+ from tests .support import PY2 , is_path_within , \
42
+ create_wsgi_environ , ServerAssertMixin , FakeStartResponse , HtmlAssertMixin
42
43
from mig .shared .base import client_id_dir , client_dir_id , get_short_id , \
43
44
invisible_path , allow_script , brief_list
44
45
@@ -179,21 +180,11 @@ def _instrumented_retrieve_handler(*args):
179
180
return _instrumented_retrieve_handler
180
181
181
182
182
- def create_wsgi_environ (config_file , wsgi_variables ):
183
- environ = {}
184
- environ ['wsgi.input' ] = ()
185
- environ ['MIG_CONF' ] = config_file
186
- environ ['HTTP_HOST' ] = wsgi_variables .get ('http_host' )
187
- environ ['PATH_INFO' ] = wsgi_variables .get ('path_info' )
188
- environ ['SCRIPT_URI' ] = '' .join (('http://' , environ ['HTTP_HOST' ], environ ['PATH_INFO' ]))
189
- return environ
190
-
191
-
192
183
def noop (* args ):
193
184
pass
194
185
195
186
196
- class MigWsgi_binMigwsgi (MigTestCase ):
187
+ class MigWsgi_binMigwsgi (MigTestCase , ServerAssertMixin , HtmlAssertMixin ):
197
188
def assertInstrumentation (self ):
198
189
simulated_action = self .instrumented_retrieve_handler .simulated
199
190
self .assertIsNotNone (simulated_action .returning , "no response programmed" )
@@ -205,44 +196,11 @@ def was_called(fake):
205
196
self .assertTrue (was_called (self .instrumented_format_output ), "no output generated" )
206
197
self .assertTrue (was_called (self .instrumented_retrieve_handler ), "no output generated" )
207
198
208
- def assertResponseStatus (self , expected_status_code ):
209
- def called_once (fake ):
210
- assert hasattr (fake , 'calls' )
211
- return len (fake .calls ) == 1
212
-
213
- self .assertTrue (called_once (self .fake_start_response ))
214
- thecall = self .fake_start_response .calls [0 ]
215
- wsgi_status = thecall [0 ]
216
- actual_status_code = int (wsgi_status [0 :3 ])
217
- self .assertEqual (actual_status_code , expected_status_code )
218
-
219
- def assertHtmlElementTextContent (self , output , tag_name , expected_text , trim_newlines = True ):
220
- # TODO: this is a definitively stop-gap way of finding a tag within the HTML
221
- # and is used purely to keep this initial change to a reasonable size.
222
- tag_open = '' .join (['<' , tag_name , '>' ])
223
- tag_open_index = output .index (tag_open )
224
- tag_close = '' .join (['</' , tag_name , '>' ])
225
- tag_close_index = output .index (tag_close )
226
- actual_text = output [tag_open_index + len (tag_open ):tag_close_index ]
227
- if trim_newlines :
228
- actual_text = actual_text .strip ('\n ' )
229
- self .assertEqual (actual_text , expected_text )
230
-
231
- def assertIsValidHtmlDocument (self , value ):
232
- assert isinstance (value , type (u"" ))
233
- assert value .startswith ("<!DOCTYPE" )
234
- end_html_tag_idx = value .rfind ('</html>' )
235
- maybe_document_end = value [end_html_tag_idx :].rstrip ()
236
- self .assertEqual (maybe_document_end , '</html>' )
237
-
238
199
def before_each (self ):
239
200
config = _assert_local_config ()
240
201
config_global_values = _assert_local_config_global_values (config )
241
202
242
- def fake_start_response (status , headers , exc = None ):
243
- fake_start_response .calls .append ((status , headers , exc ))
244
- fake_start_response .calls = []
245
- self .fake_start_response = fake_start_response
203
+ self .fake_start_response = FakeStartResponse ()
246
204
247
205
def fake_set_environ (value ):
248
206
fake_set_environ .calls .append ((value ))
@@ -257,7 +215,7 @@ def fake_set_environ(value):
257
215
self .instrumented_format_output = create_instrumented_format_output ()
258
216
self .instrumented_retrieve_handler = create_instrumented_retrieve_handler ()
259
217
260
- self .application_args = (fake_wsgi_environ , fake_start_response ,)
218
+ self .application_args = (fake_wsgi_environ , self . fake_start_response ,)
261
219
self .application_kwargs = dict (
262
220
_format_output = self .instrumented_format_output ,
263
221
_retrieve_handler = self .instrumented_retrieve_handler ,
@@ -278,7 +236,7 @@ def test_return_value_ok_returns_status_200(self):
278
236
_trigger_and_unpack_result (application_result )
279
237
280
238
self .assertInstrumentation ()
281
- self .assertResponseStatus ( 200 )
239
+ self .assertWsgiResponseStatus ( self . fake_start_response , 200 )
282
240
283
241
def test_return_value_ok_returns_valid_html_page (self ):
284
242
self .instrumented_retrieve_handler .program ([], returnvalues .OK )
0 commit comments