-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
80 lines (66 loc) · 1.85 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import click as cli
import myhttp
DEFAULT_LOCAL_PORT = 8080
FULL_PRINT: bool = True
def print_result(value) -> None:
if FULL_PRINT:
print(value)
else:
print(f"String result of {len(value)} bytes")
def external() -> None:
"""Tests external urls"""
host = "info.cern.ch"
url = "/hypertext/WWW/TheProject.html"
print_result(myhttp.get(host, url, None))
def internal(*, port: int) -> None:
"""Tests localhost"""
host = f"localhost:{port}"
methods = {
"get": myhttp.get,
"head": myhttp.head,
"post": myhttp.post,
}
test_routes = [
("get", "/"),
("head", "/"),
("post", "/"),
("get", "/html"),
("head", "/html"),
("post", "/text"),
]
for method, url in test_routes:
print(f"{method.upper()} at '{url}':")
print_result(methods[method](host, url, None))
headers = {
"Request": str(True).lower(),
"Hello": "World",
"Custom-Header-1": "Custom-Header-1-Value",
"Custom-Header-2": "Custom-Header-2-Value",
}
response = myhttp.post(host, "/request-header", None, headers)
print_result(response)
@cli.command()
@cli.option("--no-external", is_flag=True, default=False, help="Exclude external links")
@cli.option(
"--no-internal",
is_flag=True,
default=False,
help="Exclude internal (local) links",
)
@cli.option(
"--port",
type=int,
default=DEFAULT_LOCAL_PORT,
show_default=True,
help="Localhost port",
)
@cli.option("--full-print", is_flag=True, default=False, help="Prints full response")
def main(no_external: bool, no_internal: bool, port: int, full_print: bool) -> None:
global FULL_PRINT
FULL_PRINT = full_print
if not no_external:
external()
if not no_internal:
internal(port=port)
if __name__ == "__main__":
main()