Skip to content

Regression test for valid HTML being generated in the error path. #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions tests/test_mig_wsgi-bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,58 @@
from html.parser import HTMLParser


class TitleExtractingHtmlParser(HTMLParser):
"""An HTML parser using builtin machinery which will extract the title."""
class DocumentBasicsHtmlParser(HTMLParser):
"""An HTML parser using builtin machinery to check basic html structure."""

def __init__(self):
HTMLParser.__init__(self)
self._doctype = "none"
self._saw_doctype = False
self._saw_tags = False
self._tag_html = "none"

def handle_decl(self, decl):
try:
decltag, decltype = decl.split(' ')
except Exception:
decltag = ""
decltype = ""

if decltag.upper() == 'DOCTYPE':
self._saw_doctype = True
else:
decltype = "unknown"

self._doctype = decltype

def handle_starttag(self, tag, attrs):
if tag == 'html':
if self._saw_tags:
tag_html = 'not_first'
else:
tag_html = 'was_first'
self._tag_html = tag_html
self._saw_tags = True

def assert_basics(self):
if not self._saw_doctype:
raise AssertionError("missing DOCTYPE")

if self._doctype != 'html':
raise AssertionError("non-html DOCTYPE")

if self._tag_html == 'none':
raise AssertionError("missing <html>")

if self._tag_html != 'was_first':
raise AssertionError("first tag seen was not <html>")


class TitleExtractingHtmlParser(DocumentBasicsHtmlParser):
"""An HTML parser using builtin machinery which will extract the title."""

def __init__(self):
DocumentBasicsHtmlParser.__init__(self)
self._title = None
self._within_title = None

Expand All @@ -61,14 +108,20 @@ def handle_data(self, *args, **kwargs):
self._title = args[0]

def handle_starttag(self, tag, attrs):
super().handle_starttag(tag, attrs)

if tag == 'title':
self._within_title = True

def handle_endtag(self, tag):
super().handle_endtag(tag)

if tag == 'title':
self._within_title = False

def title(self, trim_newlines=False):
self.assert_basics()

if self._title and not self._within_title:
if trim_newlines:
return self._title.strip()
Expand Down Expand Up @@ -173,5 +226,46 @@ def test_return_value_ok_returns_expected_title(self):
self.assertHtmlTitle(output, title_text='TEST', trim_newlines=True)


class MigWsgibin_output_objects(MigTestCase, WsgiAssertMixin):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see you even left room for the mandatory class docstring here ... ;-)
Apart from that pedantic bit the PR looks unintrusive enough for me to proceed with the merge as-is and leave adjustments to follow-up. Perhaps one or more methods/functions would also gain clarity from added docs.

def _provide_configuration(self):
return 'testconfig'

def before_each(self):
self.fake_backend = FakeBackend()
self.fake_wsgi = prepare_wsgi(self.configuration, 'http://localhost/')

self.application_args = (
self.fake_wsgi.environ,
self.fake_wsgi.start_response,
)
self.application_kwargs = dict(
configuration=self.configuration,
_import_module=self.fake_backend.to_import_module(),
_set_os_environ=False,
)

def assertIsValidHtmlDocument(self, value):
parser = DocumentBasicsHtmlParser()
parser.feed(value)
parser.assert_basics()

def test_unknown_object_type_generates_valid_error_page(self):
output_objects = [
{
'object_type': 'nonexistent', # trigger error handling path
}
]
self.fake_backend.set_response(output_objects, returnvalues.OK)

wsgi_result = migwsgi.application(
*self.application_args,
**self.application_kwargs
)

output, _ = self.assertWsgiResponse(wsgi_result, self.fake_wsgi, 200)
self.assertIsValidHtmlDocument(output)


if __name__ == '__main__':
testmain()