def test_log_action(self, mocked_stderr):
        cmd = runserver.Command()
        test_actions = [
            (100, 'http', 'complete',
             'HTTP GET /a-path/ 100 [0.12, a-client]'),
            (200, 'http', 'complete',
             'HTTP GET /a-path/ 200 [0.12, a-client]'),
            (300, 'http', 'complete',
             'HTTP GET /a-path/ 300 [0.12, a-client]'),
            (304, 'http', 'complete',
             'HTTP GET /a-path/ 304 [0.12, a-client]'),
            (400, 'http', 'complete',
             'HTTP GET /a-path/ 400 [0.12, a-client]'),
            (404, 'http', 'complete',
             'HTTP GET /a-path/ 404 [0.12, a-client]'),
            (500, 'http', 'complete',
             'HTTP GET /a-path/ 500 [0.12, a-client]'),
            (None, 'websocket', 'connected',
             'WebSocket CONNECT /a-path/ [a-client]'),
            (None, 'websocket', 'disconnected',
             'WebSocket DISCONNECT /a-path/ [a-client]'),
            (None, 'websocket', 'something', ''),  # This shouldn't happen
        ]

        for status_code, protocol, action, output in test_actions:
            details = {'status': status_code,
                       'method': 'GET',
                       'path': '/a-path/',
                       'time_taken': 0.12345,
                       'client': 'a-client'}
            cmd.log_action(protocol, action, details)
            self.assertIn(output, mocked_stderr.getvalue())
            # Clear previous output
            mocked_stderr.truncate(0)
Esempio n. 2
0
def server(bind='127.0.0.1',
           port=8000,
           migrate_cmd=False,
           verbose=1):  # pragma: no cover
    try:
        from channels.log import setup_logger
        from channels.management.commands import runserver
        logger = setup_logger('django.channels', 1)
        use_channels = True
    except ImportError:
        from django.contrib.staticfiles.management.commands import runserver
        use_channels = False
        logger = None

    if os.environ.get('RUN_MAIN') != 'true':
        _create_db(migrate_cmd)
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = create_user('admin',
                              '*****@*****.**',
                              'admin',
                              is_staff=True,
                              is_superuser=True)
            print('')
            print('A admin user (username: %s, password: admin) '
                  'has been created.' % usr.get_username())
            print('')
    rs = runserver.Command()
    try:
        from django.core.management.base import OutputWrapper
        rs.stdout = OutputWrapper(sys.stdout)
        rs.stderr = OutputWrapper(sys.stderr)
    except ImportError:
        rs.stdout = sys.stdout
        rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    if logger:
        rs.logger = logger
    if use_channels:
        rs.http_timeout = 60
        rs.websocket_handshake_timeout = 5
    autoreload.main(
        rs.inner_run, (), {
            'addrport': '%s:%s' % (bind, port),
            'insecure_serving': True,
            'use_static_handler': True,
            'use_threading': True,
            'verbosity': verbose,
            'use_reloader': True
        })
Esempio n. 3
0
def server(bind="127.0.0.1",
           port=8000,
           migrate_cmd=False,
           verbose=1):  # pragma: no cover
    try:
        from channels.log import setup_logger
        from channels.management.commands import runserver

        logger = setup_logger("django.channels", 1)
        use_channels = True
    except ImportError:
        from django.contrib.staticfiles.management.commands import runserver

        use_channels = False
        logger = None

    if os.environ.get("RUN_MAIN") != "true":
        _create_db(migrate_cmd)
        User = get_user_model()  # NOQA
        if not User.objects.filter(is_superuser=True).exists():
            usr = create_user("admin",
                              "*****@*****.**",
                              "admin",
                              is_staff=True,
                              is_superuser=True)
            print("")
            print("A admin user (username: %s, password: admin) "
                  "has been created." % usr.get_username())
            print("")
    rs = runserver.Command()
    try:
        from django.core.management.base import OutputWrapper

        rs.stdout = OutputWrapper(sys.stdout)
        rs.stderr = OutputWrapper(sys.stderr)
    except ImportError:
        rs.stdout = sys.stdout
        rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    if logger:
        rs.logger = logger
    if use_channels:
        rs.http_timeout = 60
        rs.websocket_handshake_timeout = 5
    try:
        autoreload.run_with_reloader(
            rs.inner_run, **{
                "addrport": "%s:%s" % (bind, port),
                "insecure_serving": True,
                "use_static_handler": True,
                "use_threading": True,
                "verbosity": verbose,
                "use_reloader": True,
            })
    except AttributeError:
        autoreload.main(rs.inner_run,
                        kwargs={
                            "addrport": "%s:%s" % (bind, port),
                            "insecure_serving": True,
                            "use_static_handler": True,
                            "use_threading": True,
                            "verbosity": verbose,
                            "use_reloader": True,
                        })