Skip to content

Commit e812029

Browse files
committed
Python: Add test for wsgiref.simple_server
1 parent 9f614b1 commit e812029

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This test file demonstrates how to use an application with a wsgiref.simple_server
2+
# see https://docs.python.org/3/library/wsgiref.html#wsgiref.simple_server.WSGIServer
3+
import sys
4+
import wsgiref.simple_server
5+
6+
def ignore(*arg, **kwargs): pass
7+
ensure_tainted = ensure_not_tainted = ignore
8+
9+
ADDRESS = ("localhost", 8000)
10+
11+
12+
# I wanted to showcase that we handle both functions and bound-methods, so it's possible
13+
# to run this test-file in 2 different ways.
14+
15+
def func(environ, start_response): # $ MISSING: requestHandler
16+
ensure_tainted(
17+
environ, # $ MISSING: tainted
18+
environ["PATH_INFO"], # $ MISSING: tainted
19+
)
20+
write = start_response("200 OK", [("Content-Type", "text/plain")])
21+
write(b"hello") # $ MISSING: HttpResponse responseBody=b"hello"
22+
write(data=b" ") # $ MISSING: HttpResponse responseBody=b" "
23+
24+
# function return value should be an iterable that will also be written to to the
25+
# response.
26+
return [b"world", b"!"] # $ MISSING: HttpResponse responseBody=List
27+
28+
29+
class MyServer(wsgiref.simple_server.WSGIServer):
30+
def __init__(self):
31+
super().__init__(ADDRESS, wsgiref.simple_server.WSGIRequestHandler)
32+
self.set_app(self.my_method)
33+
34+
def my_method(self, _env, start_response): # $ MISSING: requestHandler
35+
start_response("200 OK", [])
36+
return [b"my_method"] # $ MISSING: HttpResponse responseBody=List
37+
38+
39+
case = sys.argv[1]
40+
if case == "1":
41+
server = wsgiref.simple_server.WSGIServer(ADDRESS, wsgiref.simple_server.WSGIRequestHandler)
42+
server.set_app(func)
43+
elif case == "2":
44+
server = MyServer()
45+
elif case == "3":
46+
server = MyServer()
47+
def func3(_env, start_response): # $ MISSING: requestHandler
48+
start_response("200 OK", [])
49+
return [b"foo"] # $ MISSING: HttpResponse responseBody=List
50+
server.set_app(func3)
51+
else:
52+
sys.exit("wrong case")
53+
54+
55+
print(f"Running on http://{ADDRESS[0]}:{ADDRESS[1]}")
56+
57+
server.serve_forever()

0 commit comments

Comments
 (0)