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">
34 <div class="post-edit-box">We don't want edit boxes</div>
36 <div class="post-footer">We don't want footers</div>
38 <div class="flat-post-replies">
39 <div class="post-container post-reply">
40 <div class="post-edit-box">We don't want edit boxes</div>
42 <div class="post-footer">We don't want footers</div>
44 <div class="post-container post-reply">
50 self
.send_response(self
._response
_code
())
51 self
.send_header("Content-type", "text/html")
52 self
.send_header("Content-Length", str(len(body
)))
54 self
.wfile
.write(body
)
58 class TestFetch(unittest
.TestCase
):
59 def _port(self
) -> int:
60 port
= self
._web
_server
.socket
.getsockname()[1]
61 assert isinstance(port
, int)
64 def _count_request(self
) -> None:
65 self
._request
_counter
+= 1
67 def setUp(self
) -> None:
68 self
._request
_counter
= 0
69 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
70 '_notify_test': lambda _
: self
._count
_request
()})
71 self
._web
_server
= HTTPServer(('', 0), handler
)
72 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
75 def tearDown(self
) -> None:
76 self
._web
_server
.shutdown()
78 self
._web
_server
.server_close()
80 def testFetch(self
) -> None:
81 with requests
.session() as s
:
82 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
83 self
.assertEqual(self
._request
_counter
, 1)
84 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
85 self
.assertEqual(self
._request
_counter
, 2)
87 def testFetchCaching(self
) -> None:
88 with requests_cache
.CachedSession() as s
:
89 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
90 self
.assertEqual(self
._request
_counter
, 1)
91 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
92 self
.assertEqual(self
._request
_counter
, 1)
94 def testFetchPersistentCaching(self
) -> None:
95 with requests_cache
.CachedSession() as s
:
96 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
97 self
.assertEqual(self
._request
_counter
, 1)
98 with requests_cache
.CachedSession() as s
:
99 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
100 self
.assertEqual(self
._request
_counter
, 1)
102 def testFetchConents(self
) -> None:
103 with requests
.session() as s
:
104 post
= paperdoorknob
.Post(paperdoorknob
.fetch(
105 f
"http://localhost:{self._port()}", s
, TIMEOUT
))
106 self
.assertEqual(post
.text().text
.strip(), "This is glowfic")
107 self
.assertEqual([r
.text
.strip() for r
in post
.replies()],
108 ["You sure?", "Pretty sure."])
109 self
.assertEqual([r
.text
.strip() for r
in post
.entries()],
110 ["This is glowfic", "You sure?", "Pretty sure."])
112 def testFetchErrors(self
) -> None:
113 with requests
.session() as s
:
114 with self
.assertRaises(requests
.HTTPError
):
116 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
117 with self
.assertRaises(requests
.HTTPError
):
119 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
122 if __name__
== '__main__':