Updated/removed tests due toe accept/mediatype complete API and semantics change.

This commit is contained in:
L. Karkkainen 2023-01-30 02:24:12 +00:00
parent 8da10a9c0c
commit 2e2231919c

View File

@ -185,30 +185,22 @@ def test_request_line(app):
assert request.request_line == b"GET / HTTP/1.1"
@pytest.mark.parametrize(
"raw",
(
"show/first, show/second",
"show/*, show/first",
"*/*, show/first",
"*/*, show/*",
"other/*; q=0.1, show/*; q=0.2",
"show/first; q=0.5, show/second; q=0.5",
"show/first; foo=bar, show/second; foo=bar",
"show/second, show/first; foo=bar",
"show/second; q=0.5, show/first; foo=bar; q=0.5",
"show/second; q=0.5, show/first; q=1.0",
"show/first, show/second; q=1.0",
),
)
def test_parse_accept_ordered_okay(raw):
ordered = headers.parse_accept(raw)
expected_subtype = (
"*" if all(q.subtype.is_wildcard for q in ordered) else "first"
"text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
"application/xml;q=0.9, */*;q=0.8, text/html, application/xhtml+xml",
"foo/bar;q=0.9, */*;q=0.8, text/html=0.8, text/plain, application/xhtml+xml",
)
assert ordered[0].type_ == "show"
assert ordered[0].subtype == expected_subtype
)
def test_accept_ordering(raw):
"""Should sort by q but also be stable."""
accept = headers.parse_accept(raw)
assert accept[0].type_ == "text"
raw1 = ", ".join(str(a) for a in accept)
accept = headers.parse_accept(raw1)
raw2 = ", ".join(str(a) for a in accept)
assert raw1 == raw2
@pytest.mark.parametrize(
@ -225,40 +217,27 @@ def test_bad_accept(raw):
def test_empty_accept():
assert headers.parse_accept("") == []
a = headers.parse_accept("")
assert a == []
assert not a.match("*/*")
def test_wildcard_accept_set_ok():
accept = headers.parse_accept("*/*")[0]
assert accept.type_.is_wildcard
assert accept.subtype.is_wildcard
assert accept.is_wildcard
assert accept.has_wildcard
accept = headers.parse_accept("foo/*")[0]
assert not accept.is_wildcard
assert accept.has_wildcard
accept = headers.parse_accept("*/bar")[0]
assert not accept.is_wildcard
assert accept.has_wildcard
accept = headers.parse_accept("foo/bar")[0]
assert not accept.type_.is_wildcard
assert not accept.subtype.is_wildcard
def test_accept_parsed_against_str():
accept = headers.MediaType._parse("foo/bar")
assert accept > "foo/bar; q=0.1"
def test_media_type_equality():
assert headers.MediaType("foo") == headers.MediaType("foo") == "foo"
assert headers.MediaType("foo") == headers.MediaType("*") == "*"
assert headers.MediaType("foo") != headers.MediaType("bar")
assert headers.MediaType("foo") != "bar"
def test_media_type_matching():
assert headers.MediaType("foo").match(headers.MediaType("foo"))
assert headers.MediaType("foo").match("foo")
assert not headers.MediaType("foo").match(headers.MediaType("*"))
assert not headers.MediaType("foo").match("*")
assert not headers.MediaType("foo").match(headers.MediaType("bar"))
assert not headers.MediaType("foo").match("bar")
assert not accept.is_wildcard
assert not accept.has_wildcard
@pytest.mark.parametrize(
@ -266,87 +245,52 @@ def test_media_type_matching():
(
# ALLOW BOTH
("foo/bar", "foo/bar", True, True, True),
("foo/bar", headers.MediaType._parse("foo/bar"), True, True, True),
("foo/bar", "foo/*", True, True, True),
("foo/bar", headers.MediaType._parse("foo/*"), True, True, True),
("foo/bar", "*/*", True, True, True),
("foo/bar", headers.MediaType._parse("*/*"), True, True, True),
("foo/*", "foo/bar", True, True, True),
("foo/*", headers.MediaType._parse("foo/bar"), True, True, True),
("foo/*", "foo/*", True, True, True),
("foo/*", headers.MediaType._parse("foo/*"), True, True, True),
("foo/*", "*/*", True, True, True),
("foo/*", headers.MediaType._parse("*/*"), True, True, True),
("*/*", "foo/bar", True, True, True),
("*/*", headers.MediaType._parse("foo/bar"), True, True, True),
("*/*", "foo/*", True, True, True),
("*/*", headers.MediaType._parse("foo/*"), True, True, True),
("*/*", "*/*", True, True, True),
("*/*", headers.MediaType._parse("*/*"), True, True, True),
# ALLOW TYPE
("foo/bar", "foo/bar", True, True, False),
("foo/bar", headers.MediaType._parse("foo/bar"), True, True, False),
("foo/bar", "foo/*", False, True, False),
("foo/bar", headers.MediaType._parse("foo/*"), False, True, False),
("foo/bar", "*/*", False, True, False),
("foo/bar", headers.MediaType._parse("*/*"), False, True, False),
("foo/*", "foo/bar", False, True, False),
("foo/*", headers.MediaType._parse("foo/bar"), False, True, False),
("foo/*", "foo/*", False, True, False),
("foo/*", headers.MediaType._parse("foo/*"), False, True, False),
("foo/*", "*/*", False, True, False),
("foo/*", headers.MediaType._parse("*/*"), False, True, False),
("*/*", "foo/bar", False, True, False),
("*/*", headers.MediaType._parse("foo/bar"), False, True, False),
("*/*", "foo/*", False, True, False),
("*/*", headers.MediaType._parse("foo/*"), False, True, False),
("*/*", "*/*", False, True, False),
("*/*", headers.MediaType._parse("*/*"), False, True, False),
# ALLOW SUBTYPE
("foo/bar", "foo/bar", True, False, True),
("foo/bar", headers.MediaType._parse("foo/bar"), True, False, True),
("foo/bar", "foo/*", True, False, True),
("foo/bar", headers.MediaType._parse("foo/*"), True, False, True),
("foo/bar", "*/*", False, False, True),
("foo/bar", headers.MediaType._parse("*/*"), False, False, True),
("foo/*", "foo/bar", True, False, True),
("foo/*", headers.MediaType._parse("foo/bar"), True, False, True),
("foo/*", "foo/*", True, False, True),
("foo/*", headers.MediaType._parse("foo/*"), True, False, True),
("foo/*", "*/*", False, False, True),
("foo/*", headers.MediaType._parse("*/*"), False, False, True),
("*/*", "foo/bar", False, False, True),
("*/*", headers.MediaType._parse("foo/bar"), False, False, True),
("*/*", "foo/*", False, False, True),
("*/*", headers.MediaType._parse("foo/*"), False, False, True),
("*/*", "*/*", False, False, True),
("*/*", headers.MediaType._parse("*/*"), False, False, True),
),
)
def test_accept_matching(value, other, outcome, allow_type, allow_subtype):
assert (
headers.MediaType._parse(value).match(
bool(headers.MediaType._parse(value).match(
other,
allow_type_wildcard=allow_type,
allow_subtype_wildcard=allow_subtype,
)
))
is outcome
)
@pytest.mark.parametrize("value", ("foo/bar", "foo/*", "*/*"))
def test_value_in_accept(value):
acceptable = headers.parse_accept(value)
assert "foo/bar" in acceptable
assert "foo/*" in acceptable
assert "*/*" in acceptable
@pytest.mark.parametrize("value", ("foo/bar", "foo/*"))
def test_value_not_in_accept(value):
acceptable = headers.parse_accept(value)
assert "no/match" not in acceptable
assert "no/*" not in acceptable
assert "*/*" not in acceptable
assert "*/bar" not in acceptable
@pytest.mark.parametrize(
@ -355,16 +299,22 @@ def test_value_not_in_accept(value):
(
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", # noqa: E501
[
"text/html",
"application/xhtml+xml",
"image/avif",
"image/webp",
"application/xml;q=0.9",
"*/*;q=0.8",
("text/html", 1.0),
("application/xhtml+xml", 1.0),
("image/avif", 1.0),
("image/webp", 1.0),
("application/xml", 0.9),
("*/*", 0.8),
],
),
),
)
def test_browser_headers(header, expected):
mimes = [e[0] for e in expected]
qs = [e[1] for e in expected]
request = Request(b"/", {"accept": header}, "1.1", "GET", None, None)
assert request.accept == expected
assert request.accept == mimes
for a, m, q in zip(request.accept, mimes, qs):
assert a == m
assert a.str == m
assert a.q == q