def test_write_read(self): Cache.directory = ".cache_test" link = "http://github.com/lexndru/hap" success, _ = Cache.write_link(link, "hap") self.assertTrue(success) success, data = Cache.read_link(link) self.assertTrue(success) self.assertEqual(data, "hap") rmtree(Cache.directory)
def test_empty_read_write(self): Cache.directory = "/tmp" ok, data = Cache.write("", "") self.assertFalse(ok) self.assertEqual("missing cache path", data) ok, data = Cache.write("hap_test.html", "") self.assertFalse(ok) self.assertEqual("missing cache data", data) with open("/tmp/.hap.tmp", "w") as fd: fd.write("") Cache.directory = "/tmp" ok, data = Cache.read(".hap.tmp") self.assertFalse(ok) self.assertEqual(data, "empty file") remove("/tmp/.hap.tmp")
def prepare_link(self, link): """The "link" protocol. Set or update the link of the dataplan. Can be cached. """ self.link = link if link.startswith(self.FILE_PROTOCOL): filepath = link[len(self.FILE_PROTOCOL):] if not path.exists(filepath): Log.fatal("Cannot get content from file: file does not exist") Log.debug(u"Getting content from local file: {}".format(link)) content = "" with open(filepath) as fd: content = fd.read() return self.prepare_source_code_from_cache(content) elif link.startswith(self.HTTP_PROTOCOL) \ or link.startswith(self.HTTPS_PROTOCOL): if not self.no_cache: ok, cache = Cache.read_link(self.link) if ok: Log.debug(u"Getting content from cache: {}".format(link)) return self.prepare_source_code_from_cache(cache) Log.debug(u"Getting content from URL: {}".format(link)) self.open_url() return self.prepare_source_code() Log.fatal(u"Unsupported link protocol: must be file or http(s)")
def open_url(self): """Simple URL reader. Access an URL and read it's content. Can be cached. If URL returns a non-OK (200) status code, a warning is printed, but if it return a non-HTML content-type, a fatal log is set. """ status, source = self.read_url(self.link) if not status: Log.fatal(u"Cannot reach link: {}".format(source)) if status and not str(source.code).startswith("2"): Log.warn(u"Non-2xx status code: {}".format(source.code)) if hasattr(source.info(), "gettype"): mimetype = source.info().gettype() if mimetype not in self.supported_mime_types: Log.fatal(u"Unsupported content, got {}".format(mimetype)) self.source = source.read() if not self.no_cache: ok, status = Cache.write_link(self.link, self.source) if not ok: Log.warn(status) return self
def test_bad_read(self): success, data = Cache.read_link("not existing link") self.assertFalse(success) self.assertEqual("no cache to read", data)
def test_file_friendly(self): bad_filename = "~!bad@#$%^filename&*()1234567890" good_filename = "__bad_____filename____1234567890" friendly_file = Cache.file_friendly(bad_filename) self.assertEqual(friendly_file, good_filename)
def test_get_file(self): filename = "github_com_lexndru_hap" cache_file = Cache.get_file(u"http://github.com/lexndru/hap") self.assertEqual(cache_file, filename)
def setUp(self): Cache.directory = ".cache_test" success, _ = Cache.write_link("http://localhost/mockup", HTMLDATA) self.assertTrue(success)