Skip to content

Commit

Permalink
test(mediatypes): add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vytas7 committed Oct 1, 2024
1 parent 69bd926 commit 9fdc9ce
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion falcon/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ async def __call__( # type: ignore[override] # noqa: C901
data = resp._data

if data is None and resp._media is not None:
# NOTE(kgriffs): We use a special MISSING singleton since
# NOTE(kgriffs): We use a special _UNSET singleton since
# None is ambiguous (the media handler might return None).
if resp._media_rendered is _UNSET:
opt = resp.options
Expand Down
2 changes: 1 addition & 1 deletion falcon/asgi/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(

self.uri_template = None
# PERF(vytas): Fall back to class variable(s) when unset.
# self._media = MISSING
# self._media = _UNSET
# self._media_error = None

# TODO(kgriffs): ASGI does not specify whether 'path' may be empty,
Expand Down
2 changes: 1 addition & 1 deletion falcon/asgi/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async def render_body(self) -> Optional[bytes]: # type: ignore[override]
data = self._data

if data is None and self._media is not None:
# NOTE(kgriffs): We use a special MISSING singleton since
# NOTE(kgriffs): We use a special _UNSET singleton since
# None is ambiguous (the media handler might return None).
if self._media_rendered is _UNSET:
if not self.content_type:
Expand Down
2 changes: 1 addition & 1 deletion falcon/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def render_body(self) -> Optional[bytes]:
data = self._data

if data is None and self._media is not None:
# NOTE(kgriffs): We use a special MISSING singleton since
# NOTE(kgriffs): We use a special _UNSET singleton since
# None is ambiguous (the media handler might return None).
if self._media_rendered is _UNSET:
if not self.content_type:
Expand Down
53 changes: 52 additions & 1 deletion tests/test_mediatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_media_range_private_cls():
)
# NOTE(vytas): Including the errata https://www.rfc-editor.org/errata/eid7138.
_RFC_9110_EXAMPLE_VALUES = [
('text/plain;format=flowed', 1),
('text/plain;format=flowed', 1.0),
('text/plain', 0.7),
('text/html', 0.3),
('image/jpeg', 0.5),
Expand Down Expand Up @@ -136,6 +136,53 @@ def test_quality_rfc_examples(accept, media_type, quality_value):
assert pytest.approx(mediatypes.quality(media_type, accept)) == quality_value


@pytest.mark.parametrize(
'accept,media_type,quality_value',
[
(
'application/*, */wildcard; q=0.7, */*; q=0.25',
'test/wildcard; expect=pass',
0.7,
),
(
'application/*, */wildcard; q=0.7, */*; q=0.25',
'application/wildcard; expect=pass',
1.0,
),
(
'application/*, */wildcard; q=0.7, */*; q=0.25',
'test/wildcard; expect=pass',
0.7,
),
(
'text/x-python, text/*; q=0.33, text/plain; format=fixed',
'text/plain; format=flowed',
0.33,
),
],
)
def test_quality(accept, media_type, quality_value):
assert pytest.approx(mediatypes.quality(media_type, accept)) == quality_value


@pytest.mark.parametrize(
'accept,media_type',
[
(
'foo/bar, test/app; q=0.2, test/app; p=1; q=0.9, test/app;p=1;r=2',
'test/app',
),
('test/app; q=0.1, test/app; p=1; q=0.2, test/app;p=1;r=2', 'test/app; p=1'),
(
'*/app; q=0.1, simple/app; test=true; q=0.2, simple/app; color=blue',
'simple/app; test=true',
),
],
)
def test_quality_prefer_exact_match(accept, media_type):
assert pytest.approx(mediatypes.quality(media_type, accept)) == 0.2


@pytest.mark.parametrize(
'accept,media_type',
[
Expand All @@ -147,6 +194,10 @@ def test_quality_rfc_examples(accept, media_type, quality_value):
),
('text/html, text/plain', 'text/x-python'),
('*/json; q=0.2, application/json', 'application/msgpack'),
(
'text/x-python, image/*; q=0.33, text/plain; format=fixed',
'text/plain; format=flowed',
),
],
)
def test_quality_none_matches(accept, media_type):
Expand Down

0 comments on commit 9fdc9ce

Please sign in to comment.