|
1 | 1 | # Tests for aiohttp/protocol.py
|
2 | 2 |
|
3 | 3 | import asyncio
|
| 4 | +import re |
4 | 5 | from typing import Any, List
|
5 | 6 | from unittest import mock
|
6 | 7 | from urllib.parse import quote
|
@@ -118,6 +119,26 @@ def test_parse_headers(parser: Any) -> None:
|
118 | 119 | assert not msg.upgrade
|
119 | 120 |
|
120 | 121 |
|
| 122 | +@pytest.mark.skipif(NO_EXTENSIONS, reason="Only tests C parser.") |
| 123 | +def test_invalid_character(loop: Any, protocol: Any, request: Any) -> None: |
| 124 | + parser = HttpRequestParserC( |
| 125 | + protocol, |
| 126 | + loop, |
| 127 | + 2**16, |
| 128 | + max_line_size=8190, |
| 129 | + max_field_size=8190, |
| 130 | + ) |
| 131 | + text = b"POST / HTTP/1.1\r\nHost: localhost:8080\r\nSet-Cookie: abc\x01def\r\n\r\n" |
| 132 | + error_detail = re.escape( |
| 133 | + r""": |
| 134 | +
|
| 135 | + b'Set-Cookie: abc\x01def\r' |
| 136 | + ^""" |
| 137 | + ) |
| 138 | + with pytest.raises(http_exceptions.BadHttpMessage, match=error_detail): |
| 139 | + parser.feed_data(text) |
| 140 | + |
| 141 | + |
121 | 142 | def test_parse(parser) -> None:
|
122 | 143 | text = b"GET /test HTTP/1.1\r\n\r\n"
|
123 | 144 | messages, upgrade, tail = parser.feed_data(text)
|
@@ -429,7 +450,7 @@ def test_max_header_field_size(parser, size) -> None:
|
429 | 450 | name = b"t" * size
|
430 | 451 | text = b"GET /test HTTP/1.1\r\n" + name + b":data\r\n\r\n"
|
431 | 452 |
|
432 |
| - match = f"400, message='Got more than 8190 bytes \\({size}\\) when reading" |
| 453 | + match = f"400, message:\n Got more than 8190 bytes \\({size}\\) when reading" |
433 | 454 | with pytest.raises(http_exceptions.LineTooLong, match=match):
|
434 | 455 | parser.feed_data(text)
|
435 | 456 |
|
@@ -457,7 +478,7 @@ def test_max_header_value_size(parser, size) -> None:
|
457 | 478 | name = b"t" * size
|
458 | 479 | text = b"GET /test HTTP/1.1\r\n" b"data:" + name + b"\r\n\r\n"
|
459 | 480 |
|
460 |
| - match = f"400, message='Got more than 8190 bytes \\({size}\\) when reading" |
| 481 | + match = f"400, message:\n Got more than 8190 bytes \\({size}\\) when reading" |
461 | 482 | with pytest.raises(http_exceptions.LineTooLong, match=match):
|
462 | 483 | parser.feed_data(text)
|
463 | 484 |
|
@@ -485,7 +506,7 @@ def test_max_header_value_size_continuation(parser, size) -> None:
|
485 | 506 | name = b"T" * (size - 5)
|
486 | 507 | text = b"GET /test HTTP/1.1\r\n" b"data: test\r\n " + name + b"\r\n\r\n"
|
487 | 508 |
|
488 |
| - match = f"400, message='Got more than 8190 bytes \\({size}\\) when reading" |
| 509 | + match = f"400, message:\n Got more than 8190 bytes \\({size}\\) when reading" |
489 | 510 | with pytest.raises(http_exceptions.LineTooLong, match=match):
|
490 | 511 | parser.feed_data(text)
|
491 | 512 |
|
@@ -608,7 +629,7 @@ def test_http_request_parser_bad_version(parser) -> None:
|
608 | 629 | @pytest.mark.parametrize("size", [40965, 8191])
|
609 | 630 | def test_http_request_max_status_line(parser, size) -> None:
|
610 | 631 | path = b"t" * (size - 5)
|
611 |
| - match = f"400, message='Got more than 8190 bytes \\({size}\\) when reading" |
| 632 | + match = f"400, message:\n Got more than 8190 bytes \\({size}\\) when reading" |
612 | 633 | with pytest.raises(http_exceptions.LineTooLong, match=match):
|
613 | 634 | parser.feed_data(b"GET /path" + path + b" HTTP/1.1\r\n\r\n")
|
614 | 635 |
|
@@ -651,7 +672,7 @@ def test_http_response_parser_utf8(response) -> None:
|
651 | 672 | @pytest.mark.parametrize("size", [40962, 8191])
|
652 | 673 | def test_http_response_parser_bad_status_line_too_long(response, size) -> None:
|
653 | 674 | reason = b"t" * (size - 2)
|
654 |
| - match = f"400, message='Got more than 8190 bytes \\({size}\\) when reading" |
| 675 | + match = f"400, message:\n Got more than 8190 bytes \\({size}\\) when reading" |
655 | 676 | with pytest.raises(http_exceptions.LineTooLong, match=match):
|
656 | 677 | response.feed_data(b"HTTP/1.1 200 Ok" + reason + b"\r\n\r\n")
|
657 | 678 |
|
|
0 commit comments