smolweb/tests/test_negotiate.py
2026-09-26 13:34:12 +03:00

48 lines
1.8 KiB
Python

"""Accept-header negotiation between WML and XHTML-MP."""
from __future__ import annotations
import pytest
from smolweb.negotiate import FORMAT_HTML, FORMAT_WML, FORMAT_XHTML, negotiate
class TestWildcardNeverSelectsWml:
def test_modern_browser_accept_header_gets_html(self):
# The exact header Firefox/Chrome send: a browser must never be
# handed WML through the */* wildcard.
accept = "text/html,application/xhtml+xml;q=0.9,image/webp,*/*;q=0.8"
assert negotiate(accept) == FORMAT_HTML
def test_bare_wildcard_gets_html(self):
assert negotiate("*/*") == FORMAT_HTML
def test_no_accept_header_gets_html(self):
assert negotiate(None) == FORMAT_HTML
class TestLiteralMatches:
def test_wml_accept_header_gets_wml(self):
assert negotiate("text/vnd.wap.wml") == FORMAT_WML
def test_xhtml_mp_accept_header_gets_xhtml(self):
assert negotiate("application/vnd.wap.xhtml+xml") == FORMAT_XHTML
def test_wml_wins_when_both_present_with_higher_q(self):
accept = "text/vnd.wap.wml;q=1.0, application/vnd.wap.xhtml+xml;q=0.5"
assert negotiate(accept) == FORMAT_WML
def test_xhtml_wins_when_both_present_with_higher_q(self):
accept = "text/vnd.wap.wml;q=0.5, application/vnd.wap.xhtml+xml;q=1.0"
assert negotiate(accept) == FORMAT_XHTML
def test_unrelated_type_gets_html_fallback(self):
assert negotiate("application/json") == FORMAT_HTML
class TestFormatOverride:
@pytest.mark.parametrize("fmt", [FORMAT_WML, FORMAT_XHTML, FORMAT_HTML])
def test_override_wins_regardless_of_accept(self, fmt):
assert negotiate("*/*", format_override=fmt) == fmt
def test_invalid_override_is_ignored(self):
assert negotiate("text/vnd.wap.wml", format_override="bogus") == FORMAT_WML