Skip to content

Commit

Permalink
Update api.py. Add validation on the status code 204. (#569)
Browse files Browse the repository at this point in the history
If the status code is 204 (no content), return an empty dict for the content
  • Loading branch information
Tezlaa authored Dec 10, 2023
1 parent 1eb929a commit 2ee2aff
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions {{cookiecutter.project_slug}}/src/app/testing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient as DRFAPIClient
from rest_framework.response import Response

from users.models import User

Expand Down Expand Up @@ -58,7 +59,10 @@ def _request(self, method, expected, *args, **kwargs):
assert response.status_code == expected, content
return content

def _decode(self, response):
def _decode(self, response: Response):
if response.status_code == 204:
return {}

content = response.content.decode("utf-8", errors="ignore")

if self.is_json(response):
Expand All @@ -67,7 +71,7 @@ def _decode(self, response):
return content

@staticmethod
def is_json(response) -> bool:
def is_json(response: Response) -> bool:
if response.has_header("content-type"):
return "json" in response.get("content-type")

Expand Down

0 comments on commit 2ee2aff

Please sign in to comment.