Skip to content

Commit c7c82f5

Browse files
committed
assert that a programmed title ends up in the page
1 parent b1e54b9 commit c7c82f5

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

tests/test_mig_wsgi-bin_migwsgi.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _trigger_and_unpack_result(application_result):
9696
return decoded_value
9797

9898

99-
def create_instrumented_format_output(arranged):
99+
def create_instrumented_format_output():
100100
def _instrumented_format_output(
101101
configuration,
102102
backend,
@@ -123,7 +123,7 @@ def _instrumented_format_output(
123123
insertion_idx += 1
124124
out_obj.insert(insertion_idx, {
125125
'object_type': 'title',
126-
'text': arranged,
126+
'text': _instrumented_format_output.values['title_text'],
127127
'meta': '',
128128
'style': {},
129129
'script': {},
@@ -132,7 +132,7 @@ def _instrumented_format_output(
132132
insertion_idx += 1
133133
out_obj.insert(insertion_idx, {
134134
'object_type': 'header',
135-
'text': arranged
135+
'text': _instrumented_format_output.values['header_text']
136136
})
137137

138138
return format_output(
@@ -144,6 +144,16 @@ def _instrumented_format_output(
144144
outputformat,
145145
)
146146
_instrumented_format_output.calls = []
147+
_instrumented_format_output.values = dict(
148+
title_text='',
149+
header_text='',
150+
)
151+
152+
def _program_values(**kwargs):
153+
_instrumented_format_output.values.update(kwargs)
154+
155+
_instrumented_format_output.set_values = _program_values
156+
147157
return _instrumented_format_output
148158

149159

@@ -206,6 +216,18 @@ def called_once(fake):
206216
actual_status_code = int(wsgi_status[0:3])
207217
self.assertEqual(actual_status_code, expected_status_code)
208218

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+
209231
def assertIsValidHtmlDocument(self, value):
210232
assert isinstance(value, type(u""))
211233
assert value.startswith("<!DOCTYPE")
@@ -232,7 +254,7 @@ def fake_set_environ(value):
232254
path_info='/',
233255
))
234256

235-
self.instrumented_format_output = create_instrumented_format_output('HELLO WORLD')
257+
self.instrumented_format_output = create_instrumented_format_output()
236258
self.instrumented_retrieve_handler = create_instrumented_retrieve_handler()
237259

238260
self.application_args = (fake_wsgi_environ, fake_start_response,)
@@ -258,7 +280,7 @@ def test_return_value_ok_returns_status_200(self):
258280
self.assertInstrumentation()
259281
self.assertResponseStatus(200)
260282

261-
def test_return_value_ok_creates_valid_html_page(self):
283+
def test_return_value_ok_returns_valid_html_page(self):
262284
self.instrumented_retrieve_handler.program([], returnvalues.OK)
263285

264286
application_result = migwsgi._application(
@@ -274,6 +296,23 @@ def test_return_value_ok_creates_valid_html_page(self):
274296
self.assertInstrumentation()
275297
self.assertIsValidHtmlDocument(output)
276298

299+
def test_return_value_ok_returns_expected_title(self):
300+
self.instrumented_format_output.set_values(title_text='TEST')
301+
self.instrumented_retrieve_handler.program([], returnvalues.OK)
302+
303+
application_result = migwsgi._application(
304+
*self.application_args,
305+
_wrap_wsgi_errors=noop,
306+
_config_file=_TEST_CONF_FILE,
307+
_skip_log=True,
308+
**self.application_kwargs
309+
)
310+
311+
output = _trigger_and_unpack_result(application_result)
312+
313+
self.assertInstrumentation()
314+
self.assertHtmlElementTextContent(output, 'title', 'TEST', trim_newlines=True)
315+
277316

278317
if __name__ == '__main__':
279318
testmain()

0 commit comments

Comments
 (0)