virtual QNetworkCacheMetaData metaData(const QUrl &url) { QNetworkCacheMetaData metaData; if (!cachedData.isEmpty()) { metaData.setUrl(url); QDateTime now = QDateTime::currentDateTime(); metaData.setLastModified(now.addDays(-1)); metaData.setExpirationDate(now.addDays(1)); metaData.setSaveToDisk(true); } return metaData; }
void tst_QNetworkCacheMetaData::operatorEqual_data() { QTest::addColumn<QNetworkCacheMetaData>("other"); QTest::newRow("null") << QNetworkCacheMetaData(); QNetworkCacheMetaData data; data.setUrl(QUrl(EXAMPLE_URL)); QNetworkCacheMetaData::RawHeaderList headers; headers.append(QNetworkCacheMetaData::RawHeader("foo", "Bar")); data.setRawHeaders(headers); data.setLastModified(QDateTime::currentDateTime()); data.setExpirationDate(QDateTime::currentDateTime()); data.setSaveToDisk(false); QTest::newRow("valid") << data; }
void tst_QNetworkCacheMetaData::qnetworkcachemetadata() { QNetworkCacheMetaData data; QCOMPARE(data.expirationDate(), QDateTime()); QCOMPARE(data.isValid(), false); QCOMPARE(data.lastModified(), QDateTime()); QCOMPARE(data.operator!=(QNetworkCacheMetaData()), false); QNetworkCacheMetaData metaData; QCOMPARE(data.operator=(metaData), QNetworkCacheMetaData()); QCOMPARE(data.operator==(QNetworkCacheMetaData()), true); QCOMPARE(data.rawHeaders(), QNetworkCacheMetaData::RawHeaderList()); QCOMPARE(data.saveToDisk(), true); QCOMPARE(data.url(), QUrl()); data.setExpirationDate(QDateTime()); data.setLastModified(QDateTime()); data.setRawHeaders(QNetworkCacheMetaData::RawHeaderList()); data.setSaveToDisk(false); data.setUrl(QUrl()); }
void tst_QNetworkCacheMetaData::stream() { QNetworkCacheMetaData data; data.setUrl(QUrl(EXAMPLE_URL)); QNetworkCacheMetaData::RawHeaderList headers; headers.append(QNetworkCacheMetaData::RawHeader("foo", "Bar")); data.setRawHeaders(headers); data.setLastModified(QDateTime::currentDateTime()); data.setExpirationDate(QDateTime::currentDateTime()); data.setSaveToDisk(false); QBuffer buffer; buffer.open(QIODevice::ReadWrite); QDataStream stream(&buffer); stream << data; buffer.seek(0); QNetworkCacheMetaData data2; stream >> data2; QCOMPARE(data2, data); }
bool saveToCache(const QUrl& url, const QByteArray& file) { if (auto cache = NetworkAccessManager::getInstance().cache()) { if (!cache->metaData(url).isValid()) { QNetworkCacheMetaData metaData; metaData.setUrl(url); metaData.setSaveToDisk(true); metaData.setLastModified(QDateTime::currentDateTime()); metaData.setExpirationDate(QDateTime()); // Never expires // ioDevice is managed by the cache and should either be passed back to insert or remove! if (auto ioDevice = cache->prepare(metaData)) { ioDevice->write(file); cache->insert(ioDevice); qCDebug(asset_client) << url.toDisplayString() << "saved to disk cache"; return true; } qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache."; } } else { qCWarning(asset_client) << "No disk cache to save assets to."; } return false; }