2021-05-20 13:35:19 +01:00
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
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-06-19 02:43:12 +01:00
|
|
|
def capture(command: List[str]):
|
2021-05-20 13:35:19 +01:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
command,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
cwd=Path(__file__).parent,
|
|
|
|
)
|
|
|
|
try:
|
2022-06-19 02:43:12 +01:00
|
|
|
out, err = proc.communicate(timeout=10)
|
2021-05-20 13:35:19 +01:00
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
proc.kill()
|
|
|
|
out, err = proc.communicate()
|
|
|
|
return out, err, proc.returncode
|
|
|
|
|
|
|
|
|
2022-06-19 02:43:12 +01:00
|
|
|
def starting_line(lines: List[str]):
|
2021-11-07 19:39:03 +00:00
|
|
|
for idx, line in enumerate(lines):
|
|
|
|
if line.strip().startswith(b"Sanic v"):
|
|
|
|
return idx
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
if line.startswith(b"{") and line.endswith(b"}"):
|
|
|
|
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-06-19 02:43:12 +01:00
|
|
|
def test_server_run(appname: str, extra: Optional[str]):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", appname]
|
2022-03-23 10:00:41 +00:00
|
|
|
if extra:
|
|
|
|
command.append(extra)
|
2021-05-20 13:35:19 +01:00
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2021-11-07 19:39:03 +00:00
|
|
|
firstline = lines[starting_line(lines) + 1]
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
assert exitcode != 1
|
|
|
|
assert firstline == b"Goin' Fast @ http://127.0.0.1:8000"
|
|
|
|
|
|
|
|
|
2022-03-23 10:00:41 +00:00
|
|
|
def test_server_run_factory_with_args():
|
|
|
|
command = [
|
|
|
|
"sanic",
|
|
|
|
"fake.server.create_app_with_args",
|
|
|
|
"--factory",
|
|
|
|
]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
|
|
|
|
|
|
|
assert exitcode != 1, lines
|
|
|
|
assert b"module=fake.server.create_app_with_args" in lines
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_run_factory_with_args_arbitrary():
|
|
|
|
command = [
|
|
|
|
"sanic",
|
|
|
|
"fake.server.create_app_with_args",
|
|
|
|
"--factory",
|
|
|
|
"--foo=bar",
|
|
|
|
]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
|
|
|
|
|
|
|
assert exitcode != 1, lines
|
|
|
|
assert b"foo=bar" in lines
|
|
|
|
|
|
|
|
|
2022-03-23 08:30:41 +00:00
|
|
|
def test_error_with_function_as_instance_without_factory_arg():
|
2022-03-23 10:00:41 +00:00
|
|
|
command = ["sanic", "fake.server.create_app"]
|
2022-03-23 08:30:41 +00:00
|
|
|
out, err, exitcode = capture(command)
|
2022-03-23 10:00:41 +00:00
|
|
|
assert b"try: \nsanic fake.server.create_app --factory" in err
|
2022-03-23 08:30:41 +00:00
|
|
|
assert exitcode != 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_error_with_path_as_instance_without_simple_arg():
|
|
|
|
command = ["sanic", "./fake/"]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
assert (
|
|
|
|
b"Please use --simple if you are passing a directory to sanic." in err
|
|
|
|
)
|
|
|
|
assert exitcode != 1
|
|
|
|
|
|
|
|
|
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-13 06:53:48 +01:00
|
|
|
def test_tls_options(cmd: Tuple[str, ...]):
|
2021-10-28 14:50:05 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd, "-p=9999", "--debug"]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
assert exitcode != 1
|
|
|
|
lines = out.split(b"\n")
|
2021-11-07 19:39:03 +00:00
|
|
|
firstline = lines[starting_line(lines) + 1]
|
2021-10-28 14:50:05 +01:00
|
|
|
assert firstline == b"Goin' Fast @ https://127.0.0.1:9999"
|
|
|
|
|
|
|
|
|
|
|
|
@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-13 06:53:48 +01:00
|
|
|
def test_tls_wrong_options(cmd: Tuple[str, ...]):
|
2021-10-28 14:50:05 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd, "-p=9999", "--debug"]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
assert exitcode == 1
|
|
|
|
assert not out
|
2021-11-07 19:39:03 +00:00
|
|
|
lines = err.decode().split("\n")
|
|
|
|
|
2022-06-27 09:19:26 +01:00
|
|
|
assert "TLS certificates must be specified by either of:" 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-13 06:53:48 +01:00
|
|
|
def test_host_port_localhost(cmd: Tuple[str, ...]):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2022-01-16 07:03:04 +00:00
|
|
|
expected = b"Goin' Fast @ http://localhost:9999"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
assert exitcode != 1
|
2022-01-16 07:03:04 +00:00
|
|
|
assert expected in lines, f"Lines found: {lines}\nErr output: {err}"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2021-10-24 17:14:00 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
|
|
|
("--host=127.0.0.127", "--port=9999"),
|
|
|
|
("-H", "127.0.0.127", "-p", "9999"),
|
|
|
|
),
|
|
|
|
)
|
2022-09-13 06:53:48 +01:00
|
|
|
def test_host_port_ipv4(cmd: Tuple[str, ...]):
|
2021-10-24 17:14:00 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2022-01-16 07:03:04 +00:00
|
|
|
expected = b"Goin' Fast @ http://127.0.0.127:9999"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
assert exitcode != 1
|
2022-01-16 07:03:04 +00:00
|
|
|
assert expected in lines, f"Lines found: {lines}\nErr output: {err}"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
|
|
|
("--host=::", "--port=9999"),
|
|
|
|
("-H", "::", "-p", "9999"),
|
|
|
|
),
|
|
|
|
)
|
2022-09-13 06:53:48 +01:00
|
|
|
def test_host_port_ipv6_any(cmd: Tuple[str, ...]):
|
2021-10-24 17:14:00 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2022-01-16 07:03:04 +00:00
|
|
|
expected = b"Goin' Fast @ http://[::]:9999"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
assert exitcode != 1
|
2022-01-16 07:03:04 +00:00
|
|
|
assert expected in lines, f"Lines found: {lines}\nErr output: {err}"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd",
|
|
|
|
(
|
|
|
|
("--host=::1", "--port=9999"),
|
|
|
|
("-H", "::1", "-p", "9999"),
|
|
|
|
),
|
|
|
|
)
|
2022-09-13 06:53:48 +01:00
|
|
|
def test_host_port_ipv6_loopback(cmd: Tuple[str, ...]):
|
2021-10-24 17:14:00 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2022-01-16 07:03:04 +00:00
|
|
|
expected = b"Goin' Fast @ http://[::1]:9999"
|
2021-10-24 17:14:00 +01:00
|
|
|
|
|
|
|
assert exitcode != 1
|
2022-01-16 07:03:04 +00:00
|
|
|
assert expected in lines, f"Lines found: {lines}\nErr output: {err}"
|
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-13 06:53:48 +01:00
|
|
|
def test_num_workers(num: int, cmd: Tuple[str, ...]):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", "fake.server.app", *cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
if num == 1:
|
|
|
|
expected = b"mode: production, single worker"
|
|
|
|
else:
|
|
|
|
expected = (f"mode: production, w/ {num} workers").encode()
|
|
|
|
|
2021-05-20 13:35:19 +01:00
|
|
|
assert exitcode != 1
|
2022-01-16 07:03:04 +00:00
|
|
|
assert expected in lines, f"Expected {expected}\nLines found: {lines}"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
2022-01-12 14:28:43 +00:00
|
|
|
@pytest.mark.parametrize("cmd", ("--debug",))
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_debug(cmd: str):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", "fake.server.app", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert info["debug"] is True, f"Lines found: {lines}\nErr output: {err}"
|
|
|
|
assert (
|
|
|
|
info["auto_reload"] is False
|
|
|
|
), f"Lines found: {lines}\nErr output: {err}"
|
|
|
|
assert "dev" not in info, f"Lines found: {lines}\nErr output: {err}"
|
2022-01-12 14:28:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--dev", "-d"))
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_dev(cmd: str):
|
2022-01-12 14:28:43 +00:00
|
|
|
command = ["sanic", "fake.server.app", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
|
|
|
info = read_app_info(lines)
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert info["debug"] is True, f"Lines found: {lines}\nErr output: {err}"
|
|
|
|
assert (
|
|
|
|
info["auto_reload"] is True
|
|
|
|
), f"Lines found: {lines}\nErr output: {err}"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--auto-reload", "-r"))
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_auto_reload(cmd: str):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", "fake.server.app", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert info["debug"] is False, f"Lines found: {lines}\nErr output: {err}"
|
|
|
|
assert (
|
|
|
|
info["auto_reload"] is True
|
|
|
|
), f"Lines found: {lines}\nErr output: {err}"
|
|
|
|
assert "dev" not in info, f"Lines found: {lines}\nErr output: {err}"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd,expected", (("--access-log", True), ("--no-access-log", False))
|
|
|
|
)
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_access_logs(cmd: str, expected: bool):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", "fake.server.app", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert (
|
|
|
|
info["access_log"] is expected
|
|
|
|
), f"Lines found: {lines}\nErr output: {err}"
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cmd", ("--version", "-v"))
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_version(cmd: str):
|
2021-05-20 13:35:19 +01:00
|
|
|
command = ["sanic", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
version_string = f"Sanic {__version__}; Routing {__routing_version__}\n"
|
|
|
|
|
|
|
|
assert out == version_string.encode("utf-8")
|
2021-10-27 08:43:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cmd,expected",
|
|
|
|
(
|
|
|
|
("--noisy-exceptions", True),
|
|
|
|
("--no-noisy-exceptions", False),
|
|
|
|
),
|
|
|
|
)
|
2022-06-19 02:43:12 +01:00
|
|
|
def test_noisy_exceptions(cmd: str, expected: bool):
|
2021-10-27 08:43:58 +01:00
|
|
|
command = ["sanic", "fake.server.app", cmd]
|
|
|
|
out, err, exitcode = capture(command)
|
|
|
|
lines = out.split(b"\n")
|
2021-12-25 20:20:06 +00:00
|
|
|
info = read_app_info(lines)
|
2021-10-27 08:43:58 +01:00
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert (
|
|
|
|
info["noisy_exceptions"] is expected
|
|
|
|
), f"Lines found: {lines}\nErr output: {err}"
|