1 # paperdoorknob: Print glowfic
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
10 from http
.server
import BaseHTTPRequestHandler
, HTTPServer
17 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler
):
19 def _response_code(self
) -> int:
20 if self
.path
== "/not_found":
22 if self
.path
== "/server_error":
26 def do_GET(self
) -> None:
27 body
= b
'<html><body>This is glowfic</body></html>'
28 self
.send_response(self
._response
_code
())
29 self
.send_header("Content-type", "text/html")
30 self
.send_header("Content-Length", str(len(body
)))
32 self
.wfile
.write(body
)
35 class TestFetch(unittest
.TestCase
):
36 def setUp(self
) -> None:
37 web_server
= HTTPServer(('', 0), FakeGlowficHTTPRequestHandler
)
38 self
._port
= web_server
.socket
.getsockname()[1]
39 self
._thread
= threading
.Thread(target
=web_server
.serve_forever
)
41 self
._stop
_server
= web_server
.shutdown
43 def tearDown(self
) -> None:
47 def testFetch(self
) -> None:
48 paperdoorknob
.fetch(f
"http://localhost:{self._port}", TIMEOUT
)
50 def testFetchErrors(self
) -> None:
51 with self
.assertRaises(requests
.HTTPError
):
53 f
"http://localhost:{self._port}/not_found", TIMEOUT
)
54 with self
.assertRaises(requests
.HTTPError
):
56 f
"http://localhost:{self._port}/server_error", TIMEOUT
)
59 if __name__
== '__main__':