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
18 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler
):
20 def _notify_test(self
) -> None:
21 raise NotImplementedError()
23 def _response_code(self
) -> int:
24 if self
.path
== "/not_found":
26 if self
.path
== "/server_error":
30 def do_GET(self
) -> None:
31 body
= b
'<html><body>This is glowfic</body></html>'
32 self
.send_response(self
._response
_code
())
33 self
.send_header("Content-type", "text/html")
34 self
.send_header("Content-Length", str(len(body
)))
36 self
.wfile
.write(body
)
40 class TestFetch(unittest
.TestCase
):
41 def _port(self
) -> int:
42 port
= self
._web
_server
.socket
.getsockname()[1]
43 assert isinstance(port
, int)
46 def _count_request(self
) -> None:
47 self
._request
_counter
+= 1
49 def setUp(self
) -> None:
50 self
._request
_counter
= 0
51 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
52 '_notify_test': lambda _
: self
._count
_request
()})
53 self
._web
_server
= HTTPServer(('', 0), handler
)
54 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
57 def tearDown(self
) -> None:
58 self
._web
_server
.shutdown()
60 self
._web
_server
.server_close()
62 def testFetch(self
) -> None:
63 with requests
.session() as s
:
64 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
65 self
.assertEqual(self
._request
_counter
, 1)
66 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
67 self
.assertEqual(self
._request
_counter
, 2)
69 def testFetchCaching(self
) -> None:
70 with requests_cache
.CachedSession() as s
:
71 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
72 self
.assertEqual(self
._request
_counter
, 1)
73 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
74 self
.assertEqual(self
._request
_counter
, 1)
76 def testFetchPersistentCaching(self
) -> None:
77 with requests_cache
.CachedSession() as s
:
78 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
79 self
.assertEqual(self
._request
_counter
, 1)
80 with requests_cache
.CachedSession() as s
:
81 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
82 self
.assertEqual(self
._request
_counter
, 1)
84 def testFetchConents(self
) -> None:
85 with requests
.session() as s
:
86 doc
= paperdoorknob
.fetch(
87 f
"http://localhost:{self._port()}", s
, TIMEOUT
)
90 self
.assertEqual(body
.text
, "This is glowfic")
92 def testFetchErrors(self
) -> None:
93 with requests
.session() as s
:
94 with self
.assertRaises(requests
.HTTPError
):
96 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
97 with self
.assertRaises(requests
.HTTPError
):
99 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
102 if __name__
== '__main__':