Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge 55bd4da into 2700cdc
  • Loading branch information
gbansaghi committed Jan 6, 2020
2 parents 2700cdc + 55bd4da commit 956d74c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
9 changes: 5 additions & 4 deletions tastypie/resources.py
Expand Up @@ -30,6 +30,7 @@
ReverseOneToOneDescriptor

from django.http import HttpResponse, HttpResponseNotFound, Http404
from django.http.response import HttpResponseBase
from django.utils.cache import patch_cache_control, patch_vary_headers
from django.utils.html import escape
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -256,7 +257,7 @@ def wrapper(request, *args, **kwargs):
except Exception as e:
# Prevent muting non-django's exceptions
# i.e. RequestException from 'requests' library
if hasattr(e, 'response') and isinstance(e.response, HttpResponse):
if hasattr(e, 'response') and isinstance(e.response, HttpResponseBase):
return e.response

# A real, non-expected exception.
Expand All @@ -281,7 +282,7 @@ def get_response_class_for_exception(self, request, exception):
"""
Can be overridden to customize response classes used for uncaught
exceptions. Should always return a subclass of
``django.http.HttpResponse``.
``django.http.response.HttpResponseBase``.
"""
if isinstance(exception, (NotFound, ObjectDoesNotExist, Http404)):
return HttpResponseNotFound
Expand Down Expand Up @@ -504,7 +505,7 @@ def dispatch(self, request_type, request, **kwargs):
# If what comes back isn't a ``HttpResponse``, assume that the
# request was accepted and that some action occurred. This also
# prevents Django from freaking out.
if not isinstance(response, HttpResponse):
if not isinstance(response, HttpResponseBase):
return http.HttpNoContent()

return response
Expand Down Expand Up @@ -576,7 +577,7 @@ def is_authenticated(self, request):
# Authenticate the request as needed.
auth_result = self._meta.authentication.is_authenticated(request)

if isinstance(auth_result, HttpResponse):
if isinstance(auth_result, HttpResponseBase):
raise ImmediateHttpResponse(response=auth_result)

if auth_result is not True:
Expand Down
23 changes: 22 additions & 1 deletion tests/core/tests/resources.py
Expand Up @@ -6,6 +6,7 @@
import copy
import datetime
from decimal import Decimal
from io import BytesIO
import json
from mock import patch, Mock
import sys
Expand All @@ -22,7 +23,7 @@
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.http import HttpRequest, QueryDict, Http404
from django.http import FileResponse, HttpRequest, QueryDict, Http404
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone
Expand Down Expand Up @@ -190,6 +191,14 @@ class Meta:
authorization = Authorization()


class FileResource(Resource):
class Meta:
file_allowed_methods = ['get']

def get_file(self, request, **kwargs):
return FileResponse(BytesIO(b'file contents'))


class MangledBasicResource(BasicResource):
class Meta:
object_class = TestObject
Expand Down Expand Up @@ -894,6 +903,18 @@ def test_get_list_with_use_in(self):

self.assertNotIn('view_count', basic_resource_list[0])

def test_dispatch(self):
resource = FileResource()
request = HttpRequest()
request.method = 'GET'

resp = resource.dispatch('file', request)
self.assertEqual(resp.status_code, 200)
content = b''
for chunk in resp.streaming_content:
content += chunk
self.assertEqual(content, b'file contents')


# ====================
# Model-based tests...
Expand Down

0 comments on commit 956d74c

Please sign in to comment.