From 2ee2affc3a4ecefb96a24786c5527bf0550270fc Mon Sep 17 00:00:00 2001 From: Bogdan Tishenko <95874565+Tezlaa@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:00:05 +0300 Subject: [PATCH] Update api.py. Add validation on the status code 204. (#569) If the status code is 204 (no content), return an empty dict for the content --- {{cookiecutter.project_slug}}/src/app/testing/api.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/src/app/testing/api.py b/{{cookiecutter.project_slug}}/src/app/testing/api.py index f9d66ae2..3f65376a 100644 --- a/{{cookiecutter.project_slug}}/src/app/testing/api.py +++ b/{{cookiecutter.project_slug}}/src/app/testing/api.py @@ -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 @@ -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): @@ -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")