| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import socket |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from urllib3.connection import _get_default_user_agent |
| 9 |
from urllib3.exceptions import ConnectionError |
| 10 |
from urllib3.http2.connection import ( |
| 11 |
HTTP2Connection, |
| 12 |
_is_illegal_header_value, |
| 13 |
_is_legal_header_name, |
| 14 |
) |
| 15 |
|
| 16 |
# [1] https://httpwg.org/specs/rfc9113.html#n-field-validity |
| 17 |
|
| 18 |
|
| 19 |
class TestHTTP2Connection: |
| 20 |
def test__is_legal_header_name(self) -> None: |
| 21 |
assert _is_legal_header_name(b"foo"), "foo" |
| 22 |
assert _is_legal_header_name(b"foo-bar"), "foo-bar" |
| 23 |
assert _is_legal_header_name(b"foo-bar-baz"), "foo-bar-baz" |
| 24 |
|
| 25 |
# A field name MUST NOT contain characters in the ranges 0x00-0x20, |
| 26 |
# 0x41-0x5a, or 0x7f-0xff (all ranges inclusive). [1] |
| 27 |
for i in range(0x00, 0x20): |
| 28 |
assert not _is_legal_header_name( |
| 29 |
f"foo{chr(i)}bar".encode() |
| 30 |
), f"foo\\x{i}bar" |
| 31 |
for i in range(0x41, 0x5A): |
| 32 |
assert not _is_legal_header_name( |
| 33 |
f"foo{chr(i)}bar".encode() |
| 34 |
), f"foo\\x{i}bar" |
| 35 |
for i in range(0x7F, 0xFF): |
| 36 |
assert not _is_legal_header_name( |
| 37 |
f"foo{chr(i)}bar".encode() |
| 38 |
), f"foo\\x{i}bar" |
| 39 |
|
| 40 |
# This specifically excludes all non-visible ASCII characters, ASCII SP |
| 41 |
# (0x20), and uppercase characters ('A' to 'Z', ASCII 0x41 to 0x5a). [1] |
| 42 |
assert not _is_legal_header_name(b"foo bar"), "foo bar" |
| 43 |
assert not _is_legal_header_name(b"foo\x20bar"), "foo\\x20bar" |
| 44 |
assert not _is_legal_header_name(b"Foo-Bar"), "Foo-Bar" |
| 45 |
|
| 46 |
# With the exception of pseudo-header fields (Section 8.3), which have a |
| 47 |
# name that starts with a single colon, field names MUST NOT include a |
| 48 |
# colon (ASCII COLON, 0x3a). [1] |
| 49 |
assert not _is_legal_header_name(b":foo"), ":foo" |
| 50 |
assert not _is_legal_header_name(b"foo:bar"), "foo:bar" |
| 51 |
assert not _is_legal_header_name(b"foo:"), "foo:" |
| 52 |
|
| 53 |
def test__is_illegal_header_value(self) -> None: |
| 54 |
assert not _is_illegal_header_value(b"foo"), "foo" |
| 55 |
assert not _is_illegal_header_value(b"foo bar"), "foo bar" |
| 56 |
assert not _is_illegal_header_value(b"foo\tbar"), "foo\\tbar" |
| 57 |
|
| 58 |
# A field value MUST NOT contain the zero value (ASCII NUL, 0x00), line |
| 59 |
# feed (ASCII LF, 0x0a), or carriage return (ASCII CR, 0x0d) at any |
| 60 |
# position. [1] |
| 61 |
assert _is_illegal_header_value(b"foo\0bar"), "foo\\0bar" |
| 62 |
assert _is_illegal_header_value(b"foo\x00bar"), "foo\\x00bar" |
| 63 |
assert _is_illegal_header_value(b"foo\x0abar"), "foo\\x0abar" |
| 64 |
assert _is_illegal_header_value(b"foo\x0dbar"), "foo\\x0dbar" |
| 65 |
assert _is_illegal_header_value(b"foo\rbar"), "foo\\rbar" |
| 66 |
assert _is_illegal_header_value(b"foo\nbar"), "foo\\nbar" |
| 67 |
assert _is_illegal_header_value(b"foo\r\nbar"), "foo\\r\\nbar" |
| 68 |
|
| 69 |
# A field value MUST NOT start or end with an ASCII whitespace character |
| 70 |
# (ASCII SP or HTAB, 0x20 or 0x09). [1] |
| 71 |
assert _is_illegal_header_value(b" foo"), " foo" |
| 72 |
assert _is_illegal_header_value(b"foo "), "foo " |
| 73 |
assert _is_illegal_header_value(b"foo\x20"), "foo\\x20" |
| 74 |
assert _is_illegal_header_value(b"\tfoo"), "\\tfoo" |
| 75 |
assert _is_illegal_header_value(b"foo\t"), "foo\\t" |
| 76 |
assert _is_illegal_header_value(b"foo\x09"), "foo\\x09" |
| 77 |
|
| 78 |
def test_default_socket_options(self) -> None: |
| 79 |
conn = HTTP2Connection("example.com") |
| 80 |
assert conn.socket_options == [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)] |
| 81 |
assert conn.port == 443 |
| 82 |
|
| 83 |
def test_putheader(self) -> None: |
| 84 |
conn = HTTP2Connection("example.com") |
| 85 |
conn.putheader("foo", "bar") |
| 86 |
assert conn._headers == [(b"foo", b"bar")] |
| 87 |
|
| 88 |
def test_request_putheader(self) -> None: |
| 89 |
conn = HTTP2Connection("example.com") |
| 90 |
conn.sock = mock.MagicMock( |
| 91 |
sendall=mock.Mock(return_value=None), |
| 92 |
) |
| 93 |
conn.putheader = mock.MagicMock(return_value=None) # type: ignore[method-assign] |
| 94 |
conn.request("GET", "/", headers={"foo": "bar"}) |
| 95 |
conn.putheader.assert_has_calls( |
| 96 |
[ |
| 97 |
mock.call("foo", "bar"), |
| 98 |
mock.call(b"user-agent", _get_default_user_agent()), |
| 99 |
] |
| 100 |
) |
| 101 |
|
| 102 |
def test_putheader_ValueError(self) -> None: |
| 103 |
conn = HTTP2Connection("example.com") |
| 104 |
with pytest.raises(ValueError): |
| 105 |
conn.putheader("foo\0bar", "baz") |
| 106 |
with pytest.raises(ValueError): |
| 107 |
conn.putheader("foo", "foo\r\nbar") |
| 108 |
|
| 109 |
def test_endheaders_ConnectionError(self) -> None: |
| 110 |
conn = HTTP2Connection("example.com") |
| 111 |
with pytest.raises(ConnectionError): |
| 112 |
conn.endheaders() |
| 113 |
|
| 114 |
def test_send_ConnectionError(self) -> None: |
| 115 |
conn = HTTP2Connection("example.com") |
| 116 |
with pytest.raises(ConnectionError): |
| 117 |
conn.send(b"foo") |
| 118 |
|
| 119 |
def test_send_bytes(self) -> None: |
| 120 |
conn = HTTP2Connection("example.com") |
| 121 |
conn.sock = mock.MagicMock( |
| 122 |
sendall=mock.Mock(return_value=None), |
| 123 |
) |
| 124 |
conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"bar") # type: ignore[method-assign] |
| 125 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 126 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 127 |
|
| 128 |
conn.putrequest("GET", "/") |
| 129 |
conn.endheaders() |
| 130 |
conn.send(b"foo") |
| 131 |
|
| 132 |
conn._h2_conn._obj.data_to_send.assert_called_with() |
| 133 |
conn.sock.sendall.assert_called_with(b"bar") |
| 134 |
conn._h2_conn._obj.send_data.assert_called_with(1, b"foo", end_stream=True) |
| 135 |
|
| 136 |
def test_send_str(self) -> None: |
| 137 |
conn = HTTP2Connection("example.com") |
| 138 |
conn.sock = mock.MagicMock( |
| 139 |
sendall=mock.Mock(return_value=None), |
| 140 |
) |
| 141 |
conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"bar") # type: ignore[method-assign] |
| 142 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 143 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 144 |
|
| 145 |
conn.putrequest("GET", "/") |
| 146 |
conn.endheaders(message_body=b"foo") |
| 147 |
conn.send("foo") |
| 148 |
|
| 149 |
conn._h2_conn._obj.data_to_send.assert_called_with() |
| 150 |
conn.sock.sendall.assert_called_with(b"bar") |
| 151 |
conn._h2_conn._obj.send_data.assert_called_with(1, b"foo", end_stream=True) |
| 152 |
|
| 153 |
def test_send_iter(self) -> None: |
| 154 |
conn = HTTP2Connection("example.com") |
| 155 |
conn.sock = mock.MagicMock( |
| 156 |
sendall=mock.Mock(return_value=None), |
| 157 |
) |
| 158 |
conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"baz") # type: ignore[method-assign] |
| 159 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 160 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 161 |
conn._h2_conn._obj.end_stream = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 162 |
|
| 163 |
conn.putrequest("GET", "/") |
| 164 |
conn.endheaders(message_body=[b"foo", b"bar"]) |
| 165 |
conn.send([b"foo", b"bar"]) |
| 166 |
|
| 167 |
conn._h2_conn._obj.data_to_send.assert_has_calls( |
| 168 |
[ |
| 169 |
mock.call(), |
| 170 |
mock.call(), |
| 171 |
] |
| 172 |
) |
| 173 |
conn.sock.sendall.assert_has_calls( |
| 174 |
[ |
| 175 |
mock.call(b"baz"), |
| 176 |
mock.call(b"baz"), |
| 177 |
] |
| 178 |
) |
| 179 |
conn._h2_conn._obj.send_data.assert_has_calls( |
| 180 |
[ |
| 181 |
mock.call(1, b"foo", end_stream=False), |
| 182 |
mock.call(1, b"bar", end_stream=False), |
| 183 |
] |
| 184 |
) |
| 185 |
conn._h2_conn._obj.end_stream.assert_called_with(1) |
| 186 |
|
| 187 |
def test_send_file_str(self) -> None: |
| 188 |
conn = HTTP2Connection("example.com") |
| 189 |
mock_open = mock.mock_open(read_data="foo\r\nbar\r\n") |
| 190 |
with mock.patch("builtins.open", mock_open): |
| 191 |
conn.sock = mock.MagicMock( |
| 192 |
sendall=mock.Mock(return_value=None), |
| 193 |
) |
| 194 |
conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 195 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 196 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 197 |
conn._h2_conn._obj.end_stream = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 198 |
|
| 199 |
with open("foo") as body: |
| 200 |
conn.putrequest("GET", "/") |
| 201 |
conn.endheaders(message_body=body) |
| 202 |
conn.send(body) |
| 203 |
|
| 204 |
conn._h2_conn._obj.data_to_send.assert_called_with() |
| 205 |
conn.sock.sendall.assert_called_with(b"foo") |
| 206 |
conn._h2_conn._obj.send_data.assert_called_with( |
| 207 |
1, b"foo\r\nbar\r\n", end_stream=False |
| 208 |
) |
| 209 |
conn._h2_conn._obj.end_stream.assert_called_with(1) |
| 210 |
|
| 211 |
def test_send_file_bytes(self) -> None: |
| 212 |
conn = HTTP2Connection("example.com") |
| 213 |
mock_open = mock.mock_open(read_data=b"foo\r\nbar\r\n") |
| 214 |
with mock.patch("builtins.open", mock_open): |
| 215 |
conn.sock = mock.MagicMock( |
| 216 |
sendall=mock.Mock(return_value=None), |
| 217 |
) |
| 218 |
conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 219 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 220 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 221 |
conn._h2_conn._obj.end_stream = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 222 |
|
| 223 |
body = open("foo", "rb") |
| 224 |
conn.putrequest("GET", "/") |
| 225 |
conn.endheaders(message_body=body) |
| 226 |
conn.send(body) |
| 227 |
|
| 228 |
conn._h2_conn._obj.data_to_send.assert_called_with() |
| 229 |
conn.sock.sendall.assert_called_with(b"foo") |
| 230 |
conn._h2_conn._obj.send_data.assert_called_with( |
| 231 |
1, b"foo\r\nbar\r\n", end_stream=False |
| 232 |
) |
| 233 |
conn._h2_conn._obj.end_stream.assert_called_with(1) |
| 234 |
|
| 235 |
def test_send_invalid_type(self) -> None: |
| 236 |
conn = HTTP2Connection("example.com") |
| 237 |
conn.putrequest("GET", "/") |
| 238 |
with pytest.raises(TypeError): |
| 239 |
conn.send(1) |
| 240 |
|
| 241 |
def test_request_GET(self) -> None: |
| 242 |
conn = HTTP2Connection("example.com") |
| 243 |
conn.sock = mock.MagicMock( |
| 244 |
sendall=mock.Mock(return_value=None), |
| 245 |
) |
| 246 |
sendall = conn.sock.sendall |
| 247 |
data_to_send = conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 248 |
send_headers = conn._h2_conn._obj.send_headers = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 249 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 250 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 251 |
close_connection = conn._h2_conn._obj.close_connection = mock.Mock( # type: ignore[method-assign] |
| 252 |
return_value=None |
| 253 |
) |
| 254 |
|
| 255 |
conn.request("GET", "/") |
| 256 |
conn.close() |
| 257 |
|
| 258 |
data_to_send.assert_called_with() |
| 259 |
sendall.assert_called_with(b"foo") |
| 260 |
send_headers.assert_called_with( |
| 261 |
stream_id=1, |
| 262 |
headers=[ |
| 263 |
(b":scheme", b"https"), |
| 264 |
(b":method", b"GET"), |
| 265 |
(b":authority", b"example.com:443"), |
| 266 |
(b":path", b"/"), |
| 267 |
(b"user-agent", _get_default_user_agent().encode()), |
| 268 |
], |
| 269 |
end_stream=True, |
| 270 |
) |
| 271 |
|
| 272 |
close_connection.assert_called_with() |
| 273 |
|
| 274 |
def test_request_POST(self) -> None: |
| 275 |
conn = HTTP2Connection("example.com") |
| 276 |
conn.sock = mock.MagicMock( |
| 277 |
sendall=mock.Mock(return_value=None), |
| 278 |
) |
| 279 |
sendall = conn.sock.sendall |
| 280 |
data_to_send = conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 281 |
send_headers = conn._h2_conn._obj.send_headers = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 282 |
send_data = conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 283 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 284 |
close_connection = conn._h2_conn._obj.close_connection = mock.Mock( # type: ignore[method-assign] |
| 285 |
return_value=None |
| 286 |
) |
| 287 |
|
| 288 |
conn.request("POST", "/", body=b"foo") |
| 289 |
conn.close() |
| 290 |
|
| 291 |
data_to_send.assert_called_with() |
| 292 |
sendall.assert_called_with(b"foo") |
| 293 |
send_headers.assert_called_with( |
| 294 |
stream_id=1, |
| 295 |
headers=[ |
| 296 |
(b":scheme", b"https"), |
| 297 |
(b":method", b"POST"), |
| 298 |
(b":authority", b"example.com:443"), |
| 299 |
(b":path", b"/"), |
| 300 |
(b"user-agent", _get_default_user_agent().encode()), |
| 301 |
], |
| 302 |
end_stream=False, |
| 303 |
) |
| 304 |
send_data.assert_called_with(1, b"foo", end_stream=True) |
| 305 |
close_connection.assert_called_with() |
| 306 |
|
| 307 |
def test_close(self) -> None: |
| 308 |
conn = HTTP2Connection("example.com") |
| 309 |
conn.sock = mock.MagicMock( |
| 310 |
sendall=mock.Mock(side_effect=Exception("foo")), |
| 311 |
) |
| 312 |
sendall = conn.sock.sendall |
| 313 |
data_to_send = conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 314 |
close_connection = conn._h2_conn._obj.close_connection = mock.Mock( # type: ignore[method-assign] |
| 315 |
return_value=None |
| 316 |
) |
| 317 |
|
| 318 |
try: |
| 319 |
conn.close() |
| 320 |
except Exception: |
| 321 |
assert False, "Exception was raised" |
| 322 |
|
| 323 |
close_connection.assert_called_with() |
| 324 |
data_to_send.assert_called_with() |
| 325 |
sendall.assert_called_with(b"foo") |
| 326 |
assert conn._h2_stream is None |
| 327 |
assert conn._headers == [] |
| 328 |
|
| 329 |
def test_request_ignore_chunked(self) -> None: |
| 330 |
conn = HTTP2Connection("example.com") |
| 331 |
conn.sock = mock.MagicMock( |
| 332 |
sendall=mock.Mock(return_value=None), |
| 333 |
) |
| 334 |
sendall = conn.sock.sendall |
| 335 |
data_to_send = conn._h2_conn._obj.data_to_send = mock.Mock(return_value=b"foo") # type: ignore[method-assign] |
| 336 |
send_headers = conn._h2_conn._obj.send_headers = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 337 |
conn._h2_conn._obj.send_data = mock.Mock(return_value=None) # type: ignore[method-assign] |
| 338 |
conn._h2_conn._obj.get_next_available_stream_id = mock.Mock(return_value=1) # type: ignore[method-assign] |
| 339 |
close_connection = conn._h2_conn._obj.close_connection = mock.Mock( # type: ignore[method-assign] |
| 340 |
return_value=None |
| 341 |
) |
| 342 |
|
| 343 |
conn.request("GET", "/", headers={"Transfer-Encoding": "chunked"}, chunked=True) |
| 344 |
conn.close() |
| 345 |
|
| 346 |
data_to_send.assert_called_with() |
| 347 |
sendall.assert_called_with(b"foo") |
| 348 |
send_headers.assert_called_with( |
| 349 |
stream_id=1, |
| 350 |
headers=[ |
| 351 |
(b":scheme", b"https"), |
| 352 |
(b":method", b"GET"), |
| 353 |
(b":authority", b"example.com:443"), |
| 354 |
(b":path", b"/"), |
| 355 |
(b"user-agent", _get_default_user_agent().encode()), |
| 356 |
], |
| 357 |
end_stream=True, |
| 358 |
) |
| 359 |
|
| 360 |
close_connection.assert_called_with() |