From 2fcafd169b5fcf4bb6711ca8aa4d59d80225ec7a Mon Sep 17 00:00:00 2001 From: David Evans Date: Tue, 26 Apr 2016 18:43:34 +0100 Subject: [PATCH] Fixed #26546 -- Allowed HTTPStatus enum values for HttpResponse.status. --- django/core/handlers/wsgi.py | 2 +- tests/handlers/tests.py | 22 +++++++++++++++++----- tests/handlers/urls.py | 1 + tests/handlers/views.py | 9 +++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 1f85511f62..3099cfb64f 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -171,7 +171,7 @@ class WSGIHandler(base.BaseHandler): 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()] for c in response.cookies.values(): response_headers.append((str('Set-Cookie'), str(c.output(header='')))) diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index a3f3ec7f70..d0b161cf44 100644 --- a/tests/handlers/tests.py +++ b/tests/handlers/tests.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals +import unittest + from django.core.handlers.wsgi import WSGIHandler, WSGIRequest, get_script_name from django.core.signals import request_finished, request_started from django.db import close_old_connections, connection @@ -11,6 +13,11 @@ from django.test import ( from django.utils import six from django.utils.encoding import force_str +try: + from http import HTTPStatus +except ImportError: # Python < 3.5 + HTTPStatus = None + class HandlerTests(SimpleTestCase): @@ -160,16 +167,12 @@ class SignalsTests(SimpleTestCase): @override_settings(ROOT_URLCONF='handlers.urls') -class HandlerSuspiciousOpsTest(SimpleTestCase): +class HandlerRequestTests(SimpleTestCase): def test_suspiciousop_in_view_returns_400(self): response = self.client.get('/suspicious/') self.assertEqual(response.status_code, 400) - -@override_settings(ROOT_URLCONF='handlers.urls') -class HandlerNotFoundTest(SimpleTestCase): - def test_invalid_urls(self): response = self.client.get('~%A9helloworld') 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 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): def test_get_script_name(self): diff --git a/tests/handlers/urls.py b/tests/handlers/urls.py index ad46ef6f03..9d23fd3fa7 100644 --- a/tests/handlers/urls.py +++ b/tests/handlers/urls.py @@ -11,4 +11,5 @@ urlpatterns = [ url(r'^not_in_transaction/$', views.not_in_transaction), url(r'^suspicious/$', views.suspicious), url(r'^malformed_post/$', views.malformed_post), + url(r'^httpstatus_enum/$', views.httpstatus_enum), ] diff --git a/tests/handlers/views.py b/tests/handlers/views.py index 4004dfe033..21c1387f08 100644 --- a/tests/handlers/views.py +++ b/tests/handlers/views.py @@ -5,6 +5,11 @@ from django.db import connection, transaction from django.http import HttpResponse, StreamingHttpResponse from django.views.decorators.csrf import csrf_exempt +try: + from http import HTTPStatus +except ImportError: # Python < 3.5 + pass + def regular(request): return HttpResponse(b"regular content") @@ -31,3 +36,7 @@ def suspicious(request): def malformed_post(request): request.POST return HttpResponse() + + +def httpstatus_enum(request): + return HttpResponse(status=HTTPStatus.OK)