Server IP : 85.214.239.14 / Your IP : 18.222.67.8 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/self/root/proc/2/root/proc/3/cwd/lib/python3/dist-packages/h11/tests/ |
Upload File : |
import pytest from .._events import Request from .._headers import ( get_comma_header, has_expect_100_continue, Headers, normalize_and_validate, set_comma_header, ) from .._util import LocalProtocolError def test_normalize_and_validate() -> None: assert normalize_and_validate([("foo", "bar")]) == [(b"foo", b"bar")] assert normalize_and_validate([(b"foo", b"bar")]) == [(b"foo", b"bar")] # no leading/trailing whitespace in names with pytest.raises(LocalProtocolError): normalize_and_validate([(b"foo ", "bar")]) with pytest.raises(LocalProtocolError): normalize_and_validate([(b" foo", "bar")]) # no weird characters in names with pytest.raises(LocalProtocolError) as excinfo: normalize_and_validate([(b"foo bar", b"baz")]) assert "foo bar" in str(excinfo.value) with pytest.raises(LocalProtocolError): normalize_and_validate([(b"foo\x00bar", b"baz")]) # Not even 8-bit characters: with pytest.raises(LocalProtocolError): normalize_and_validate([(b"foo\xffbar", b"baz")]) # And not even the control characters we allow in values: with pytest.raises(LocalProtocolError): normalize_and_validate([(b"foo\x01bar", b"baz")]) # no return or NUL characters in values with pytest.raises(LocalProtocolError) as excinfo: normalize_and_validate([("foo", "bar\rbaz")]) assert "bar\\rbaz" in str(excinfo.value) with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", "bar\nbaz")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", "bar\x00baz")]) # no leading/trailing whitespace with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", "barbaz ")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", " barbaz")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", "barbaz\t")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("foo", "\tbarbaz")]) # content-length assert normalize_and_validate([("Content-Length", "1")]) == [ (b"content-length", b"1") ] with pytest.raises(LocalProtocolError): normalize_and_validate([("Content-Length", "asdf")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("Content-Length", "1x")]) with pytest.raises(LocalProtocolError): normalize_and_validate([("Content-Length", "1"), ("Content-Length", "2")]) assert normalize_and_validate( [("Content-Length", "0"), ("Content-Length", "0")] ) == [(b"content-length", b"0")] assert normalize_and_validate([("Content-Length", "0 , 0")]) == [ (b"content-length", b"0") ] with pytest.raises(LocalProtocolError): normalize_and_validate( [("Content-Length", "1"), ("Content-Length", "1"), ("Content-Length", "2")] ) with pytest.raises(LocalProtocolError): normalize_and_validate([("Content-Length", "1 , 1,2")]) # transfer-encoding assert normalize_and_validate([("Transfer-Encoding", "chunked")]) == [ (b"transfer-encoding", b"chunked") ] assert normalize_and_validate([("Transfer-Encoding", "cHuNkEd")]) == [ (b"transfer-encoding", b"chunked") ] with pytest.raises(LocalProtocolError) as excinfo: normalize_and_validate([("Transfer-Encoding", "gzip")]) assert excinfo.value.error_status_hint == 501 # Not Implemented with pytest.raises(LocalProtocolError) as excinfo: normalize_and_validate( [("Transfer-Encoding", "chunked"), ("Transfer-Encoding", "gzip")] ) assert excinfo.value.error_status_hint == 501 # Not Implemented def test_get_set_comma_header() -> None: headers = normalize_and_validate( [ ("Connection", "close"), ("whatever", "something"), ("connectiON", "fOo,, , BAR"), ] ) assert get_comma_header(headers, b"connection") == [b"close", b"foo", b"bar"] headers = set_comma_header(headers, b"newthing", ["a", "b"]) # type: ignore with pytest.raises(LocalProtocolError): set_comma_header(headers, b"newthing", [" a", "b"]) # type: ignore assert headers == [ (b"connection", b"close"), (b"whatever", b"something"), (b"connection", b"fOo,, , BAR"), (b"newthing", b"a"), (b"newthing", b"b"), ] headers = set_comma_header(headers, b"whatever", ["different thing"]) # type: ignore assert headers == [ (b"connection", b"close"), (b"connection", b"fOo,, , BAR"), (b"newthing", b"a"), (b"newthing", b"b"), (b"whatever", b"different thing"), ] def test_has_100_continue() -> None: assert has_expect_100_continue( Request( method="GET", target="/", headers=[("Host", "example.com"), ("Expect", "100-continue")], ) ) assert not has_expect_100_continue( Request(method="GET", target="/", headers=[("Host", "example.com")]) ) # Case insensitive assert has_expect_100_continue( Request( method="GET", target="/", headers=[("Host", "example.com"), ("Expect", "100-Continue")], ) ) # Doesn't work in HTTP/1.0 assert not has_expect_100_continue( Request( method="GET", target="/", headers=[("Host", "example.com"), ("Expect", "100-continue")], http_version="1.0", ) )