def test_format_url_to_filename(self): url = 'https://stackoverflow.com/questions/tagged/cat' invalid_filename_characters = ['/', '\\', '%'] filename = howdoi._format_url_to_filename(url, 'html') self.assertTrue(filename) self.assertTrue(filename.endswith('html')) for invalid_character in invalid_filename_characters: self.assertNotIn(invalid_character, filename)
def _get_result_mock(self, url): file_name = howdoi._format_url_to_filename(url, 'html.gz') file_path = os.path.join(howdoi.HTML_CACHE_PATH, file_name) try: with gzip.open(file_path, 'rb') as f: cached_page_content = str(f.read(), encoding='utf-8') return cached_page_content except FileNotFoundError: page_content = self.original_get_result(url) with gzip.open(file_path, 'wb') as f: f.write(bytes(page_content, encoding='utf-8')) return page_content
def _get_result_mock(self, url): file_name = howdoi._format_url_to_filename(url) file_path = os.path.join(howdoi.HTML_CACHE_PATH, file_name) try: f = open(file_path, 'r', encoding="utf8") cached_file_content = f.read() f.close() return cached_file_content except FileNotFoundError: result = self.original_get_result(url) f = open(file_path, 'w+') f.write(result) f.close() return result
def _get_result_mock(url): # pylint: disable=protected-access file_name = howdoi._format_url_to_filename(url, 'html.gz') # pylint: disable=no-member file_path = Path.joinpath(Path(howdoi.HTML_CACHE_PATH), Path(file_name)).resolve() try: with gzip.open(file_path, 'rb') as f: cached_page_content = str(f.read(), encoding='utf-8') return cached_page_content except FileNotFoundError: page_content = original_get_result(url) with gzip.open(file_path, 'wb') as f: f.write(bytes(page_content, encoding='utf-8')) return page_content