From b0467bac975abdc2fa3b49e650f92d9f81fcd77e Mon Sep 17 00:00:00 2001
From: Gisle Aas <gisle@aas.no>
Date: Mon, 22 Feb 2010 17:31:54 -0500
Subject: [PATCH 1/2] Ticket #12747 Set custom status_text message
---
django/core/handlers/wsgi.py | 2 +-
django/http/__init__.py | 14 +++++++++++++-
docs/ref/request-response.txt | 11 ++++++++++-
tests/regressiontests/httpwrappers/tests.py | 20 ++++++++++++++++++++
4 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
index 927b098..0320048 100644
a
|
b
|
class WSGIHandler(base.BaseHandler):
|
248 | 248 | signals.request_finished.send(sender=self.__class__) |
249 | 249 | |
250 | 250 | try: |
251 | | status_text = STATUS_CODE_TEXT[response.status_code] |
| 251 | status_text = response.status_text or STATUS_CODE_TEXT[response.status_code] |
252 | 252 | except KeyError: |
253 | 253 | status_text = 'UNKNOWN STATUS CODE' |
254 | 254 | status = '%s %s' % (response.status_code, status_text) |
diff --git a/django/http/__init__.py b/django/http/__init__.py
index 6353637..b2cd794 100644
a
|
b
|
def parse_cookie(cookie):
|
293 | 293 | cookiedict[key] = c.get(key).value |
294 | 294 | return cookiedict |
295 | 295 | |
| 296 | class BadStatusError(ValueError): |
| 297 | pass |
| 298 | |
296 | 299 | class BadHeaderError(ValueError): |
297 | 300 | pass |
298 | 301 | |
… |
… |
class HttpResponse(object):
|
300 | 303 | """A basic HTTP response, with content and dictionary-accessed headers.""" |
301 | 304 | |
302 | 305 | status_code = 200 |
| 306 | status_text = None |
303 | 307 | |
304 | 308 | def __init__(self, content='', mimetype=None, status=None, |
305 | 309 | content_type=None): |
… |
… |
class HttpResponse(object):
|
318 | 322 | self._is_string = True |
319 | 323 | self.cookies = CompatCookie() |
320 | 324 | if status: |
321 | | self.status_code = status |
| 325 | if isinstance(status, basestring): |
| 326 | m = re.match(r'(\d{3}) +([^\n]*)$', status) |
| 327 | if m: |
| 328 | self.status_code = int(m.group(1)) |
| 329 | self.status_text = m.group(2) |
| 330 | else: |
| 331 | raise BadStatusError("Bad HTTP status line string (%r)" % (status)) |
| 332 | else: |
| 333 | self.status_code = status |
322 | 334 | |
323 | 335 | # _headers is a mapping of the lower-case name to the original case of |
324 | 336 | # the header (required for working with legacy systems) and the header |
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index 1788e06..7e74dfd 100644
a
|
b
|
Attributes
|
466 | 466 | |
467 | 467 | The `HTTP Status code`_ for the response. |
468 | 468 | |
| 469 | .. attribute:: HttpResponse.status_text |
| 470 | |
| 471 | The `HTTP Status code`_ text for the response. This is the human readable |
| 472 | string passed with the status code in the status line. If not provided the |
| 473 | standard text corresponding to ``status_code`` is used. |
| 474 | |
469 | 475 | Methods |
470 | 476 | ------- |
471 | 477 | |
… |
… |
Methods
|
478 | 484 | return strings, and those strings will be joined together to form the |
479 | 485 | content of the response. |
480 | 486 | |
481 | | ``status`` is the `HTTP Status code`_ for the response. |
| 487 | ``status`` is the `HTTP Status code`_ for the response. Normally this will |
| 488 | be an integer, but if passed as a string it should be on the form "200 OK" |
| 489 | and this will then intialize both the ``status_code`` and ``status_text`` |
| 490 | attribute of the response. |
482 | 491 | |
483 | 492 | .. versionadded:: 1.0 |
484 | 493 | |
diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
index 132472c..c842602 100644
a
|
b
|
class Cookies(TestCase):
|
503 | 503 | c2.load(c.output()) |
504 | 504 | self.assertEqual(c['test'].value, c2['test'].value) |
505 | 505 | |
| 506 | class Response(TestCase): |
| 507 | |
| 508 | def test_status(self): |
| 509 | """ |
| 510 | Test setting of status code in the constructor call |
| 511 | """ |
| 512 | r = HttpResponse() |
| 513 | self.assertEqual(r.status_code, 200) |
| 514 | self.assertEqual(r.status_text, None) |
| 515 | r = HttpResponse(status=404) |
| 516 | self.assertEqual(r.status_code, 404) |
| 517 | self.assertEqual(r.status_text, None) |
| 518 | r = HttpResponse(status="202 Thanks!") |
| 519 | self.assertEqual(r.status_code, 202) |
| 520 | self.assertEqual(r.status_text, "Thanks!") |
| 521 | |
| 522 | from django.http import BadStatusError |
| 523 | self.assertRaises(ValueError, lambda: HttpResponse(status="OK")) |
| 524 | self.assertRaises(BadStatusError, lambda: HttpResponse(status="OK")) |
| 525 | |
506 | 526 | if __name__ == "__main__": |
507 | 527 | import doctest |
508 | 528 | doctest.testmod() |