]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
fetch: test: Choose port dynamically
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
b25a2f90
SW
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
8import unittest
9import threading
10from http.server import BaseHTTPRequestHandler, HTTPServer
11import paperdoorknob
12
b25a2f90
SW
13TIMEOUT = 8
14
15
16class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
17
18 def do_GET(self) -> None:
19 body = b'<html><body>This is glowfic</body></html>'
20 self.send_response(200)
21 self.send_header("Content-type", "text/html")
22 self.send_header("Content-Length", str(len(body)))
23 self.end_headers()
24 self.wfile.write(body)
25
26
27class TestFetch(unittest.TestCase):
28 def setUp(self) -> None:
680f7d10
SW
29 web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler)
30 self._port = web_server.socket.getsockname()[1]
b25a2f90
SW
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:
680f7d10 38 paperdoorknob.fetch(f"http://localhost:{self._port}", TIMEOUT)
b25a2f90
SW
39
40
41if __name__ == '__main__':
42 unittest.main()