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:
21 <div class="post-container post-post">
22 <div class="post-edit-box">We don't want edit boxes</div>
24 <div class="post-footer">We don't want footers</div>
26 <div class="flat-post-replies">
27 <div class="post-container post-reply">
28 <div class="post-edit-box">We don't want edit boxes</div>
30 <div class="post-footer">We don't want footers</div>
32 <div class="post-container post-reply">
38 self
.send_response(self
._response
_code
())
39 self
.send_header("Content-type", "text/html")
40 self
.send_header("Content-Length", str(len(body
)))
42 self
.wfile
.write(body
)
46 class FakeGlowficServer():
47 def __init__(self
) -> None:
48 self
._request
_counter
= 0
49 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
50 '_notify_server': lambda _
: self
._count
_request
()})
51 self
._web
_server
= HTTPServer(('', 0), handler
)
52 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
54 def __enter__(self
) -> Self
:
58 def __exit__(self
, *_
: Any
) -> None:
59 self
._web
_server
.shutdown()
61 self
._web
_server
.server_close()
63 def _count_request(self
) -> None:
64 self
._request
_counter
+= 1
66 def port(self
) -> int:
67 p
= self
._web
_server
.socket
.getsockname()[1]
68 assert isinstance(p
, int)
71 def request_count(self
) -> int:
72 return self
._request
_counter