def test_cache_existing_metadata_file(config_stub, tmpdir): """Test querying existing meta data file from activated cache.""" config_stub.data = { 'storage': { 'cache-size': 1024 }, 'general': { 'private-browsing': False } } url = 'http://qutebrowser.org' content = b'foobar' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) assert device is not None device.write(content) disk_cache.insert(device) disk_cache.updateMetaData(metadata) files = list(tmpdir.visit(fil=lambda path: path.isfile())) assert len(files) == 1 assert disk_cache.fileMetaData(str(files[0])) == metadata
def preload_cache(cache, url='http://www.example.com/', content=b'foobar'): metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() device = cache.prepare(metadata) assert device is not None device.write(content) cache.insert(device)
def test_cache_deactivated_private_browsing(config_stub, tmpdir): """Test if cache is deactivated in private-browsing mode.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": True}} disk_cache = cache.DiskCache(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl("http://www.example.com/")) assert metadata.isValid() assert disk_cache.prepare(metadata) is None
def test_cache_deactivated_update_metadata(config_stub, tmpdir): """Test updating the meta data when cache is not activated.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": True}} url = "http://qutebrowser.org" disk_cache = cache.DiskCache(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() assert disk_cache.updateMetaData(metadata) is None
def test_cache_metadata(disk_cache): """Ensure that DiskCache.metaData() returns exactly what was inserted.""" url = 'http://qutebrowser.org' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() device = disk_cache.prepare(metadata) device.write(b'foobar') disk_cache.insert(device) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_update_metadata(disk_cache): """Test updating the meta data for an existing cache entry.""" url = 'http://qutebrowser.org' preload_cache(disk_cache, url, b'foo') assert disk_cache.cacheSize() > 0 metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache.updateMetaData(metadata) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_deactivated_private_browsing(config_stub, tmpdir): """Test if cache is deactivated in private-browsing mode.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } disk_cache = cache.DiskCache(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl('http://www.example.com/')) assert metadata.isValid() assert disk_cache.prepare(metadata) is None
def test_cache_metadata(config_stub, tmpdir): """Ensure that DiskCache.metaData() returns exactly what was inserted.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": False}} url = "http://qutebrowser.org" metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) device.write(b"foobar") disk_cache.insert(device) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_update_metadata(config_stub, tmpdir): """Test updating the meta data for an existing cache entry.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": False}} url = "http://qutebrowser.org" disk_cache = cache.DiskCache(str(tmpdir)) preload_cache(disk_cache, url, b"foo") assert disk_cache.cacheSize() > 0 metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache.updateMetaData(metadata) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_deactivated_update_metadata(config_stub, tmpdir): """Test updating the meta data when cache is not activated.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } url = 'http://qutebrowser.org' disk_cache = cache.DiskCache(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() assert disk_cache.updateMetaData(metadata) is None
def test_cache_config_disable_private_browsing(config_stub, tmpdir): """Change private-browsing config to False and emit signal.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": True}} url = "http://qutebrowser.org" metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.prepare(metadata) is None config_stub.set("general", "private-browsing", False) content = b"cute" preload_cache(disk_cache, url, content) assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_update_metadata(config_stub, tmpdir): """Test updating the meta data for an existing cache entry.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': False} } url = 'http://qutebrowser.org' disk_cache = cache.DiskCache(str(tmpdir)) preload_cache(disk_cache, url, b'foo') assert disk_cache.cacheSize() > 0 metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache.updateMetaData(metadata) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_metadata(config_stub, tmpdir): """Ensure that DiskCache.metaData() returns exactly what was inserted.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': False} } url = 'http://qutebrowser.org' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) device.write(b'foobar') disk_cache.insert(device) assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_deactivated_insert_data(config_stub, tmpdir): """Insert data when cache is deactivated.""" # First create QNetworkDiskCache just to get a valid QIODevice from it url = "http://qutebrowser.org" disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) device = disk_cache.prepare(metadata) assert device is not None # Now create a deactivated DiskCache and insert the valid device created # above (there probably is a better way to get a valid QIODevice...) config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": True}} deactivated_cache = cache.DiskCache(str(tmpdir)) assert deactivated_cache.insert(device) is None
def test_cache_deactivated_metadata_file(config_stub, tmpdir): """Test querying meta data file when cache is deactivated.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.fileMetaData("foo") == QNetworkCacheMetaData()
def test_cache_full(config_stub, tmpdir): """Do a sanity test involving everything.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': False} } disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) url = 'http://qutebrowser.org' content = b'cutebowser' preload_cache(disk_cache, url, content) url2 = 'https://qutebrowser.org' content2 = b'ohmycert' preload_cache(disk_cache, url2, content2) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) soon = QDateTime.currentDateTime().addMonths(4) assert soon.isValid() metadata.setLastModified(soon) assert metadata.isValid() disk_cache.updateMetaData(metadata) disk_cache.remove(QUrl(url2)) assert disk_cache.metaData(QUrl(url)).lastModified() == soon assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_config_disable_private_browsing(config_stub, tmpdir): """Change private-browsing config to False and emit signal.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } url = 'http://qutebrowser.org' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.prepare(metadata) is None config_stub.set('general', 'private-browsing', False) content = b'cute' preload_cache(disk_cache, url, content) assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_existing_metadata_file(tmpdir, disk_cache): """Test querying existing meta data file from activated cache.""" url = 'http://qutebrowser.org' content = b'foobar' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() device = disk_cache.prepare(metadata) assert device is not None device.write(content) disk_cache.insert(device) disk_cache.updateMetaData(metadata) files = list(tmpdir.visit(fil=lambda path: path.isfile())) assert len(files) == 1 assert disk_cache.fileMetaData(str(files[0])) == metadata
def test_cache_deactivated_metadata(config_stub, tmpdir): """Test querying metaData() on not activated cache.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } url = 'http://qutebrowser.org' disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.metaData(QUrl(url)) == QNetworkCacheMetaData()
def test_cache_deactivated_insert_data(config_stub, tmpdir): """Insert data when cache is deactivated.""" # First create QNetworkDiskCache just to get a valid QIODevice from it url = 'http://qutebrowser.org' disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) device = disk_cache.prepare(metadata) assert device is not None # Now create a deactivated DiskCache and insert the valid device created # above (there probably is a better way to get a valid QIODevice...) config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } deactivated_cache = cache.DiskCache(str(tmpdir)) assert deactivated_cache.insert(device) is None
def test_cache_existing_metadata_file(config_stub, tmpdir): """Test querying existing meta data file from activated cache.""" config_stub.data = {"storage": {"cache-size": 1024}, "general": {"private-browsing": False}} url = "http://qutebrowser.org" content = b"foobar" metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) assert device is not None device.write(content) disk_cache.insert(device) disk_cache.updateMetaData(metadata) files = list(tmpdir.visit(fil=lambda path: path.isfile())) assert len(files) == 1 assert disk_cache.fileMetaData(str(files[0])) == metadata
def metaData(self, url): """Return the meta data for the url url. Args: url: A QUrl. Return: A QNetworkCacheMetaData object. """ if objreg.get('general', 'private-browsing'): return QNetworkCacheMetaData() else: return super().metaData(url)
def fileMetaData(self, filename): """Returns the QNetworkCacheMetaData for the cache file filename. Args: filename: The file name as a string. Return: A QNetworkCacheMetaData object. """ if objreg.get('general', 'private-browsing'): return QNetworkCacheMetaData() else: return super().fileMetaData(filename)
def fileMetaData(self, filename): """Return the QNetworkCacheMetaData for the cache file filename. Args: filename: The file name as a string. Return: A QNetworkCacheMetaData object. """ if self._activated: return super().fileMetaData(filename) else: return QNetworkCacheMetaData()
def metaData(self, url): """Return the meta data for the url url. Args: url: A QUrl. Return: A QNetworkCacheMetaData object. """ if self._activated: return super().metaData(url) else: return QNetworkCacheMetaData()
def test_cache_full(tmpdir): """Do a sanity test involving everything.""" disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) url = 'http://glimpsebrowser.org' content = b'cutebowser' preload_cache(disk_cache, url, content) url2 = 'https://glimpsebrowser.org' content2 = b'ohmycert' preload_cache(disk_cache, url2, content2) metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) soon = QDateTime.currentDateTime().addMonths(4) assert soon.isValid() metadata.setLastModified(soon) assert metadata.isValid() disk_cache.updateMetaData(metadata) disk_cache.remove(QUrl(url2)) assert disk_cache.metaData(QUrl(url)).lastModified() == soon assert disk_cache.data(QUrl(url)).readAll() == content
def metaData(self, _url): return QNetworkCacheMetaData()