Exemplo n.º 1
0
    def test_log_message(self):
        # 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],
            }

            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
Exemplo n.º 2
0
    def test_log_message(self):
        # 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],
            }

            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:
                        if wrong_level != level:
                            messages = _log_level_code(wrong_level, status_code)
                            self.assertEqual(len(messages), 0)
        finally:
            logger.handlers = original_handlers
    def test_log_message(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()
        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with captured_stderr() as stderr:
            handler.log_message('GET %s %s', 'A', 'B')
        self.assertIn('] GET A B', stderr.getvalue())
Exemplo n.º 4
0
    def test_log_message(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()
        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with captured_stderr() as stderr:
            handler.log_message('GET %s %s', 'A', 'B')
        self.assertIn('] GET A B', stderr.getvalue())
Exemplo n.º 5
0
    def test_https(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with captured_stderr() as stderr:
            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())
Exemplo n.º 6
0
    def test_https(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        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.", messages[0])
Exemplo n.º 7
0
    def test_https(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with self.assertLogs('django.server', 'ERROR') as cm:
            handler.log_message("GET %s %s", '\x16\x03', "4")
        self.assertIn(
            "You're accessing the development server over HTTPS, "
            "but it only supports HTTP.", cm.records[0].getMessage())
Exemplo n.º 8
0
    def test_https(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with captured_stderr() as stderr:
            handler.log_message("GET %s %s", str('\x16\x03'), "4")
            self.assertIn(
                "You're accessing the developement server over HTTPS, "
                "but it only supports HTTP.",
                stderr.getvalue()
            )
Exemplo n.º 9
0
    def test_https(self):
        request = WSGIRequest(RequestFactory().get('/').environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, '192.168.0.2', None)

        with patch_logger('django.server', 'error') as messages:
            handler.log_message("GET %s %s", '\x16\x03', "4")
        self.assertIn(
            "You're accessing the development server over HTTPS, "
            "but it only supports HTTP.",
            messages[0]
        )
Exemplo n.º 10
0
    def test_https(self):
        request = WSGIRequest(self.request_factory.get("/").environ)
        request.makefile = lambda *args, **kwargs: BytesIO()

        handler = WSGIRequestHandler(request, "192.168.0.2", None)

        with self.assertLogs("django.server", "ERROR") as cm:
            handler.log_message("GET %s %s", "\x16\x03", "4")
        self.assertEqual(
            "You're accessing the development server over HTTPS, "
            "but it only supports HTTP.",
            cm.records[0].getMessage(),
        )
Exemplo n.º 11
0
 def test_log_message(self):
     request = WSGIRequest(self.request_factory.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],
     }
     for level, status_codes in level_status_codes.items():
         for status_code in status_codes:
             # The correct level gets the message.
             with self.assertLogs('django.server', level.upper()) as cm:
                 handler.log_message('GET %s %s', 'A', str(status_code))
             self.assertIn('GET A %d' % status_code, cm.output[0])
             # Incorrect levels don't have any messages.
             for wrong_level in level_status_codes:
                 if wrong_level != level:
                     with self.assertLogs('django.server', 'INFO') as cm:
                         handler.log_message('GET %s %s', 'A', str(status_code))
                     self.assertNotEqual(cm.records[0].levelname, wrong_level.upper())
Exemplo n.º 12
0
 def test_log_message(self):
     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],
     }
     for level, status_codes in level_status_codes.items():
         for status_code in status_codes:
             # The correct level gets the message.
             with self.assertLogs('django.server', level.upper()) as cm:
                 handler.log_message('GET %s %s', 'A', str(status_code))
             self.assertIn('GET A %d' % status_code, cm.output[0])
             # Incorrect levels don't have any messages.
             for wrong_level in level_status_codes:
                 if wrong_level != level:
                     with self.assertLogs('django.server', 'INFO') as cm:
                         handler.log_message('GET %s %s', 'A', str(status_code))
                     self.assertNotEqual(cm.records[0].levelname, wrong_level.upper())
Exemplo n.º 13
0
 def test_log_message(self):
     request = WSGIRequest(self.request_factory.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],
     }
     for level, status_codes in level_status_codes.items():
         for status_code in status_codes:
             # The correct level gets the message.
             with self.assertLogs("django.server", level.upper()) as cm:
                 handler.log_message("GET %s %s", "A", str(status_code))
             self.assertIn("GET A %d" % status_code, cm.output[0])
             # Incorrect levels don't have any messages.
             for wrong_level in level_status_codes:
                 if wrong_level != level:
                     with self.assertLogs("django.server", "INFO") as cm:
                         handler.log_message("GET %s %s", "A",
                                             str(status_code))
                     self.assertNotEqual(cm.records[0].levelname,
                                         wrong_level.upper())