Skip to content

Commit

Permalink
Fix test_cli and test_cookies (#2479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihweiLHBird committed Jun 19, 2022
1 parent ce926a3 commit d1c5e80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
37 changes: 19 additions & 18 deletions tests/test_cli.py
Expand Up @@ -2,6 +2,7 @@
import subprocess

from pathlib import Path
from typing import List, Optional, Tuple

import pytest

Expand All @@ -10,29 +11,29 @@
from sanic import __version__


def capture(command):
def capture(command: List[str]):
proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=Path(__file__).parent,
)
try:
out, err = proc.communicate(timeout=1)
out, err = proc.communicate(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
out, err = proc.communicate()
return out, err, proc.returncode


def starting_line(lines):
def starting_line(lines: List[str]):
for idx, line in enumerate(lines):
if line.strip().startswith(b"Sanic v"):
return idx
return 0


def read_app_info(lines):
def read_app_info(lines: List[str]):
for line in lines:
if line.startswith(b"{") and line.endswith(b"}"):
return json.loads(line)
Expand All @@ -46,7 +47,7 @@ def read_app_info(lines):
("fake.server.create_app()", None),
),
)
def test_server_run(appname, extra):
def test_server_run(appname: str, extra: Optional[str]):
command = ["sanic", appname]
if extra:
command.append(extra)
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_error_with_path_as_instance_without_simple_arg():
),
),
)
def test_tls_options(cmd):
def test_tls_options(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd, "-p=9999", "--debug"]
out, err, exitcode = capture(command)
assert exitcode != 1
Expand All @@ -140,7 +141,7 @@ def test_tls_options(cmd):
("--tls-strict-host",),
),
)
def test_tls_wrong_options(cmd):
def test_tls_wrong_options(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd, "-p=9999", "--debug"]
out, err, exitcode = capture(command)
assert exitcode == 1
Expand All @@ -158,7 +159,7 @@ def test_tls_wrong_options(cmd):
("-H", "localhost", "-p", "9999"),
),
)
def test_host_port_localhost(cmd):
def test_host_port_localhost(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -175,7 +176,7 @@ def test_host_port_localhost(cmd):
("-H", "127.0.0.127", "-p", "9999"),
),
)
def test_host_port_ipv4(cmd):
def test_host_port_ipv4(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -192,7 +193,7 @@ def test_host_port_ipv4(cmd):
("-H", "::", "-p", "9999"),
),
)
def test_host_port_ipv6_any(cmd):
def test_host_port_ipv6_any(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -209,7 +210,7 @@ def test_host_port_ipv6_any(cmd):
("-H", "::1", "-p", "9999"),
),
)
def test_host_port_ipv6_loopback(cmd):
def test_host_port_ipv6_loopback(cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -230,7 +231,7 @@ def test_host_port_ipv6_loopback(cmd):
(4, ("-w", "4")),
),
)
def test_num_workers(num, cmd):
def test_num_workers(num: int, cmd: Tuple[str]):
command = ["sanic", "fake.server.app", *cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -245,7 +246,7 @@ def test_num_workers(num, cmd):


@pytest.mark.parametrize("cmd", ("--debug",))
def test_debug(cmd):
def test_debug(cmd: str):
command = ["sanic", "fake.server.app", cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -259,7 +260,7 @@ def test_debug(cmd):


@pytest.mark.parametrize("cmd", ("--dev", "-d"))
def test_dev(cmd):
def test_dev(cmd: str):
command = ["sanic", "fake.server.app", cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -272,7 +273,7 @@ def test_dev(cmd):


@pytest.mark.parametrize("cmd", ("--auto-reload", "-r"))
def test_auto_reload(cmd):
def test_auto_reload(cmd: str):
command = ["sanic", "fake.server.app", cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -288,7 +289,7 @@ def test_auto_reload(cmd):
@pytest.mark.parametrize(
"cmd,expected", (("--access-log", True), ("--no-access-log", False))
)
def test_access_logs(cmd, expected):
def test_access_logs(cmd: str, expected: bool):
command = ["sanic", "fake.server.app", cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand All @@ -300,7 +301,7 @@ def test_access_logs(cmd, expected):


@pytest.mark.parametrize("cmd", ("--version", "-v"))
def test_version(cmd):
def test_version(cmd: str):
command = ["sanic", cmd]
out, err, exitcode = capture(command)
version_string = f"Sanic {__version__}; Routing {__routing_version__}\n"
Expand All @@ -315,7 +316,7 @@ def test_version(cmd):
("--no-noisy-exceptions", False),
),
)
def test_noisy_exceptions(cmd, expected):
def test_noisy_exceptions(cmd: str, expected: bool):
command = ["sanic", "fake.server.app", cmd]
out, err, exitcode = capture(command)
lines = out.split(b"\n")
Expand Down
14 changes: 7 additions & 7 deletions tests/test_cookies.py
Expand Up @@ -3,6 +3,7 @@

import pytest

from sanic import Sanic
from sanic.cookies import Cookie
from sanic.response import text

Expand Down Expand Up @@ -221,30 +222,29 @@ def handler(request):
assert response.status == 500


@pytest.mark.parametrize(
"expires", [datetime.utcnow() + timedelta(seconds=60)]
)
def test_cookie_expires(app, expires):
expires = expires.replace(microsecond=0)
@pytest.mark.parametrize("expires", [timedelta(seconds=60)])
def test_cookie_expires(app: Sanic, expires: timedelta):
expires_time = datetime.utcnow().replace(microsecond=0) + expires
cookies = {"test": "wait"}

@app.get("/")
def handler(request):
response = text("pass")
response.cookies["test"] = "pass"
response.cookies["test"]["expires"] = expires
response.cookies["test"]["expires"] = expires_time
return response

request, response = app.test_client.get(
"/", cookies=cookies, raw_cookies=True
)

cookie_expires = datetime.utcfromtimestamp(
response.raw_cookies["test"].expires
).replace(microsecond=0)

assert response.status == 200
assert response.cookies["test"] == "pass"
assert cookie_expires == expires
assert cookie_expires == expires_time


@pytest.mark.parametrize("expires", ["Fri, 21-Dec-2018 15:30:00 GMT"])
Expand Down

0 comments on commit d1c5e80

Please sign in to comment.