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">
36 <div class="flat-post-replies">
37 <div class="post-container post-reply">
40 <div class="post-container post-reply">
46 self
.send_response(self
._response
_code
())
47 self
.send_header("Content-type", "text/html")
48 self
.send_header("Content-Length", str(len(body
)))
50 self
.wfile
.write(body
)
54 class TestFetch(unittest
.TestCase
):
55 def _port(self
) -> int:
56 port
= self
._web
_server
.socket
.getsockname()[1]
57 assert isinstance(port
, int)
60 def _count_request(self
) -> None:
61 self
._request
_counter
+= 1
63 def setUp(self
) -> None:
64 self
._request
_counter
= 0
65 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
66 '_notify_test': lambda _
: self
._count
_request
()})
67 self
._web
_server
= HTTPServer(('', 0), handler
)
68 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
71 def tearDown(self
) -> None:
72 self
._web
_server
.shutdown()
74 self
._web
_server
.server_close()
76 def testFetch(self
) -> None:
77 with requests
.session() as s
:
78 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
79 self
.assertEqual(self
._request
_counter
, 1)
80 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
81 self
.assertEqual(self
._request
_counter
, 2)
83 def testFetchCaching(self
) -> None:
84 with requests_cache
.CachedSession() as s
:
85 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
86 self
.assertEqual(self
._request
_counter
, 1)
87 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
88 self
.assertEqual(self
._request
_counter
, 1)
90 def testFetchPersistentCaching(self
) -> None:
91 with requests_cache
.CachedSession() as s
:
92 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
93 self
.assertEqual(self
._request
_counter
, 1)
94 with requests_cache
.CachedSession() as s
:
95 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
96 self
.assertEqual(self
._request
_counter
, 1)
98 def testFetchConents(self
) -> None:
99 with requests
.session() as s
:
100 post
= paperdoorknob
.Post(paperdoorknob
.fetch(
101 f
"http://localhost:{self._port()}", s
, TIMEOUT
))
102 self
.assertEqual(post
.text().text
.strip(), "This is glowfic")
103 self
.assertEqual([r
.text
.strip() for r
in post
.replies()],
104 ["You sure?", "Pretty sure."])
105 self
.assertEqual([r
.text
.strip() for r
in post
.entries()],
106 ["This is glowfic", "You sure?", "Pretty sure."])
108 def testFetchErrors(self
) -> None:
109 with requests
.session() as s
:
110 with self
.assertRaises(requests
.HTTPError
):
112 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
113 with self
.assertRaises(requests
.HTTPError
):
115 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
118 if __name__
== '__main__':