Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: update tests to be more consistent with http.client tests
  • Loading branch information
epicfaace committed Aug 15, 2019
commit 6cd271ae2f0ff5c4962bd0808eda2d8bf4464408
39 changes: 30 additions & 9 deletions Lib/test/test_wsgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,37 @@ def testExtras(self):
'\r\n'
)

def testValidateHeaders(self):
validation_cases = (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a comment mentioning bpo-11671.

Rather than an hardcoded list, why not building such list using a few invalid characters. For example for header names:

invalid_names = []
invalid_name_chars = ['\r', '\n', '\r\n', ':']
for invalid_chars in invalid_name_chars:
  invalid_names.append(f'{invalid_chars}InvalidName')
  invalid_names.append(f'Invalid{invalid_chars}Name')
  invalid_names.append(f'InvalidName{invalid_chars}')
invalid_names.append(' InvalidName')

Are whitespaces (" ") and colon (":") only invalid at the beginning of the name?

('Invalid\r\nName', 'ValidValue'),
('Invalid\rName', 'ValidValue'),
('Invalid\nName', 'ValidValue'),
('\r\nInvalidName', 'ValidValue'),
('\rInvalidName', 'ValidValue'),
('\nInvalidName', 'ValidValue'),
(' InvalidName', 'ValidValue'),
('\tInvalidName', 'ValidValue'),
('Invalid:Name', 'ValidValue'),
(':InvalidName', 'ValidValue'),
('ValidName', 'Invalid\r\nValue'),
('ValidName', 'Invalid\rValue'),
('ValidName', 'Invalid\nValue'),
('ValidName', 'InvalidValue\r\n'),
('ValidName', 'InvalidValue\r'),
('ValidName', 'InvalidValue\n')
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are there already tests for header names which cannot be encoded to ASCII? And tests for values not encodable to Latin1?


def test_add_header_validation(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest to merge the two tests a single test, to factorize code.

I suggest to remove the b'InvalidName' and b'InvalidValue' from validation_cases. Maybe create a second list, maybe put it directly in the test method to avoid with self.assertRaises(exception[0]) if len(exception) else self.assertRaisesRegex(ValueError, 'Invalid header'): complexity.

h = Headers([])
self.assertRaises(ValueError, h.add_header, 'foo', 'bar\rbaz: bat')
self.assertRaises(ValueError, h.add_header, 'foo', 'bar\nbaz: bat')
self.assertRaises(ValueError, h.add_header, 'foo: bar', 'baz')
self.assertRaises(ValueError, h.add_header, 'foo:\rbar', 'baz')
self.assertRaises(ValueError, h.add_header, 'foo:\nbar', 'baz')
self.assertRaises(ValueError, h.add_header, 'foo', 'bar\nbaz: bat')
self.assertRaises(ValueError, h.add_header, 'foo', 'bar', baz='bat\rqux: spam')
self.assertRaises(ValueError, h.add_header, 'foo', 'bar', baz='bat\nqux: spam')
for name, value in self.validation_cases:
with self.subTest((name, value)):
with self.assertRaisesRegex(ValueError, 'Invalid header'):
h.add_header(name, value)

def test_initialize_validation(self):
Comment thread
epicfaace marked this conversation as resolved.
for name, value in self.validation_cases:
with self.subTest((name, value)):
with self.assertRaisesRegex(ValueError, 'Invalid header'):
Headers([(name, value)])

class ErrorHandler(BaseCGIHandler):
"""Simple handler subclass for testing BaseHandler"""
Expand Down