summaryrefslogtreecommitdiff
path: root/tests/servers
diff options
context:
space:
mode:
authorFlavio Curella <flavio.curella@gmail.com>2015-11-06 10:19:41 -0600
committerTim Graham <timograham@gmail.com>2016-01-11 07:35:17 -0500
commit0bc5cd628042bf0a44df60a93085a4f991a84dfb (patch)
tree076999e7afc4c14840a44eff6d92b0c0ba215d36 /tests/servers
parent3792e291e6aee9cd43a48fac33f19a6ab24920d2 (diff)
Fixed #25684 -- Made runserver use logging for request/response output.
Thanks andreif for the contributing to the patch.
Diffstat (limited to 'tests/servers')
-rw-r--r--tests/servers/test_basehttp.py53
1 files changed, 40 insertions, 13 deletions
diff --git a/tests/servers/test_basehttp.py b/tests/servers/test_basehttp.py
index 4a3ba3577e..2d57d7c257 100644
--- a/tests/servers/test_basehttp.py
+++ b/tests/servers/test_basehttp.py
@@ -1,10 +1,11 @@
+import logging
from io import BytesIO
from django.core.handlers.wsgi import WSGIRequest
from django.core.servers.basehttp import WSGIRequestHandler
from django.test import SimpleTestCase
from django.test.client import RequestFactory
-from django.test.utils import captured_stderr
+from django.test.utils import captured_stderr, patch_logger
class Stub(object):
@@ -15,13 +16,39 @@ class Stub(object):
class WSGIRequestHandlerTestCase(SimpleTestCase):
def test_log_message(self):
- request = WSGIRequest(RequestFactory().get('/').environ)
- request.makefile = lambda *args, **kwargs: BytesIO()
- handler = WSGIRequestHandler(request, '192.168.0.2', None)
+ # Silence the django.server logger by replacing its StreamHandler with
+ # NullHandler.
+ logger = logging.getLogger('django.server')
+ original_handlers = logger.handlers
+ logger.handlers = [logging.NullHandler()]
+ try:
+ request = WSGIRequest(RequestFactory().get('/').environ)
+ request.makefile = lambda *args, **kwargs: BytesIO()
+ handler = WSGIRequestHandler(request, '192.168.0.2', None)
+ level_status_codes = {
+ 'info': [200, 301, 304],
+ 'warning': [400, 403, 404],
+ 'error': [500, 503],
+ }
- with captured_stderr() as stderr:
- handler.log_message('GET %s %s', 'A', 'B')
- self.assertIn('] GET A B', stderr.getvalue())
+ def _log_level_code(level, status_code):
+ with patch_logger('django.server', level) as messages:
+ handler.log_message('GET %s %s', 'A', str(status_code))
+ return messages
+
+ for level, status_codes in level_status_codes.items():
+ for status_code in status_codes:
+ # The correct level gets the message.
+ messages = _log_level_code(level, status_code)
+ self.assertIn('GET A %d' % status_code, messages[0])
+
+ # Incorrect levels shouldn't have any messages.
+ for wrong_level in level_status_codes.keys():
+ if wrong_level != level:
+ messages = _log_level_code(wrong_level, status_code)
+ self.assertEqual(len(messages), 0)
+ finally:
+ logger.handlers = original_handlers
def test_https(self):
request = WSGIRequest(RequestFactory().get('/').environ)
@@ -29,13 +56,13 @@ class WSGIRequestHandlerTestCase(SimpleTestCase):
handler = WSGIRequestHandler(request, '192.168.0.2', None)
- with captured_stderr() as stderr:
+ with patch_logger('django.server', 'error') as messages:
handler.log_message("GET %s %s", str('\x16\x03'), "4")
- self.assertIn(
- "You're accessing the development server over HTTPS, "
- "but it only supports HTTP.",
- stderr.getvalue()
- )
+ self.assertIn(
+ "You're accessing the development server over HTTPS, "
+ "but it only supports HTTP.",
+ messages[0]
+ )
def test_strips_underscore_headers(self):
"""WSGIRequestHandler ignores headers containing underscores.