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.
11 from http
.server
import BaseHTTPRequestHandler
, HTTPServer
19 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler
):
21 def _notify_test(self
) -> None:
22 raise NotImplementedError()
24 def _response_code(self
) -> int:
25 if self
.path
== "/not_found":
27 if self
.path
== "/server_error":
31 def do_GET(self
) -> None:
34 <div class="post-container post-post">
35 <div class="post-edit-box">We don't want edit boxes</div>
37 <div class="post-footer">We don't want footers</div>
39 <div class="flat-post-replies">
40 <div class="post-container post-reply">
41 <div class="post-edit-box">We don't want edit boxes</div>
43 <div class="post-footer">We don't want footers</div>
45 <div class="post-container post-reply">
51 self
.send_response(self
._response
_code
())
52 self
.send_header("Content-type", "text/html")
53 self
.send_header("Content-Length", str(len(body
)))
55 self
.wfile
.write(body
)
59 class TestFetch(unittest
.TestCase
):
60 def _port(self
) -> int:
61 port
= self
._web
_server
.socket
.getsockname()[1]
62 assert isinstance(port
, int)
65 def _count_request(self
) -> None:
66 self
._request
_counter
+= 1
68 def setUp(self
) -> None:
69 self
._request
_counter
= 0
70 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
71 '_notify_test': lambda _
: self
._count
_request
()})
72 self
._web
_server
= HTTPServer(('', 0), handler
)
73 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
76 def tearDown(self
) -> None:
77 self
._web
_server
.shutdown()
79 self
._web
_server
.server_close()
81 def testFetch(self
) -> None:
82 with requests
.session() as s
:
83 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
84 self
.assertEqual(self
._request
_counter
, 1)
85 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
86 self
.assertEqual(self
._request
_counter
, 2)
88 def testFetchCaching(self
) -> None:
89 with requests_cache
.CachedSession() as s
:
90 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
91 self
.assertEqual(self
._request
_counter
, 1)
92 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
93 self
.assertEqual(self
._request
_counter
, 1)
95 def testFetchPersistentCaching(self
) -> None:
96 with requests_cache
.CachedSession() as s
:
97 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
98 self
.assertEqual(self
._request
_counter
, 1)
99 with requests_cache
.CachedSession() as s
:
100 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
101 self
.assertEqual(self
._request
_counter
, 1)
103 def testReplies(self
) -> None:
104 with requests
.session() as s
:
105 replies
= paperdoorknob
.replies(
108 f
"http://localhost:{self._port()}",
111 self
.assertEqual([r
.text
.strip() for r
in replies
],
112 ["This is glowfic", "You sure?", "Pretty sure."])
114 def testFetchErrors(self
) -> None:
115 with requests
.session() as s
:
116 with self
.assertRaises(requests
.HTTPError
):
118 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
119 with self
.assertRaises(requests
.HTTPError
):
121 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
123 def testProcess(self
) -> None:
124 with requests
.session() as s
:
126 paperdoorknob
.process(
127 f
"http://localhost:{self._port()}",
132 self
.assertEqual(buf
.getvalue(),
133 b
'This is glowfic\nYou sure?\nPretty sure.\n')
136 if __name__
== '__main__':