Skip to content

Commit e6416e7

Browse files
committed
Test missing and wildcard Accept headers
1 parent 07f372d commit e6416e7

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

tbx/core/tests/test_views.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ def test_accessible(self) -> None:
1818
class PageNotFoundTestCase(TestCase):
1919
url = "/does-not-exist/"
2020

21-
def test_accessible(self) -> None:
22-
response = self.client.get(self.url)
23-
self.assertEqual(response.status_code, 404)
24-
self.assertIn("text/html", response.headers["content-type"])
25-
2621
def test_accept_html(self) -> None:
2722
response = self.client.get(self.url, headers={"Accept": "text/html"})
2823
self.assertEqual(response.status_code, 404)
@@ -39,3 +34,26 @@ def test_simple_when_html_not_highest(self) -> None:
3934
)
4035
self.assertEqual(response.status_code, 404)
4136
self.assertIn("text/plain", response.headers["content-type"])
37+
38+
def test_missing_accept_header(self) -> None:
39+
response = self.client.get(self.url, headers={"Accept": ""})
40+
self.assertEqual(response.status_code, 404)
41+
self.assertIn("text/plain", response.headers["content-type"])
42+
43+
def test_wildcard_accept_header(self) -> None:
44+
response = self.client.get(self.url, headers={"Accept": "*/*"})
45+
self.assertEqual(response.status_code, 404)
46+
self.assertIn("text/plain", response.headers["content-type"])
47+
48+
def test_browser_request(self) -> None:
49+
"""
50+
Test Accept header from Firefox 128
51+
"""
52+
response = self.client.get(
53+
self.url,
54+
headers={
55+
"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
56+
},
57+
)
58+
self.assertEqual(response.status_code, 404)
59+
self.assertIn("text/html", response.headers["content-type"])

tbx/core/utils/views.py

+7
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)