Avoid error when request Accept header is not set

This commit is contained in:
Mouse Reeve 2021-03-29 14:05:58 -07:00
parent 18c552d215
commit 662ddf44b9
2 changed files with 6 additions and 1 deletions

View file

@ -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"})

View file

@ -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):