Skip to content

Commit

Permalink
pythonGH-105973 Update test to test multiple header whitespace permut…
Browse files Browse the repository at this point in the history
…ations

Although python#105973 is promarily concerned with trailing whitespace not
being stripped, it's easy/inexpensive to test for other whitespace
behaviour (to detect any other future incompatabilities).

This test change ensures that

- Leading whitespace is trimmed from header values
- Whitespace in the middle of a header value is not removed
  • Loading branch information
bentasker committed Jun 22, 2023
1 parent 598f791 commit 8e65cf2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Lib/test/test_httplib.py
Expand Up @@ -369,13 +369,20 @@ def test_headers_debuglevel(self):
self.assertEqual(lines[3], "header: Second: val2")

def test_header_whitespace_removal(self):
request = b"Host: example.com\r\nContent-Length: 12 \r\n\r\n"
request = b"Host: example.com\r\nContent-Length: 12 \r\nuser-agent: web browser \r\n"
fh = io.BytesIO(request)
parsed = client.parse_headers(fh)

# Check whether the trailing whitespace has been preserved
self.assertTrue(parsed.get('content-length') == "12",
'Header parsing did not strip trailing whitespace from value')
'Header parsing did not strip trailing whitespace from header value')
# And whether leading has been preserved
self.assertTrue(parsed.get('host') == "example.com",
'Header parsing did not strip leading whitespace from header value')
# whitespace within header values should be preserved
self.assertTrue(parsed.get('user-agent') == "web browser",
'Header parsing stripped whitespace from middle of header value')


class HttpMethodTests(TestCase):
def test_invalid_method_names(self):
Expand Down

0 comments on commit 8e65cf2

Please sign in to comment.