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.
12 from http
.server
import BaseHTTPRequestHandler
, HTTPServer
20 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler
):
22 def _notify_test(self
) -> None:
23 raise NotImplementedError()
25 def _response_code(self
) -> int:
26 if self
.path
== "/not_found":
28 if self
.path
== "/server_error":
32 def do_GET(self
) -> None:
35 <div class="post-container post-post">
36 <div class="post-edit-box">We don't want edit boxes</div>
38 <div class="post-footer">We don't want footers</div>
40 <div class="flat-post-replies">
41 <div class="post-container post-reply">
42 <div class="post-edit-box">We don't want edit boxes</div>
44 <div class="post-footer">We don't want footers</div>
46 <div class="post-container post-reply">
52 self
.send_response(self
._response
_code
())
53 self
.send_header("Content-type", "text/html")
54 self
.send_header("Content-Length", str(len(body
)))
56 self
.wfile
.write(body
)
60 class TestFetch(unittest
.TestCase
):
61 def _port(self
) -> int:
62 port
= self
._web
_server
.socket
.getsockname()[1]
63 assert isinstance(port
, int)
66 def _count_request(self
) -> None:
67 self
._request
_counter
+= 1
69 def setUp(self
) -> None:
70 self
._request
_counter
= 0
71 handler
= type("Handler", (FakeGlowficHTTPRequestHandler
,), {
72 '_notify_test': lambda _
: self
._count
_request
()})
73 self
._web
_server
= HTTPServer(('', 0), handler
)
74 self
._thread
= threading
.Thread(target
=self
._web
_server
.serve_forever
)
77 def tearDown(self
) -> None:
78 self
._web
_server
.shutdown()
80 self
._web
_server
.server_close()
82 def testFetch(self
) -> None:
83 with requests
.session() as s
:
84 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
85 self
.assertEqual(self
._request
_counter
, 1)
86 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
87 self
.assertEqual(self
._request
_counter
, 2)
89 def testFetchCaching(self
) -> None:
90 with requests_cache
.CachedSession() as s
:
91 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
92 self
.assertEqual(self
._request
_counter
, 1)
93 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
94 self
.assertEqual(self
._request
_counter
, 1)
96 def testFetchPersistentCaching(self
) -> None:
97 with requests_cache
.CachedSession() as s
:
98 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
99 self
.assertEqual(self
._request
_counter
, 1)
100 with requests_cache
.CachedSession() as s
:
101 paperdoorknob
.fetch(f
"http://localhost:{self._port()}", s
, TIMEOUT
)
102 self
.assertEqual(self
._request
_counter
, 1)
104 def testReplies(self
) -> None:
105 with requests
.session() as s
:
106 replies
= paperdoorknob
.replies(
109 f
"http://localhost:{self._port()}",
112 self
.assertEqual([r
.text
.strip() for r
in replies
],
113 ["This is glowfic", "You sure?", "Pretty sure."])
115 def testFetchErrors(self
) -> None:
116 with requests
.session() as s
:
117 with self
.assertRaises(requests
.HTTPError
):
119 f
"http://localhost:{self._port()}/not_found", s
, TIMEOUT
)
120 with self
.assertRaises(requests
.HTTPError
):
122 f
"http://localhost:{self._port()}/server_error", s
, TIMEOUT
)
124 def testProcess(self
) -> None:
125 with requests
.session() as s
:
127 paperdoorknob
.process(
128 f
"http://localhost:{self._port()}",
133 self
.assertEqual(buf
.getvalue(), b
'''\\documentclass{article}
141 def testPDF(self
) -> None:
142 with requests
.session() as s
:
143 with open("test.tex", 'wb') as out
:
144 paperdoorknob
.process(
145 f
"http://localhost:{self._port()}",
150 subprocess
.run(['pdflatex', 'test.tex'],
151 stdin
=subprocess
.DEVNULL
, check
=True)
154 if __name__
== '__main__':