Ticket #15762: patch_test_client_wsgi_input.diff

File patch_test_client_wsgi_input.diff, 3.7 KB (added by Tom Christie, 13 years ago)

Ensure test client wsgi.input is wrapped in LimitedStream

  • tests/regressiontests/test_client_regress/views.py

     
    9696    "A view that is requested with GET and accesses request.raw_post_data. Refs #14753."
    9797    return HttpResponse(request.raw_post_data)
    9898
     99def read_limited_stream(request):
     100    "A view that is requested with PUT and accesses request.read(LARGE_BUFFER)."
     101    return HttpResponse(request.read(99999))
     102
    99103def request_context_view(request):
    100104    # Special attribute that won't be present on a plain HttpRequest
    101105    request.special_path = request.path
  • tests/regressiontests/test_client_regress/models.py

     
    908908            response = self.client.get("/test_client_regress/raw_post_data/")
    909909        except AssertionError:
    910910            self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.")
     911
     912class ReadLimitedStreamTest(TestCase):
     913    """
     914    Attempting to read beyond META["CONTENT_LENGTH"] from the test client
     915    should simply return the full content of the request.
     916    """
     917    def test_read_limited_stream(self):
     918        try:
     919            response = self.client.put("/test_client_regress/read_limited_stream/", data={'foo':'whiz'})
     920        except AssertionError:
     921            self.fail("Reading more than META['CONTENT_LENGTH'] of data from a view fetched with PUT by the test client " +
     922                      "shouldn't fail, it should simply return the full content of the request.")       
  • tests/regressiontests/test_client_regress/urls.py

     
    2727    (r'^check_headers/$', views.check_headers),
    2828    (r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')),
    2929    (r'^raw_post_data/$', views.raw_post_data),
     30    (r'^read_limited_stream/$', views.read_limited_stream),
    3031    (r'^request_context_view/$', views.request_context_view),
    3132)
  • django/test/client.py

     
    2626from django.utils.itercompat import is_iterable
    2727from django.db import transaction, close_connection
    2828from django.test.utils import ContextList
     29import socket
    2930
    3031__all__ = ('Client', 'RequestFactory', 'encode_file', 'encode_multipart')
    3132
     
    3435MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
    3536CONTENT_TYPE_RE = re.compile('.*; charset=([\w\d-]+);?')
    3637
    37 class FakePayload(object):
     38class FakePayload(socket._fileobject):
    3839    """
    3940    A wrapper around StringIO that restricts what can be read since data from
    4041    the network can't be seeked and cannot be read outside of its content
    4142    length. This makes sure that views can't do anything under the test client
    4243    that wouldn't work in Real Life.
     44
     45    We fake this up as a socket._fileobject, so that the WSGIrequest wraps it
     46    up in a LimitedStream as it would do for any WSGI stream that hasn't
     47    already defined it's own .read()/.readlines() implementations.
    4348    """
    4449    def __init__(self, content):
    4550        self.__content = StringIO(content)
Back to Top