Fixed #26546 -- Allowed HTTPStatus enum values for HttpResponse.status.

This commit is contained in:
David Evans 2016-04-26 18:43:34 +01:00 committed by Tim Graham
parent 9f8941eda4
commit 2fcafd169b
4 changed files with 28 additions and 6 deletions

View File

@ -171,7 +171,7 @@ class WSGIHandler(base.BaseHandler):
response._handler_class = self.__class__ response._handler_class = self.__class__
status = '%s %s' % (response.status_code, response.reason_phrase) status = '%d %s' % (response.status_code, response.reason_phrase)
response_headers = [(str(k), str(v)) for k, v in response.items()] response_headers = [(str(k), str(v)) for k, v in response.items()]
for c in response.cookies.values(): for c in response.cookies.values():
response_headers.append((str('Set-Cookie'), str(c.output(header='')))) response_headers.append((str('Set-Cookie'), str(c.output(header=''))))

View File

@ -2,6 +2,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import unittest
from django.core.handlers.wsgi import WSGIHandler, WSGIRequest, get_script_name from django.core.handlers.wsgi import WSGIHandler, WSGIRequest, get_script_name
from django.core.signals import request_finished, request_started from django.core.signals import request_finished, request_started
from django.db import close_old_connections, connection from django.db import close_old_connections, connection
@ -11,6 +13,11 @@ from django.test import (
from django.utils import six from django.utils import six
from django.utils.encoding import force_str from django.utils.encoding import force_str
try:
from http import HTTPStatus
except ImportError: # Python < 3.5
HTTPStatus = None
class HandlerTests(SimpleTestCase): class HandlerTests(SimpleTestCase):
@ -160,16 +167,12 @@ class SignalsTests(SimpleTestCase):
@override_settings(ROOT_URLCONF='handlers.urls') @override_settings(ROOT_URLCONF='handlers.urls')
class HandlerSuspiciousOpsTest(SimpleTestCase): class HandlerRequestTests(SimpleTestCase):
def test_suspiciousop_in_view_returns_400(self): def test_suspiciousop_in_view_returns_400(self):
response = self.client.get('/suspicious/') response = self.client.get('/suspicious/')
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
@override_settings(ROOT_URLCONF='handlers.urls')
class HandlerNotFoundTest(SimpleTestCase):
def test_invalid_urls(self): def test_invalid_urls(self):
response = self.client.get('~%A9helloworld') response = self.client.get('~%A9helloworld')
self.assertContains(response, '~%A9helloworld', status_code=404) self.assertContains(response, '~%A9helloworld', status_code=404)
@ -187,6 +190,15 @@ class HandlerNotFoundTest(SimpleTestCase):
environ = RequestFactory().get('/%E2%A8%87%87%A5%E2%A8%A0').environ environ = RequestFactory().get('/%E2%A8%87%87%A5%E2%A8%A0').environ
self.assertIsInstance(environ['PATH_INFO'], six.text_type) self.assertIsInstance(environ['PATH_INFO'], six.text_type)
@unittest.skipIf(HTTPStatus is None, 'HTTPStatus only exists on Python 3.5+')
def test_handle_accepts_httpstatus_enum_value(self):
def start_response(status, headers):
start_response.status = status
environ = RequestFactory().get('/httpstatus_enum/').environ
WSGIHandler()(environ, start_response)
self.assertEqual(start_response.status, '200 OK')
class ScriptNameTests(SimpleTestCase): class ScriptNameTests(SimpleTestCase):
def test_get_script_name(self): def test_get_script_name(self):

View File

@ -11,4 +11,5 @@ urlpatterns = [
url(r'^not_in_transaction/$', views.not_in_transaction), url(r'^not_in_transaction/$', views.not_in_transaction),
url(r'^suspicious/$', views.suspicious), url(r'^suspicious/$', views.suspicious),
url(r'^malformed_post/$', views.malformed_post), url(r'^malformed_post/$', views.malformed_post),
url(r'^httpstatus_enum/$', views.httpstatus_enum),
] ]

View File

@ -5,6 +5,11 @@ from django.db import connection, transaction
from django.http import HttpResponse, StreamingHttpResponse from django.http import HttpResponse, StreamingHttpResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
try:
from http import HTTPStatus
except ImportError: # Python < 3.5
pass
def regular(request): def regular(request):
return HttpResponse(b"regular content") return HttpResponse(b"regular content")
@ -31,3 +36,7 @@ def suspicious(request):
def malformed_post(request): def malformed_post(request):
request.POST request.POST
return HttpResponse() return HttpResponse()
def httpstatus_enum(request):
return HttpResponse(status=HTTPStatus.OK)