From 662ddf44b910fbdc92ca7dca03757bba12692f88 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 29 Mar 2021 14:05:58 -0700 Subject: [PATCH] Avoid error when request Accept header is not set --- bookwyrm/tests/views/test_helpers.py | 5 +++++ bookwyrm/views/helpers.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py index 2d100f7b..a9dadcad 100644 --- a/bookwyrm/tests/views/test_helpers.py +++ b/bookwyrm/tests/views/test_helpers.py @@ -84,6 +84,11 @@ class ViewsHelpers(TestCase): request.headers = {"Accept": "Praise"} self.assertFalse(views.helpers.is_api_request(request)) + def test_is_api_request_no_headers(self, _): + """ should it return html or json """ + request = self.factory.get("/path") + self.assertFalse(views.helpers.is_api_request(request)) + def test_is_bookwyrm_request(self, _): """ checks if a request came from a bookwyrm instance """ request = self.factory.get("", {"q": "Test Book"}) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index 52b5818f..006c6494 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -21,7 +21,7 @@ def get_user_from_username(viewer, username): def is_api_request(request): """ check whether a request is asking for html or data """ - return "json" in request.headers.get("Accept") or request.path[-5:] == ".json" + return "json" in request.headers.get("Accept", "") or request.path[-5:] == ".json" def is_bookwyrm_request(request):