2021-05-20 13:35:19 +01:00
|
|
|
import json
|
2022-09-18 15:17:23 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
from pathlib import Path
|
2022-06-19 02:43:12 +01:00
|
|
|
from typing import List, Optional, Tuple
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sanic_routing import __version__ as __routing_version__
|
|
|
|
|
|
|
|
from sanic import __version__
|
2022-09-18 15:17:23 +01:00
|
|
|
from sanic.__main__ import main
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def tty():
|
|
|
|
orig = sys.stdout.isatty
|
|
|
|
sys.stdout.isatty = lambda: False
|
|
|
|
yield
|
|
|
|
sys.stdout.isatty = orig
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def capture(command: List[str], caplog):
|
|
|
|
caplog.clear()
|
|
|
|
os.chdir(Path(__file__).parent)
|
|
|
|
try:
|
|
|
|
main(command)
|
|
|
|
except SystemExit:
|
|
|
|
...
|
|
|
|
return [record.message for record in caplog.records]
|
2021-11-07 19:39:03 +00:00
|
|
|
|
|
|
|
|
2022-06-19 02:43:12 +01:00
|
|
|
def read_app_info(lines: List[str]):
|
2021-12-25 20:20:06 +00:00
|
|
|
for line in lines:
|
2022-09-18 15:17:23 +01:00
|
|
|
if line.startswith("{") and line.endswith("}"): # type: ignore
|
2021-12-25 20:20:06 +00:00
|
|
|
return json.loads(line)
|
|
|
|
|
|
|
|
|
2021-06-09 10:05:56 +01:00
|
|
|
@pytest.mark.parametrize(
|
2022-03-23 10:00:41 +00:00
|
|
|
"appname,extra",
|
2021-06-09 10:05:56 +01:00
|
|
|
(
|
2022-03-23 10:00:41 +00:00
|
|
|
("fake.server.app", None),
|
|
|
|
("fake.server:create_app", "--factory"),
|
|
|
|
("fake.server.create_app()", None),
|
2021-06-18 09:39:09 +01:00
|
|
|
),
|
2021-06-09 10:05:56 +01:00
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_server_run(
|
|
|
|
appname: str,
|
|
|
|
extra: Optional[str],
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
):
|
|
|
|
command = [appname]
|
2022-03-23 10:00:41 +00:00
|
|
|
if extra:
|
|
|
|
command.append(extra)
|
2022-09-18 15:17:23 +01:00
|
|
|
lines = capture(command, caplog)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert "Goin' Fast @ http://127.0.0.1:8000" in lines
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_server_run_factory_with_args(caplog):
|
2022-03-23 10:00:41 +00:00
|
|
|
command = [
|
|
|
|
"fake.server.create_app_with_args",
|
|
|
|
"--factory",
|
|
|
|
]
|
2022-09-18 15:17:23 +01:00
|
|
|
lines = capture(command, caplog)
|
2022-03-23 10:00:41 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert "module=fake.server.create_app_with_args" in lines
|
2022-03-23 10:00:41 +00:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_server_run_factory_with_args_arbitrary(caplog):
|
2022-03-23 10:00:41 +00:00
|
|
|
command = [
|
|
|
|
"fake.server.create_app_with_args",
|
|
|
|
"--factory",
|
|
|
|
"--foo=bar",
|
|
|
|
]
|
2022-09-18 15:17:23 +01:00
|
|
|
lines = capture(command, caplog)
|
2022-03-23 10:00:41 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert "foo=bar" in lines
|
2022-03-23 10:00:41 +00:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_error_with_function_as_instance_without_factory_arg(caplog):
|
|
|
|
command = ["fake.server.create_app"]
|
|
|
|
lines = capture(command, caplog)
|
|
|
|
assert (
|
|
|
|
"Failed to run app: Module is not a Sanic app, it is a function\n "
|
|
|
|
"If this callable returns a Sanic instance try: \n"
|
|
|
|
"sanic fake.server.create_app --factory"
|
|
|
|
) in lines
|
2022-03-23 08:30:41 +00:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_error_with_path_as_instance_without_simple_arg(caplog):
|
|
|
|
command = ["./fake/"]
|
|
|
|
lines = capture(command, caplog)
|
2022-03-23 08:30:41 +00:00
|
|
|
assert (
|
2022-09-18 15:17:23 +01:00
|
|
|
"Failed to run app: App not found.\n Please use --simple if you "
|
|
|
|
"are passing a directory to sanic.\n eg. sanic ./fake/ --simple"
|
|
|
|
) in lines
|
2022-03-23 08:30:41 +00:00
|
|
|
|
|
|
|
|
2021-10-28 14:50:05 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
|
|
|
(
|
|
|
|
"--cert=certs/sanic.example/fullchain.pem",
|
|
|
|
"--key=certs/sanic.example/privkey.pem",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"--tls=certs/sanic.example/",
|
|
|
|
"--tls=certs/localhost/",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"--tls=certs/sanic.example/",
|
|
|
|
"--tls=certs/localhost/",
|
|
|
|
"--tls-strict-host",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_tls_options(cmd: Tuple[str, ...], caplog):
|
2022-12-15 09:49:26 +00:00
|
|
|
command = [
|
|
|
|
"fake.server.app",
|
|
|
|
*cmd,
|
|
|
|
"--port=9999",
|
|
|
|
"--debug",
|
|
|
|
"--single-process",
|
|
|
|
]
|
2022-09-18 15:17:23 +01:00
|
|
|
lines = capture(command, caplog)
|
|
|
|
assert "Goin' Fast @ https://127.0.0.1:9999" in lines
|
2021-10-28 14:50:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
2021-11-07 19:39:03 +00:00
|
|
|
("--cert=certs/sanic.example/fullchain.pem",),
|
2021-10-28 14:50:05 +01:00
|
|
|
(
|
|
|
|
"--cert=certs/sanic.example/fullchain.pem",
|
|
|
|
"--key=certs/sanic.example/privkey.pem",
|
|
|
|
"--tls=certs/localhost/",
|
|
|
|
),
|
2021-11-07 19:39:03 +00:00
|
|
|
("--tls-strict-host",),
|
2021-10-28 14:50:05 +01:00
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_tls_wrong_options(cmd: Tuple[str, ...], caplog):
|
|
|
|
command = ["fake.server.app", *cmd, "-p=9999", "--debug"]
|
|
|
|
lines = capture(command, caplog)
|
2021-11-07 19:39:03 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert (
|
|
|
|
"TLS certificates must be specified by either of:\n "
|
|
|
|
"--cert certdir/fullchain.pem --key certdir/privkey.pem\n "
|
|
|
|
"--tls certdir (equivalent to the above)"
|
|
|
|
) in lines
|
2021-10-28 14:50:05 +01:00
|
|
|
|
|
|
|
|
2021-05-20 13:35:19 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
|
|
|
("--host=localhost", "--port=9999"),
|
|
|
|
("-H", "localhost", "-p", "9999"),
|
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_host_port_localhost(cmd: Tuple[str, ...], caplog):
|
|
|
|
command = ["fake.server.app", *cmd]
|
|
|
|
lines = capture(command, caplog)
|
|
|
|
expected = "Goin' Fast @ http://localhost:9999"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert expected in lines
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-09-18 15:17:23 +01:00
|
|
|
"cmd,expected",
|
2021-10-24 17:14:00 +01:00
|
|
|
(
|
2022-09-18 15:17:23 +01:00
|
|
|
(
|
|
|
|
("--host=localhost", "--port=9999"),
|
|
|
|
"Goin' Fast @ http://localhost:9999",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("-H", "localhost", "-p", "9999"),
|
|
|
|
"Goin' Fast @ http://localhost:9999",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("--host=127.0.0.127", "--port=9999"),
|
|
|
|
"Goin' Fast @ http://127.0.0.127:9999",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("-H", "127.0.0.127", "-p", "9999"),
|
|
|
|
"Goin' Fast @ http://127.0.0.127:9999",
|
|
|
|
),
|
|
|
|
(("--host=::", "--port=9999"), "Goin' Fast @ http://[::]:9999"),
|
|
|
|
(("-H", "::", "-p", "9999"), "Goin' Fast @ http://[::]:9999"),
|
|
|
|
(("--host=::1", "--port=9999"), "Goin' Fast @ http://[::1]:9999"),
|
|
|
|
(("-H", "::1", "-p", "9999"), "Goin' Fast @ http://[::1]:9999"),
|
2021-10-24 17:14:00 +01:00
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_host_port(cmd: Tuple[str, ...], expected: str, caplog):
|
|
|
|
command = ["fake.server.app", *cmd]
|
|
|
|
lines = capture(command, caplog)
|
2021-10-24 17:14:00 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert expected in lines
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
|
2021-05-20 13:35:19 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"num,cmd",
|
|
|
|
(
|
|
|
|
(1, (f"--workers={1}",)),
|
|
|
|
(2, (f"--workers={2}",)),
|
|
|
|
(4, (f"--workers={4}",)),
|
|
|
|
(1, ("-w", "1")),
|
|
|
|
(2, ("-w", "2")),
|
|
|
|
(4, ("-w", "4")),
|
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_num_workers(num: int, cmd: Tuple[str, ...], caplog):
|
|
|
|
command = ["fake.server.app", *cmd]
|
|
|
|
lines = capture(command, caplog)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
if num == 1:
|
2022-09-18 15:17:23 +01:00
|
|
|
expected = "mode: production, single worker"
|
2022-01-16 07:03:04 +00:00
|
|
|
else:
|
2022-09-18 15:17:23 +01:00
|
|
|
expected = f"mode: production, w/ {num} workers"
|
2022-01-16 07:03:04 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert expected in lines
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2022-01-12 14:28:43 +00:00
|
|
|
@pytest.mark.parametrize("cmd", ("--debug",))
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_debug(cmd: str, caplog):
|
|
|
|
command = ["fake.server.app", cmd]
|
|
|
|
lines = capture(command, caplog)
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert info["debug"] is True
|
|
|
|
assert info["auto_reload"] is False
|
2022-01-12 14:28:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--dev", "-d"))
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_dev(cmd: str, caplog):
|
|
|
|
command = ["fake.server.app", cmd]
|
|
|
|
lines = capture(command, caplog)
|
2022-01-12 14:28:43 +00:00
|
|
|
info = read_app_info(lines)
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert info["debug"] is True
|
|
|
|
assert info["auto_reload"] is True
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--auto-reload", "-r"))
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_auto_reload(cmd: str, caplog):
|
|
|
|
command = ["fake.server.app", cmd]
|
|
|
|
lines = capture(command, caplog)
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert info["debug"] is False
|
|
|
|
assert info["auto_reload"] is True
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-09-18 15:17:23 +01:00
|
|
|
"cmd,expected",
|
|
|
|
(
|
|
|
|
("", False),
|
|
|
|
("--debug", True),
|
|
|
|
("--access-log", True),
|
|
|
|
("--no-access-log", False),
|
|
|
|
),
|
2021-05-20 13:35:19 +01:00
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_access_logs(cmd: str, expected: bool, caplog):
|
|
|
|
command = ["fake.server.app"]
|
|
|
|
if cmd:
|
|
|
|
command.append(cmd)
|
|
|
|
lines = capture(command, caplog)
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert info["access_log"] is expected
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--version", "-v"))
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_version(cmd: str, caplog, capsys):
|
|
|
|
command = [cmd]
|
|
|
|
capture(command, caplog)
|
2021-05-20 13:35:19 +01:00
|
|
|
version_string = f"Sanic {__version__}; Routing {__routing_version__}\n"
|
2022-09-18 15:17:23 +01:00
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert version_string == out
|
2021-10-27 08:43:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd,expected",
|
|
|
|
(
|
|
|
|
("--noisy-exceptions", True),
|
|
|
|
("--no-noisy-exceptions", False),
|
|
|
|
),
|
|
|
|
)
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_noisy_exceptions(cmd: str, expected: bool, caplog):
|
|
|
|
command = ["fake.server.app", cmd]
|
|
|
|
lines = capture(command, caplog)
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-10-27 08:43:58 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
assert info["noisy_exceptions"] is expected
|