]> git.scottworley.com Git - paperdoorknob/blob - fetch_test.py
6bdf69ecc2a39e93f1903d15ce8a5fd8eea1ddf3
[paperdoorknob] / fetch_test.py
1 # paperdoorknob: Print glowfic
2 #
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.
6
7
8 import unittest
9 from requests import HTTPError
10 from testing.fakeserver import FakeGlowficServer
11 from fetch import CachingFetcher, DirectFetcher
12
13 TIMEOUT = 8
14
15
16 class TestFetch(unittest.TestCase):
17 def setUp(self) -> None:
18 self._server = self.enterContext(FakeGlowficServer())
19 self._port = self._server.port()
20
21 def testDirectFetch(self) -> None:
22 with DirectFetcher(TIMEOUT) as f:
23 f.fetch(f"http://localhost:{self._port}")
24 self.assertEqual(self._server.request_count(), 1)
25 f.fetch(f"http://localhost:{self._port}")
26 self.assertEqual(self._server.request_count(), 2)
27
28 def testFetchCaching(self) -> None:
29 with CachingFetcher("testcache", TIMEOUT) as f:
30 f.fetch(f"http://localhost:{self._port}")
31 self.assertEqual(self._server.request_count(), 1)
32 f.fetch(f"http://localhost:{self._port}")
33 self.assertEqual(self._server.request_count(), 1)
34
35 def testFetchPersistentCaching(self) -> None:
36 with CachingFetcher("testpersistentcache", TIMEOUT) as f:
37 f.fetch(f"http://localhost:{self._port}")
38 self.assertEqual(self._server.request_count(), 1)
39 with CachingFetcher("testpersistentcache", TIMEOUT) as f:
40 f.fetch(f"http://localhost:{self._port}")
41 self.assertEqual(self._server.request_count(), 1)
42
43 def testFetchErrors(self) -> None:
44 with DirectFetcher(TIMEOUT) as f:
45 with self.assertRaises(HTTPError):
46 f.fetch(f"http://localhost:{self._port}/not_found")
47 with self.assertRaises(HTTPError):
48 f.fetch(f"http://localhost:{self._port}/server_error")
49 with CachingFetcher("testerrorscache", TIMEOUT) as f:
50 with self.assertRaises(HTTPError):
51 f.fetch(f"http://localhost:{self._port}/not_found")
52 with self.assertRaises(HTTPError):
53 f.fetch(f"http://localhost:{self._port}/server_error")
54
55
56 if __name__ == '__main__':
57 unittest.main()