Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/utils.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Any, Dict, Optional, cast | from typing import Any, Dict, Optional, cast | ||||
from django.http import HttpResponse | from django.http import HttpResponse, StreamingHttpResponse | ||||
from django.test.client import Client | from django.test.client import Client | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from rest_framework.test import APIClient | from rest_framework.test import APIClient | ||||
from swh.web.tests.django_asserts import assert_template_used | from swh.web.tests.django_asserts import assert_template_used | ||||
def _assert_http_response( | def _assert_http_response( | ||||
response: HttpResponse, status_code: int, content_type: str | response: HttpResponse, status_code: int, content_type: str | ||||
) -> HttpResponse: | ) -> HttpResponse: | ||||
if isinstance(response, Response): | if isinstance(response, Response): | ||||
drf_response = cast(Response, response) | drf_response = cast(Response, response) | ||||
error_context = ( | error_context = ( | ||||
drf_response.data.pop("traceback") | drf_response.data.pop("traceback") | ||||
if isinstance(drf_response.data, dict) and "traceback" in drf_response.data | if isinstance(drf_response.data, dict) and "traceback" in drf_response.data | ||||
else drf_response.data | else drf_response.data | ||||
) | ) | ||||
elif isinstance(response, StreamingHttpResponse): | |||||
error_context = getattr(response, "traceback", response.streaming_content) | |||||
else: | else: | ||||
error_context = getattr(response, "traceback", response.content) | error_context = getattr(response, "traceback", response.content) | ||||
assert response.status_code == status_code, error_context | assert response.status_code == status_code, error_context | ||||
if content_type != "*/*": | if content_type != "*/*": | ||||
assert response["Content-Type"].startswith(content_type) | assert response["Content-Type"].startswith(content_type) | ||||
return response | return response | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |