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:
33 <div class="post-container post-post">
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 TestFetch(unittest
.TestCase
):
47 def _port(self
) -> int:
48 port
= self
._web
_server
.socket
.getsockname()[1]
49 assert isinstance(port
, int)
52 def _count_request(self
) -> None:
53 self
._request
_counter
+= 1
55 def setUp(self
) -> None:
56 self
._request
_counter
= 0
57 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
58 '_notify_test': lambda _
: self
._count
_request
()})
59 self
._web
_server
= HTTPServer(('', 0), handler
)
60 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
63 def tearDown(self
) -> None:
64 self
._web
_server
.shutdown()
66 self
._web
_server
.server_close()
68 def testFetch(self
) -> None:
69 with requests
.session() as s
:
70 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
71 self
.assertEqual(self
._request
_counter
, 1)
72 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
73 self
.assertEqual(self
._request
_counter
, 2)
75 def testFetchCaching(self
) -> None:
76 with requests_cache
.CachedSession() as s
:
77 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
78 self
.assertEqual(self
._request
_counter
, 1)
79 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
80 self
.assertEqual(self
._request
_counter
, 1)
82 def testFetchPersistentCaching(self
) -> None:
83 with requests_cache
.CachedSession() as s
:
84 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
85 self
.assertEqual(self
._request
_counter
, 1)
86 with requests_cache
.CachedSession() as s
:
87 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
88 self
.assertEqual(self
._request
_counter
, 1)
90 def testFetchConents(self
) -> None:
91 with requests
.session() as s
:
92 post
= paperdoorknob
.Post(paperdoorknob
.fetch(
93 f
"http://localhost:{self._port()}", s
, TIMEOUT
))
94 self
.assertEqual(post
.text().text
.strip(), "This is glowfic")
96 def testFetchErrors(self
) -> None:
97 with requests
.session() as s
:
98 with self
.assertRaises(requests
.HTTPError
):
100 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
101 with self
.assertRaises(requests
.HTTPError
):
103 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
106 if __name__
== '__main__':