0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#include <QtGui/QtGui>
|
|
45 |
#include <QtNetwork/QtNetwork>
|
|
46 |
#include "../../shared/util.h"
|
|
47 |
#include "../network-settings.h"
|
|
48 |
|
|
49 |
#define TESTFILE QString("http://%1/qtest/cgi-bin/").arg(QtNetworkSettings::serverName())
|
|
50 |
|
|
51 |
class tst_QAbstractNetworkCache : public QObject
|
|
52 |
{
|
|
53 |
Q_OBJECT
|
|
54 |
|
|
55 |
public:
|
|
56 |
tst_QAbstractNetworkCache();
|
|
57 |
virtual ~tst_QAbstractNetworkCache();
|
|
58 |
|
|
59 |
private slots:
|
|
60 |
void expires_data();
|
|
61 |
void expires();
|
|
62 |
|
|
63 |
void lastModified_data();
|
|
64 |
void lastModified();
|
|
65 |
|
|
66 |
void etag_data();
|
|
67 |
void etag();
|
|
68 |
|
|
69 |
void cacheControl_data();
|
|
70 |
void cacheControl();
|
|
71 |
|
|
72 |
void deleteCache();
|
|
73 |
|
|
74 |
private:
|
|
75 |
void check();
|
|
76 |
};
|
|
77 |
|
|
78 |
class NetworkDiskCache : public QNetworkDiskCache
|
|
79 |
{
|
|
80 |
Q_OBJECT
|
|
81 |
public:
|
|
82 |
NetworkDiskCache(QObject *parent = 0)
|
|
83 |
: QNetworkDiskCache(parent)
|
|
84 |
, gotData(false)
|
|
85 |
{
|
|
86 |
QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation)
|
|
87 |
+ QLatin1String("/qnetworkdiskcache/");
|
|
88 |
setCacheDirectory(location);
|
|
89 |
clear();
|
|
90 |
}
|
|
91 |
|
|
92 |
QIODevice *data(const QUrl &url)
|
|
93 |
{
|
|
94 |
gotData = true;
|
|
95 |
return QNetworkDiskCache::data(url);
|
|
96 |
}
|
|
97 |
|
|
98 |
bool gotData;
|
|
99 |
};
|
|
100 |
|
|
101 |
|
|
102 |
tst_QAbstractNetworkCache::tst_QAbstractNetworkCache()
|
|
103 |
{
|
|
104 |
Q_SET_DEFAULT_IAP
|
|
105 |
|
|
106 |
QCoreApplication::setOrganizationName(QLatin1String("Trolltech"));
|
|
107 |
QCoreApplication::setApplicationName(QLatin1String("autotest_qabstractnetworkcache"));
|
|
108 |
QCoreApplication::setApplicationVersion(QLatin1String("1.0"));
|
|
109 |
}
|
|
110 |
|
|
111 |
tst_QAbstractNetworkCache::~tst_QAbstractNetworkCache()
|
|
112 |
{
|
|
113 |
}
|
|
114 |
|
|
115 |
static bool AlwaysTrue = true;
|
|
116 |
static bool AlwaysFalse = false;
|
|
117 |
|
|
118 |
Q_DECLARE_METATYPE(QNetworkRequest::CacheLoadControl)
|
|
119 |
|
|
120 |
void tst_QAbstractNetworkCache::expires_data()
|
|
121 |
{
|
|
122 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl");
|
|
123 |
QTest::addColumn<QString>("url");
|
|
124 |
QTest::addColumn<bool>("fetchFromCache");
|
|
125 |
|
|
126 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires304.cgi" << AlwaysFalse;
|
|
127 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires304.cgi" << true;
|
|
128 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires304.cgi" << AlwaysTrue;
|
|
129 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_expires304.cgi" << true;
|
|
130 |
|
|
131 |
QTest::newRow("500-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires500.cgi" << AlwaysFalse;
|
|
132 |
QTest::newRow("500-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires500.cgi" << true;
|
|
133 |
QTest::newRow("500-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires500.cgi" << AlwaysTrue;
|
|
134 |
QTest::newRow("500-3") << QNetworkRequest::PreferCache << "httpcachetest_expires500.cgi" << true;
|
|
135 |
|
|
136 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires200.cgi" << AlwaysFalse;
|
|
137 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires200.cgi" << false;
|
|
138 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires200.cgi" << AlwaysTrue;
|
|
139 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_expires200.cgi" << true;
|
|
140 |
}
|
|
141 |
|
|
142 |
void tst_QAbstractNetworkCache::expires()
|
|
143 |
{
|
|
144 |
check();
|
|
145 |
}
|
|
146 |
|
|
147 |
void tst_QAbstractNetworkCache::lastModified_data()
|
|
148 |
{
|
|
149 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl");
|
|
150 |
QTest::addColumn<QString>("url");
|
|
151 |
QTest::addColumn<bool>("fetchFromCache");
|
|
152 |
|
|
153 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified304.cgi" << AlwaysFalse;
|
|
154 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified304.cgi" << true;
|
|
155 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified304.cgi" << AlwaysTrue;
|
|
156 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified304.cgi" << true;
|
|
157 |
|
|
158 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified200.cgi" << AlwaysFalse;
|
|
159 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified200.cgi" << false;
|
|
160 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified200.cgi" << AlwaysTrue;
|
|
161 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified200.cgi" << true;
|
|
162 |
}
|
|
163 |
|
|
164 |
void tst_QAbstractNetworkCache::lastModified()
|
|
165 |
{
|
|
166 |
check();
|
|
167 |
}
|
|
168 |
|
|
169 |
void tst_QAbstractNetworkCache::etag_data()
|
|
170 |
{
|
|
171 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl");
|
|
172 |
QTest::addColumn<QString>("url");
|
|
173 |
QTest::addColumn<bool>("fetchFromCache");
|
|
174 |
|
|
175 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag304.cgi" << AlwaysFalse;
|
|
176 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag304.cgi" << true;
|
|
177 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag304.cgi" << AlwaysTrue;
|
|
178 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_etag304.cgi" << true;
|
|
179 |
|
|
180 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag200.cgi" << AlwaysFalse;
|
|
181 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag200.cgi" << false;
|
|
182 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag200.cgi" << AlwaysTrue;
|
|
183 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_etag200.cgi" << true;
|
|
184 |
}
|
|
185 |
|
|
186 |
void tst_QAbstractNetworkCache::etag()
|
|
187 |
{
|
|
188 |
check();
|
|
189 |
}
|
|
190 |
|
|
191 |
void tst_QAbstractNetworkCache::cacheControl_data()
|
|
192 |
{
|
|
193 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl");
|
|
194 |
QTest::addColumn<QString>("url");
|
|
195 |
QTest::addColumn<bool>("fetchFromCache");
|
|
196 |
QTest::newRow("200-0") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=-1" << true;
|
|
197 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol-expire.cgi" << false;
|
|
198 |
|
|
199 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?no-cache" << AlwaysFalse;
|
|
200 |
QTest::newRow("200-3") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?no-cache" << false;
|
|
201 |
QTest::newRow("200-4") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?no-cache" << false;//AlwaysTrue;
|
|
202 |
QTest::newRow("200-5") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?no-cache" << false;
|
|
203 |
|
|
204 |
QTest::newRow("304-0") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000" << true;
|
|
205 |
|
|
206 |
QTest::newRow("304-1") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysFalse;
|
|
207 |
QTest::newRow("304-2") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
|
|
208 |
QTest::newRow("304-3") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysTrue;
|
|
209 |
QTest::newRow("304-4") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
|
|
210 |
}
|
|
211 |
|
|
212 |
void tst_QAbstractNetworkCache::cacheControl()
|
|
213 |
{
|
|
214 |
check();
|
|
215 |
}
|
|
216 |
|
|
217 |
void tst_QAbstractNetworkCache::check()
|
|
218 |
{
|
|
219 |
QFETCH(QNetworkRequest::CacheLoadControl, cacheLoadControl);
|
|
220 |
QFETCH(QString, url);
|
|
221 |
QFETCH(bool, fetchFromCache);
|
|
222 |
|
|
223 |
QNetworkAccessManager manager;
|
|
224 |
NetworkDiskCache *diskCache = new NetworkDiskCache(&manager);
|
|
225 |
manager.setCache(diskCache);
|
|
226 |
QCOMPARE(diskCache->gotData, false);
|
|
227 |
|
|
228 |
QNetworkRequest request(QUrl(TESTFILE + url));
|
|
229 |
|
|
230 |
// prime the cache
|
|
231 |
QNetworkReply *reply = manager.get(request);
|
|
232 |
QSignalSpy downloaded1(reply, SIGNAL(finished()));
|
|
233 |
QTRY_COMPARE(downloaded1.count(), 1);
|
|
234 |
QCOMPARE(diskCache->gotData, false);
|
|
235 |
QByteArray goodData = reply->readAll();
|
|
236 |
|
|
237 |
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, cacheLoadControl);
|
|
238 |
|
|
239 |
// should be in the cache now
|
|
240 |
QNetworkReply *reply2 = manager.get(request);
|
|
241 |
QSignalSpy downloaded2(reply2, SIGNAL(finished()));
|
|
242 |
QTRY_COMPARE(downloaded2.count(), 1);
|
|
243 |
|
|
244 |
QByteArray secondData = reply2->readAll();
|
|
245 |
if (!fetchFromCache && cacheLoadControl == QNetworkRequest::AlwaysCache) {
|
|
246 |
QCOMPARE(reply2->error(), QNetworkReply::ContentNotFoundError);
|
|
247 |
QCOMPARE(secondData, QByteArray());
|
|
248 |
} else {
|
|
249 |
if (reply2->error() != QNetworkReply::NoError)
|
|
250 |
qDebug() << reply2->errorString();
|
|
251 |
QCOMPARE(reply2->error(), QNetworkReply::NoError);
|
|
252 |
QCOMPARE(QString(secondData), QString(goodData));
|
|
253 |
QCOMPARE(secondData, goodData);
|
|
254 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
|
|
255 |
}
|
|
256 |
|
|
257 |
if (fetchFromCache) {
|
|
258 |
QList<QByteArray> rawHeaderList = reply->rawHeaderList();
|
|
259 |
QList<QByteArray> rawHeaderList2 = reply2->rawHeaderList();
|
|
260 |
qSort(rawHeaderList);
|
|
261 |
qSort(rawHeaderList2);
|
|
262 |
|
|
263 |
// headers can change
|
|
264 |
for (int i = 0; i < rawHeaderList.count(); ++i) {
|
|
265 |
//qDebug() << i << rawHeaderList.value(i) << reply->rawHeader(rawHeaderList.value(i));
|
|
266 |
//qDebug() << i << rawHeaderList2.value(i) << reply2->rawHeader(rawHeaderList2.value(i));
|
|
267 |
//QCOMPARE(QString(rawHeaderList.value(i)), QString(rawHeaderList2.value(i)));
|
|
268 |
//QCOMPARE(QString(reply->rawHeader(rawHeaderList.value(i))), QString(reply2->rawHeader(rawHeaderList2.value(i))));
|
|
269 |
}
|
|
270 |
//QCOMPARE(rawHeaderList.count(), rawHeaderList2.count());
|
|
271 |
|
|
272 |
}
|
|
273 |
QCOMPARE(diskCache->gotData, fetchFromCache);
|
|
274 |
}
|
|
275 |
|
|
276 |
void tst_QAbstractNetworkCache::deleteCache()
|
|
277 |
{
|
|
278 |
QNetworkAccessManager manager;
|
|
279 |
NetworkDiskCache *diskCache = new NetworkDiskCache(&manager);
|
|
280 |
manager.setCache(diskCache);
|
|
281 |
|
|
282 |
QString url = "httpcachetest_cachecontrol.cgi?max-age=1000";
|
|
283 |
QNetworkRequest request(QUrl(TESTFILE + url));
|
|
284 |
QNetworkReply *reply = manager.get(request);
|
|
285 |
QSignalSpy downloaded1(reply, SIGNAL(finished()));
|
|
286 |
manager.setCache(0);
|
|
287 |
QTRY_COMPARE(downloaded1.count(), 1);
|
|
288 |
}
|
|
289 |
|
|
290 |
|
|
291 |
QTEST_MAIN(tst_QAbstractNetworkCache)
|
|
292 |
#include "tst_qabstractnetworkcache.moc"
|
|
293 |
|