1 from typing
import Any
, Self
3 from http
.server
import BaseHTTPRequestHandler
, HTTPServer
6 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler
):
8 def _notify_server(self
) -> None:
9 raise NotImplementedError()
11 def _response_code(self
) -> int:
12 if self
.path
== "/not_found":
14 if self
.path
== "/server_error":
18 def do_GET(self
) -> None:
20 with open("testdata/this-is-glowfic.html", "rb") as f
:
22 self
.send_response(self
._response
_code
())
23 self
.send_header("Content-type", "text/html")
24 self
.send_header("Content-Length", str(len(body
)))
26 self
.wfile
.write(body
)
29 class FakeGlowficServer():
30 def __init__(self
) -> None:
31 self
._request
_counter
= 0
32 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
33 '_notify_server': lambda _
: self
._count
_request
()})
34 self
._web
_server
= HTTPServer(('', 0), handler
)
35 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
37 def __enter__(self
) -> Self
:
41 def __exit__(self
, *_
: Any
) -> None:
42 self
._web
_server
.shutdown()
44 self
._web
_server
.server_close()
46 def _count_request(self
) -> None:
47 self
._request
_counter
+= 1
49 def port(self
) -> int:
50 p
= self
._web
_server
.socket
.getsockname()[1]
51 assert isinstance(p
, int)
54 def request_count(self
) -> int:
55 return self
._request
_counter