Ticket #18678: 18678-1.diff

File 18678-1.diff, 5.0 KB (added by Claude Paroz, 12 years ago)

Allow HttpResponse arguments for subclasses

  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index 7782ebe..c6f1046 100644
    a b class HttpResponse(object):  
    728728class HttpResponseRedirectBase(HttpResponse):
    729729    allowed_schemes = ['http', 'https', 'ftp']
    730730
    731     def __init__(self, redirect_to):
     731    def __init__(self, redirect_to, *args, **kwargs):
    732732        parsed = urlparse(redirect_to)
    733733        if parsed.scheme and parsed.scheme not in self.allowed_schemes:
    734734            raise SuspiciousOperation("Unsafe redirect to URL with protocol '%s'" % parsed.scheme)
    735         super(HttpResponseRedirectBase, self).__init__()
     735        super(HttpResponseRedirectBase, self).__init__(*args, **kwargs)
    736736        self['Location'] = iri_to_uri(redirect_to)
    737737
    738738class HttpResponseRedirect(HttpResponseRedirectBase):
    class HttpResponseForbidden(HttpResponse):  
    766766class HttpResponseNotAllowed(HttpResponse):
    767767    status_code = 405
    768768
    769     def __init__(self, permitted_methods):
    770         super(HttpResponseNotAllowed, self).__init__()
     769    def __init__(self, permitted_methods, *args, **kwargs):
     770        super(HttpResponseNotAllowed, self).__init__(*args, **kwargs)
    771771        self['Allow'] = ', '.join(permitted_methods)
    772772
    773773class HttpResponseGone(HttpResponse):
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index 8d514dc..21e99de 100644
    a b types of HTTP responses. Like ``HttpResponse``, these subclasses live in  
    731731
    732732.. class:: HttpResponseRedirect
    733733
    734     The constructor takes a single argument -- the path to redirect to. This
    735     can be a fully qualified URL (e.g. ``'http://www.yahoo.com/search/'``) or
    736     an absolute path with no domain (e.g. ``'/search/'``). Note that this
    737     returns an HTTP status code 302.
     734    The first argument to the constructor is required -- the path to redirect
     735    to. This can be a fully qualified URL
     736    (e.g. ``'http://www.yahoo.com/search/'``) or an absolute path with no
     737    domain (e.g. ``'/search/'``). See :class:`HttpResponse` for other optional
     738    constructor arguments. Note that this returns an HTTP status code 302.
    738739
    739740.. class:: HttpResponsePermanentRedirect
    740741
    types of HTTP responses. Like ``HttpResponse``, these subclasses live in  
    761762
    762763.. class:: HttpResponseNotAllowed
    763764
    764     Like :class:`HttpResponse`, but uses a 405 status code. Takes a single,
    765     required argument: a list of permitted methods (e.g. ``['GET', 'POST']``).
     765    Like :class:`HttpResponse`, but uses a 405 status code. The first argument
     766    to the constructor is required: a list of permitted methods (e.g.
     767    ``['GET', 'POST']``).
    766768
    767769.. class:: HttpResponseGone
    768770
  • tests/regressiontests/httpwrappers/tests.py

    diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
    index c84b2cd..21ba198 100644
    a b import pickle  
    66
    77from django.core.exceptions import SuspiciousOperation
    88from django.http import (QueryDict, HttpResponse, HttpResponseRedirect,
    9                          HttpResponsePermanentRedirect, HttpResponseNotModified,
     9                         HttpResponsePermanentRedirect, HttpResponseNotAllowed,
     10                         HttpResponseNotModified,
    1011                         SimpleCookie, BadHeaderError,
    1112                         parse_cookie)
     13from django.test import TestCase
    1214from django.utils import six
    1315from django.utils import unittest
    1416
    class HttpResponseTests(unittest.TestCase):  
    330332                              HttpResponsePermanentRedirect, url)
    331333
    332334
    333 class HttpResponseSubclassesTests(unittest.TestCase):
     335class HttpResponseSubclassesTests(TestCase):
     336    def test_redirect(self):
     337        response = HttpResponseRedirect('/redirected/')
     338        self.assertEqual(response.status_code, 302)
     339        # Test that standard HttpResponse init args can be used
     340        response = HttpResponseRedirect('/redirected/',
     341            content='The resource has temporarily moved',
     342            content_type='text/html')
     343        self.assertContains(response, 'The resource has temporarily moved', status_code=302)
     344
    334345    def test_not_modified(self):
    335346        response = HttpResponseNotModified()
    336347        self.assertEqual(response.status_code, 304)
    class HttpResponseSubclassesTests(unittest.TestCase):  
    339350            response.content = "Hello dear"
    340351        self.assertNotIn('content-type', response)
    341352
     353    def test_not_allowed(self):
     354        response = HttpResponseNotAllowed(['GET'])
     355        self.assertEqual(response.status_code, 405)
     356        # Test that standard HttpResponse init args can be used
     357        response = HttpResponseNotAllowed(['GET'],
     358            content='Only the GET method is allowed',
     359            content_type='text/html')
     360        self.assertContains(response, 'Only the GET method is allowed', status_code=405)
    342361
    343362class CookieTests(unittest.TestCase):
    344363    def test_encode(self):
Back to Top