Skip to content

Commit 89ae354

Browse files
committed
Test missing and wildcard Accept headers
1 parent 8109908 commit 89ae354

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

tbx/core/tests/test_views.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ def test_setting_theme_on_one_site_sets_it_on_multiple_sites(self):
110110
class PageNotFoundTestCase(TestCase):
111111
url = "/does-not-exist/"
112112

113-
def test_accessible(self) -> None:
114-
response = self.client.get(self.url)
115-
self.assertEqual(response.status_code, 404)
116-
self.assertIn("text/html", response.headers["content-type"])
117-
118113
def test_accept_html(self) -> None:
119114
response = self.client.get(self.url, headers={"Accept": "text/html"})
120115
self.assertEqual(response.status_code, 404)
@@ -131,3 +126,26 @@ def test_simple_when_html_not_highest(self) -> None:
131126
)
132127
self.assertEqual(response.status_code, 404)
133128
self.assertIn("text/plain", response.headers["content-type"])
129+
130+
def test_missing_accept_header(self) -> None:
131+
response = self.client.get(self.url, headers={"Accept": ""})
132+
self.assertEqual(response.status_code, 404)
133+
self.assertIn("text/plain", response.headers["content-type"])
134+
135+
def test_wildcard_accept_header(self) -> None:
136+
response = self.client.get(self.url, headers={"Accept": "*/*"})
137+
self.assertEqual(response.status_code, 404)
138+
self.assertIn("text/plain", response.headers["content-type"])
139+
140+
def test_browser_request(self) -> None:
141+
"""
142+
Test Accept header from Firefox 128
143+
"""
144+
response = self.client.get(
145+
self.url,
146+
headers={
147+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8" # noqa:E501
148+
},
149+
)
150+
self.assertEqual(response.status_code, 404)
151+
self.assertIn("text/html", response.headers["content-type"])

tbx/core/utils/views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ def get_quality(media_type):
1010

1111

1212
def show_html_error_page(request):
13+
# If there is no `Accept` header, serve the simpler page
14+
if not request.headers.get("Accept"):
15+
return False
16+
1317
accepted_types = sorted(request.accepted_types, key=get_quality, reverse=True)
1418

19+
if len(accepted_types) == 1 and accepted_types[0].match("*/*"):
20+
return False
21+
1522
html_type = next(
1623
(
1724
accepted_type

0 commit comments

Comments
 (0)