]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Fetch URLs
[paperdoorknob] / paperdoorknob_test.py
1 # paperdoorknob: Print glowfic
2 #
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.
6
7
8 import unittest
9 import threading
10 from http.server import BaseHTTPRequestHandler, HTTPServer
11 import paperdoorknob
12
13 TEST_PORT = 8080
14 TIMEOUT = 8
15
16
17 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
18
19 def do_GET(self) -> None:
20 body = b'<html><body>This is glowfic</body></html>'
21 self.send_response(200)
22 self.send_header("Content-type", "text/html")
23 self.send_header("Content-Length", str(len(body)))
24 self.end_headers()
25 self.wfile.write(body)
26
27
28 class TestFetch(unittest.TestCase):
29 def setUp(self) -> None:
30 web_server = HTTPServer(('', TEST_PORT), FakeGlowficHTTPRequestHandler)
31 threading.Thread(target=web_server.serve_forever).start()
32 self._stop_server = web_server.shutdown
33
34 def tearDown(self) -> None:
35 self._stop_server()
36
37 def testFetch(self) -> None:
38 paperdoorknob.fetch(f"http://localhost:{TEST_PORT}", TIMEOUT)
39
40
41 if __name__ == '__main__':
42 unittest.main()