| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from urllib3._collections import HTTPHeaderDict |
| 8 |
from urllib3._collections import RecentlyUsedContainer as Container |
| 9 |
|
| 10 |
|
| 11 |
class TestLRUContainer: |
| 12 |
def test_maxsize(self) -> None: |
| 13 |
d: Container[int, str] = Container(5) |
| 14 |
|
| 15 |
for i in range(5): |
| 16 |
d[i] = str(i) |
| 17 |
|
| 18 |
assert len(d) == 5 |
| 19 |
|
| 20 |
for i in range(5): |
| 21 |
assert d[i] == str(i) |
| 22 |
|
| 23 |
d[i + 1] = str(i + 1) |
| 24 |
|
| 25 |
assert len(d) == 5 |
| 26 |
assert 0 not in d |
| 27 |
assert (i + 1) in d |
| 28 |
|
| 29 |
def test_maxsize_0(self) -> None: |
| 30 |
d: Container[int, int] = Container(0) |
| 31 |
d[1] = 1 |
| 32 |
assert len(d) == 0 |
| 33 |
|
| 34 |
def test_expire(self) -> None: |
| 35 |
d: Container[int, str] = Container(5) |
| 36 |
|
| 37 |
for i in range(5): |
| 38 |
d[i] = str(i) |
| 39 |
|
| 40 |
for i in range(5): |
| 41 |
d.get(0) |
| 42 |
|
| 43 |
# Add one more entry |
| 44 |
d[5] = "5" |
| 45 |
|
| 46 |
# Check state |
| 47 |
assert list(d._container.keys()) == [2, 3, 4, 0, 5] |
| 48 |
|
| 49 |
def test_same_key(self) -> None: |
| 50 |
d: Container[str, int] = Container(5) |
| 51 |
|
| 52 |
for i in range(10): |
| 53 |
d["foo"] = i |
| 54 |
|
| 55 |
assert list(d._container.keys()) == ["foo"] |
| 56 |
assert len(d) == 1 |
| 57 |
|
| 58 |
def test_access_ordering(self) -> None: |
| 59 |
d: Container[int, bool] = Container(5) |
| 60 |
|
| 61 |
for i in range(10): |
| 62 |
d[i] = True |
| 63 |
|
| 64 |
# Keys should be ordered by access time |
| 65 |
assert list(d._container.keys()) == [5, 6, 7, 8, 9] |
| 66 |
|
| 67 |
new_order = [7, 8, 6, 9, 5] |
| 68 |
for k in new_order: |
| 69 |
d[k] |
| 70 |
|
| 71 |
assert list(d._container.keys()) == new_order |
| 72 |
|
| 73 |
def test_delete(self) -> None: |
| 74 |
d: Container[int, bool] = Container(5) |
| 75 |
|
| 76 |
for i in range(5): |
| 77 |
d[i] = True |
| 78 |
|
| 79 |
del d[0] |
| 80 |
assert 0 not in d |
| 81 |
|
| 82 |
d.pop(1) |
| 83 |
assert 1 not in d |
| 84 |
|
| 85 |
d.pop(1, None) |
| 86 |
|
| 87 |
def test_get(self) -> None: |
| 88 |
d: Container[int, bool | int] = Container(5) |
| 89 |
|
| 90 |
for i in range(5): |
| 91 |
d[i] = True |
| 92 |
|
| 93 |
r = d.get(4) |
| 94 |
assert r is True |
| 95 |
|
| 96 |
r = d.get(5) |
| 97 |
assert r is None |
| 98 |
|
| 99 |
r = d.get(5, 42) |
| 100 |
assert r == 42 |
| 101 |
|
| 102 |
with pytest.raises(KeyError): |
| 103 |
d[5] |
| 104 |
|
| 105 |
def test_disposal(self) -> None: |
| 106 |
evicted_items: list[int] = [] |
| 107 |
|
| 108 |
def dispose_func(arg: int) -> None: |
| 109 |
# Save the evicted datum for inspection |
| 110 |
evicted_items.append(arg) |
| 111 |
|
| 112 |
d: Container[int, int] = Container(5, dispose_func=dispose_func) |
| 113 |
for i in range(5): |
| 114 |
d[i] = i |
| 115 |
assert list(d._container.keys()) == list(range(5)) |
| 116 |
assert evicted_items == [] # Nothing disposed |
| 117 |
|
| 118 |
d[5] = 5 |
| 119 |
assert list(d._container.keys()) == list(range(1, 6)) |
| 120 |
assert evicted_items == [0] |
| 121 |
|
| 122 |
del d[1] |
| 123 |
assert evicted_items == [0, 1] |
| 124 |
|
| 125 |
d.clear() |
| 126 |
assert evicted_items == [0, 1, 2, 3, 4, 5] |
| 127 |
|
| 128 |
def test_iter(self) -> None: |
| 129 |
d: Container[str, str] = Container() |
| 130 |
|
| 131 |
with pytest.raises(NotImplementedError): |
| 132 |
d.__iter__() |
| 133 |
|
| 134 |
|
| 135 |
class NonMappingHeaderContainer: |
| 136 |
def __init__(self, **kwargs: str) -> None: |
| 137 |
self._data = {} |
| 138 |
self._data.update(kwargs) |
| 139 |
|
| 140 |
def keys(self) -> typing.Iterator[str]: |
| 141 |
return iter(self._data) |
| 142 |
|
| 143 |
def __getitem__(self, key: str) -> str: |
| 144 |
return self._data[key] |
| 145 |
|
| 146 |
|
| 147 |
@pytest.fixture() |
| 148 |
def d() -> HTTPHeaderDict: |
| 149 |
header_dict = HTTPHeaderDict(Cookie="foo") |
| 150 |
header_dict.add("cookie", "bar") |
| 151 |
return header_dict |
| 152 |
|
| 153 |
|
| 154 |
class TestHTTPHeaderDict: |
| 155 |
def test_create_from_kwargs(self) -> None: |
| 156 |
h = HTTPHeaderDict(ab="1", cd="2", ef="3", gh="4") |
| 157 |
assert len(h) == 4 |
| 158 |
assert "ab" in h |
| 159 |
|
| 160 |
def test_setdefault(self) -> None: |
| 161 |
h = HTTPHeaderDict(a="1") |
| 162 |
assert h.setdefault("A", "3") == "1" |
| 163 |
assert h.setdefault("b", "2") == "2" |
| 164 |
assert h.setdefault("c") == "" |
| 165 |
assert h["c"] == "" |
| 166 |
assert h["b"] == "2" |
| 167 |
|
| 168 |
def test_create_from_dict(self) -> None: |
| 169 |
h = HTTPHeaderDict(dict(ab="1", cd="2", ef="3", gh="4")) |
| 170 |
assert len(h) == 4 |
| 171 |
assert "ab" in h |
| 172 |
|
| 173 |
def test_create_from_iterator(self) -> None: |
| 174 |
teststr = "urllib3ontherocks" |
| 175 |
h = HTTPHeaderDict((c, c * 5) for c in teststr) |
| 176 |
assert len(h) == len(set(teststr)) |
| 177 |
|
| 178 |
def test_create_from_list(self) -> None: |
| 179 |
headers = [ |
| 180 |
("ab", "A"), |
| 181 |
("cd", "B"), |
| 182 |
("cookie", "C"), |
| 183 |
("cookie", "D"), |
| 184 |
("cookie", "E"), |
| 185 |
] |
| 186 |
h = HTTPHeaderDict(headers) |
| 187 |
assert len(h) == 3 |
| 188 |
assert "ab" in h |
| 189 |
clist = h.getlist("cookie") |
| 190 |
assert len(clist) == 3 |
| 191 |
assert clist[0] == "C" |
| 192 |
assert clist[-1] == "E" |
| 193 |
|
| 194 |
def test_create_from_headerdict(self) -> None: |
| 195 |
headers = [ |
| 196 |
("ab", "A"), |
| 197 |
("cd", "B"), |
| 198 |
("cookie", "C"), |
| 199 |
("cookie", "D"), |
| 200 |
("cookie", "E"), |
| 201 |
] |
| 202 |
org = HTTPHeaderDict(headers) |
| 203 |
h = HTTPHeaderDict(org) |
| 204 |
assert len(h) == 3 |
| 205 |
assert "ab" in h |
| 206 |
clist = h.getlist("cookie") |
| 207 |
assert len(clist) == 3 |
| 208 |
assert clist[0] == "C" |
| 209 |
assert clist[-1] == "E" |
| 210 |
assert h is not org |
| 211 |
assert h == org |
| 212 |
|
| 213 |
def test_setitem(self, d: HTTPHeaderDict) -> None: |
| 214 |
d["Cookie"] = "foo" |
| 215 |
# The bytes value gets converted to str. The API is typed for str only, |
| 216 |
# but the implementation continues supports bytes. |
| 217 |
d[b"Cookie"] = "bar" # type: ignore[index] |
| 218 |
assert d["cookie"] == "bar" |
| 219 |
d["cookie"] = "with, comma" |
| 220 |
assert d.getlist("cookie") == ["with, comma"] |
| 221 |
|
| 222 |
def test_update(self, d: HTTPHeaderDict) -> None: |
| 223 |
d.update(dict(Cookie="foo")) |
| 224 |
assert d["cookie"] == "foo" |
| 225 |
d.update(dict(cookie="with, comma")) |
| 226 |
assert d.getlist("cookie") == ["with, comma"] |
| 227 |
|
| 228 |
def test_delitem(self, d: HTTPHeaderDict) -> None: |
| 229 |
del d["cookie"] |
| 230 |
assert "cookie" not in d |
| 231 |
assert "COOKIE" not in d |
| 232 |
|
| 233 |
def test_delitem_with_bytes_key(self, d: HTTPHeaderDict) -> None: |
| 234 |
del d[b"cookie"] # type: ignore[arg-type] |
| 235 |
assert "cookie" not in d |
| 236 |
|
| 237 |
def test_add_well_known_multiheader(self, d: HTTPHeaderDict) -> None: |
| 238 |
d.add("COOKIE", "asdf") |
| 239 |
assert d.getlist("cookie") == ["foo", "bar", "asdf"] |
| 240 |
assert d["cookie"] == "foo, bar, asdf" |
| 241 |
|
| 242 |
def test_add_comma_separated_multiheader(self, d: HTTPHeaderDict) -> None: |
| 243 |
d.add("bar", "foo") |
| 244 |
# The bytes value gets converted to str. The API is typed for str only, |
| 245 |
# but the implementation continues supports bytes. |
| 246 |
d.add(b"BAR", "bar") # type: ignore[arg-type] |
| 247 |
d.add("Bar", "asdf") |
| 248 |
assert d.getlist("bar") == ["foo", "bar", "asdf"] |
| 249 |
assert d["bar"] == "foo, bar, asdf" |
| 250 |
|
| 251 |
def test_extend_from_list(self, d: HTTPHeaderDict) -> None: |
| 252 |
d.extend([("set-cookie", "100"), ("set-cookie", "200"), ("set-cookie", "300")]) |
| 253 |
assert d["set-cookie"] == "100, 200, 300" |
| 254 |
|
| 255 |
def test_extend_from_dict(self, d: HTTPHeaderDict) -> None: |
| 256 |
d.extend(dict(cookie="asdf"), b="100") |
| 257 |
assert d["cookie"] == "foo, bar, asdf" |
| 258 |
assert d["b"] == "100" |
| 259 |
d.add("cookie", "with, comma") |
| 260 |
assert d.getlist("cookie") == ["foo", "bar", "asdf", "with, comma"] |
| 261 |
|
| 262 |
def test_extend_from_container(self, d: HTTPHeaderDict) -> None: |
| 263 |
h = NonMappingHeaderContainer(Cookie="foo", e="foofoo") |
| 264 |
d.extend(h) |
| 265 |
assert d["cookie"] == "foo, bar, foo" |
| 266 |
assert d["e"] == "foofoo" |
| 267 |
assert len(d) == 2 |
| 268 |
|
| 269 |
def test_header_repeat(self, d: HTTPHeaderDict) -> None: |
| 270 |
d["other-header"] = "hello" |
| 271 |
d.add("other-header", "world") |
| 272 |
|
| 273 |
assert list(d.items()) == [ |
| 274 |
("Cookie", "foo"), |
| 275 |
("Cookie", "bar"), |
| 276 |
("other-header", "hello"), |
| 277 |
("other-header", "world"), |
| 278 |
] |
| 279 |
|
| 280 |
d.add("other-header", "!", combine=True) |
| 281 |
expected_results = [ |
| 282 |
("Cookie", "foo"), |
| 283 |
("Cookie", "bar"), |
| 284 |
("other-header", "hello"), |
| 285 |
("other-header", "world, !"), |
| 286 |
] |
| 287 |
|
| 288 |
assert list(d.items()) == expected_results |
| 289 |
# make sure the values persist over copies |
| 290 |
assert list(d.copy().items()) == expected_results |
| 291 |
|
| 292 |
other_dict = HTTPHeaderDict() |
| 293 |
# we also need for extensions to properly maintain results |
| 294 |
other_dict.extend(d) |
| 295 |
assert list(other_dict.items()) == expected_results |
| 296 |
|
| 297 |
def test_extend_from_headerdict(self, d: HTTPHeaderDict) -> None: |
| 298 |
h = HTTPHeaderDict(Cookie="foo", e="foofoo") |
| 299 |
d.extend(h) |
| 300 |
assert d["cookie"] == "foo, bar, foo" |
| 301 |
assert d["e"] == "foofoo" |
| 302 |
assert len(d) == 2 |
| 303 |
|
| 304 |
@pytest.mark.parametrize("args", [(1, 2), (1, 2, 3, 4, 5)]) |
| 305 |
def test_extend_with_wrong_number_of_args_is_typeerror( |
| 306 |
self, d: HTTPHeaderDict, args: tuple[int, ...] |
| 307 |
) -> None: |
| 308 |
with pytest.raises( |
| 309 |
TypeError, match=r"extend\(\) takes at most 1 positional arguments" |
| 310 |
): |
| 311 |
d.extend(*args) # type: ignore[arg-type] |
| 312 |
|
| 313 |
def test_copy(self, d: HTTPHeaderDict) -> None: |
| 314 |
h = d.copy() |
| 315 |
assert d is not h |
| 316 |
assert d == h |
| 317 |
|
| 318 |
def test_getlist(self, d: HTTPHeaderDict) -> None: |
| 319 |
assert d.getlist("cookie") == ["foo", "bar"] |
| 320 |
assert d.getlist("Cookie") == ["foo", "bar"] |
| 321 |
assert d.getlist("b") == [] |
| 322 |
d.add("b", "asdf") |
| 323 |
assert d.getlist("b") == ["asdf"] |
| 324 |
|
| 325 |
def test_getlist_with_bytes_key(self, d: HTTPHeaderDict) -> None: |
| 326 |
assert d.getlist(b"cookie") == ["foo", "bar"] # type: ignore[call-overload] |
| 327 |
|
| 328 |
def test_getitem_with_bytes(self, d: HTTPHeaderDict) -> None: |
| 329 |
d["Content-Type"] = "application/json" |
| 330 |
d.add("Content-Type", "charset=utf-8") |
| 331 |
result = d[b"Content-Type"] # type: ignore[index] |
| 332 |
assert result == "application/json, charset=utf-8" |
| 333 |
|
| 334 |
def test_contains_with_bytes(self, d: HTTPHeaderDict) -> None: |
| 335 |
d["Content-Type"] = "application/json" |
| 336 |
assert b"Content-Type" in d # type: ignore[comparison-overlap] |
| 337 |
assert b"X-Not-There" not in d # type: ignore[comparison-overlap] |
| 338 |
|
| 339 |
def test_getlist_after_copy(self, d: HTTPHeaderDict) -> None: |
| 340 |
assert d.getlist("cookie") == HTTPHeaderDict(d).getlist("cookie") |
| 341 |
|
| 342 |
def test_equal(self, d: HTTPHeaderDict) -> None: |
| 343 |
b = HTTPHeaderDict(cookie="foo, bar") |
| 344 |
c = NonMappingHeaderContainer(cookie="foo, bar") |
| 345 |
e = [("cookie", "foo, bar")] |
| 346 |
assert d == b |
| 347 |
assert d == c |
| 348 |
assert d == e |
| 349 |
assert d != 2 |
| 350 |
|
| 351 |
def test_not_equal(self, d: HTTPHeaderDict) -> None: |
| 352 |
b = HTTPHeaderDict(cookie="foo, bar") |
| 353 |
c = NonMappingHeaderContainer(cookie="foo, bar") |
| 354 |
e = [("cookie", "foo, bar")] |
| 355 |
assert not (d != b) |
| 356 |
assert not (d != c) |
| 357 |
assert not (d != e) |
| 358 |
assert d != 2 |
| 359 |
|
| 360 |
def test_pop(self, d: HTTPHeaderDict) -> None: |
| 361 |
key = "Cookie" |
| 362 |
a = d[key] |
| 363 |
b = d.pop(key) |
| 364 |
assert a == b |
| 365 |
assert key not in d |
| 366 |
with pytest.raises(KeyError): |
| 367 |
d.pop(key) |
| 368 |
dummy = object() |
| 369 |
assert dummy is d.pop(key, dummy) |
| 370 |
|
| 371 |
def test_discard(self, d: HTTPHeaderDict) -> None: |
| 372 |
d.discard("cookie") |
| 373 |
assert "cookie" not in d |
| 374 |
d.discard("cookie") |
| 375 |
|
| 376 |
def test_len(self, d: HTTPHeaderDict) -> None: |
| 377 |
assert len(d) == 1 |
| 378 |
d.add("cookie", "bla") |
| 379 |
d.add("asdf", "foo") |
| 380 |
# len determined by unique fieldnames |
| 381 |
assert len(d) == 2 |
| 382 |
|
| 383 |
def test_repr(self, d: HTTPHeaderDict) -> None: |
| 384 |
rep = "HTTPHeaderDict({'Cookie': 'foo, bar'})" |
| 385 |
assert repr(d) == rep |
| 386 |
|
| 387 |
def test_items(self, d: HTTPHeaderDict) -> None: |
| 388 |
items = d.items() |
| 389 |
assert len(items) == 2 |
| 390 |
assert list(items) == [ |
| 391 |
("Cookie", "foo"), |
| 392 |
("Cookie", "bar"), |
| 393 |
] |
| 394 |
assert ("Cookie", "foo") in items |
| 395 |
assert ("Cookie", "bar") in items |
| 396 |
assert ("X-Some-Header", "foo") not in items |
| 397 |
assert ("Cookie", "not_present") not in items |
| 398 |
assert ("Cookie", 1) not in items # type: ignore[comparison-overlap] |
| 399 |
assert "Cookie" not in items # type: ignore[comparison-overlap] |
| 400 |
|
| 401 |
def test_dict_conversion(self, d: HTTPHeaderDict) -> None: |
| 402 |
# Also tested in connectionpool, needs to preserve case |
| 403 |
hdict = { |
| 404 |
"Content-Length": "0", |
| 405 |
"Content-type": "text/plain", |
| 406 |
"Server": "Hypercorn/1.2.3", |
| 407 |
} |
| 408 |
h = dict(HTTPHeaderDict(hdict).items()) |
| 409 |
assert hdict == h |
| 410 |
assert hdict == dict(HTTPHeaderDict(hdict)) |
| 411 |
|
| 412 |
def test_string_enforcement(self, d: HTTPHeaderDict) -> None: |
| 413 |
# This currently throws AttributeError on key.lower(), should |
| 414 |
# probably be something nicer |
| 415 |
with pytest.raises(Exception): |
| 416 |
d[3] = "5" # type: ignore[index] |
| 417 |
with pytest.raises(Exception): |
| 418 |
d.add(3, "4") # type: ignore[arg-type] |
| 419 |
with pytest.raises(Exception): |
| 420 |
del d[3] # type: ignore[arg-type] |
| 421 |
with pytest.raises(Exception): |
| 422 |
HTTPHeaderDict({3: 3}) # type: ignore[arg-type] |
| 423 |
|
| 424 |
def test_dunder_contains(self, d: HTTPHeaderDict) -> None: |
| 425 |
""" |
| 426 |
Test: |
| 427 |
|
| 428 |
HTTPHeaderDict.__contains__ returns True |
| 429 |
- for matched string objects |
| 430 |
- for case-similar string objects |
| 431 |
HTTPHeaderDict.__contains__ returns False |
| 432 |
- for non-similar strings |
| 433 |
- for non-strings, even if they are keys |
| 434 |
in the underlying datastructure |
| 435 |
""" |
| 436 |
assert "cookie" in d |
| 437 |
assert "CoOkIe" in d |
| 438 |
assert "Not a cookie" not in d |
| 439 |
|
| 440 |
marker = object() |
| 441 |
d._container[marker] = ["some", "strings"] # type: ignore[index] |
| 442 |
assert marker not in d |
| 443 |
assert marker in d._container |
| 444 |
|
| 445 |
def test_union(self, d: HTTPHeaderDict) -> None: |
| 446 |
to_merge = {"Cookie": "tim-tam"} |
| 447 |
result = d | to_merge |
| 448 |
assert result == HTTPHeaderDict({"Cookie": "foo, bar, tim-tam"}) |
| 449 |
assert to_merge == {"Cookie": "tim-tam"} |
| 450 |
assert d == HTTPHeaderDict({"Cookie": "foo, bar"}) |
| 451 |
|
| 452 |
def test_union_rhs(self, d: HTTPHeaderDict) -> None: |
| 453 |
to_merge = {"Cookie": "tim-tam"} |
| 454 |
result = to_merge | d |
| 455 |
assert result == HTTPHeaderDict({"Cookie": "tim-tam, foo, bar"}) |
| 456 |
assert to_merge == {"Cookie": "tim-tam"} |
| 457 |
assert d == HTTPHeaderDict({"Cookie": "foo, bar"}) |
| 458 |
|
| 459 |
def test_inplace_union(self, d: HTTPHeaderDict) -> None: |
| 460 |
to_merge = {"Cookie": "tim-tam"} |
| 461 |
d |= to_merge |
| 462 |
assert d == HTTPHeaderDict({"Cookie": "foo, bar, tim-tam"}) |
| 463 |
|
| 464 |
def test_union_with_unsupported_type(self, d: HTTPHeaderDict) -> None: |
| 465 |
with pytest.raises(TypeError, match="unsupported operand type.*'int'"): |
| 466 |
d | 42 |
| 467 |
with pytest.raises(TypeError, match="unsupported operand type.*'float'"): |
| 468 |
3.14 | d |
| 469 |
|
| 470 |
def test_inplace_union_with_unsupported_type(self, d: HTTPHeaderDict) -> None: |
| 471 |
with pytest.raises(TypeError, match="unsupported operand type.*'NoneType'"): |
| 472 |
d |= None |