author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 <QtNetwork/QtNetwork> |
|
45 |
#include "../../shared/util.h" |
|
46 |
#include "../network-settings.h" |
|
47 |
||
48 |
#define TESTFILE QString("http://%1/qtest/cgi-bin/").arg(QtNetworkSettings::serverName()) |
|
49 |
||
50 |
class tst_QAbstractNetworkCache : public QObject |
|
51 |
{ |
|
52 |
Q_OBJECT |
|
53 |
||
54 |
public: |
|
55 |
tst_QAbstractNetworkCache(); |
|
56 |
virtual ~tst_QAbstractNetworkCache(); |
|
57 |
||
58 |
private slots: |
|
59 |
void expires_data(); |
|
60 |
void expires(); |
|
61 |
||
62 |
void lastModified_data(); |
|
63 |
void lastModified(); |
|
64 |
||
65 |
void etag_data(); |
|
66 |
void etag(); |
|
67 |
||
68 |
void cacheControl_data(); |
|
69 |
void cacheControl(); |
|
70 |
||
71 |
void deleteCache(); |
|
72 |
||
73 |
private: |
|
74 |
void check(); |
|
75 |
}; |
|
76 |
||
77 |
class NetworkDiskCache : public QNetworkDiskCache |
|
78 |
{ |
|
79 |
Q_OBJECT |
|
80 |
public: |
|
81 |
NetworkDiskCache(QObject *parent = 0) |
|
82 |
: QNetworkDiskCache(parent) |
|
83 |
, gotData(false) |
|
84 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
QString location = QDir::tempPath() + QLatin1String("/tst_qnetworkdiskcache/"); |
0 | 86 |
setCacheDirectory(location); |
87 |
clear(); |
|
88 |
} |
|
89 |
||
90 |
QIODevice *data(const QUrl &url) |
|
91 |
{ |
|
92 |
gotData = true; |
|
93 |
return QNetworkDiskCache::data(url); |
|
94 |
} |
|
95 |
||
96 |
bool gotData; |
|
97 |
}; |
|
98 |
||
99 |
||
100 |
tst_QAbstractNetworkCache::tst_QAbstractNetworkCache() |
|
101 |
{ |
|
102 |
Q_SET_DEFAULT_IAP |
|
103 |
||
104 |
QCoreApplication::setOrganizationName(QLatin1String("Trolltech")); |
|
105 |
QCoreApplication::setApplicationName(QLatin1String("autotest_qabstractnetworkcache")); |
|
106 |
QCoreApplication::setApplicationVersion(QLatin1String("1.0")); |
|
107 |
} |
|
108 |
||
109 |
tst_QAbstractNetworkCache::~tst_QAbstractNetworkCache() |
|
110 |
{ |
|
111 |
} |
|
112 |
||
113 |
static bool AlwaysTrue = true; |
|
114 |
static bool AlwaysFalse = false; |
|
115 |
||
116 |
Q_DECLARE_METATYPE(QNetworkRequest::CacheLoadControl) |
|
117 |
||
118 |
void tst_QAbstractNetworkCache::expires_data() |
|
119 |
{ |
|
120 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl"); |
|
121 |
QTest::addColumn<QString>("url"); |
|
122 |
QTest::addColumn<bool>("fetchFromCache"); |
|
123 |
||
124 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires304.cgi" << AlwaysFalse; |
|
125 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires304.cgi" << true; |
|
126 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires304.cgi" << AlwaysTrue; |
|
127 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_expires304.cgi" << true; |
|
128 |
||
129 |
QTest::newRow("500-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires500.cgi" << AlwaysFalse; |
|
130 |
QTest::newRow("500-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires500.cgi" << true; |
|
131 |
QTest::newRow("500-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires500.cgi" << AlwaysTrue; |
|
132 |
QTest::newRow("500-3") << QNetworkRequest::PreferCache << "httpcachetest_expires500.cgi" << true; |
|
133 |
||
134 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires200.cgi" << AlwaysFalse; |
|
135 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires200.cgi" << false; |
|
136 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires200.cgi" << AlwaysTrue; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_expires200.cgi" << false; |
0 | 138 |
} |
139 |
||
140 |
void tst_QAbstractNetworkCache::expires() |
|
141 |
{ |
|
142 |
check(); |
|
143 |
} |
|
144 |
||
145 |
void tst_QAbstractNetworkCache::lastModified_data() |
|
146 |
{ |
|
147 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl"); |
|
148 |
QTest::addColumn<QString>("url"); |
|
149 |
QTest::addColumn<bool>("fetchFromCache"); |
|
150 |
||
151 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified304.cgi" << AlwaysFalse; |
|
152 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified304.cgi" << true; |
|
153 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified304.cgi" << AlwaysTrue; |
|
154 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified304.cgi" << true; |
|
155 |
||
156 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified200.cgi" << AlwaysFalse; |
|
157 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified200.cgi" << false; |
|
158 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified200.cgi" << AlwaysTrue; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified200.cgi" << false; |
0 | 160 |
} |
161 |
||
162 |
void tst_QAbstractNetworkCache::lastModified() |
|
163 |
{ |
|
164 |
check(); |
|
165 |
} |
|
166 |
||
167 |
void tst_QAbstractNetworkCache::etag_data() |
|
168 |
{ |
|
169 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl"); |
|
170 |
QTest::addColumn<QString>("url"); |
|
171 |
QTest::addColumn<bool>("fetchFromCache"); |
|
172 |
||
173 |
QTest::newRow("304-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag304.cgi" << AlwaysFalse; |
|
174 |
QTest::newRow("304-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag304.cgi" << true; |
|
175 |
QTest::newRow("304-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag304.cgi" << AlwaysTrue; |
|
176 |
QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_etag304.cgi" << true; |
|
177 |
||
178 |
QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag200.cgi" << AlwaysFalse; |
|
179 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag200.cgi" << false; |
|
180 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag200.cgi" << AlwaysTrue; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_etag200.cgi" << false; |
0 | 182 |
} |
183 |
||
184 |
void tst_QAbstractNetworkCache::etag() |
|
185 |
{ |
|
186 |
check(); |
|
187 |
} |
|
188 |
||
189 |
void tst_QAbstractNetworkCache::cacheControl_data() |
|
190 |
{ |
|
191 |
QTest::addColumn<QNetworkRequest::CacheLoadControl>("cacheLoadControl"); |
|
192 |
QTest::addColumn<QString>("url"); |
|
193 |
QTest::addColumn<bool>("fetchFromCache"); |
|
194 |
QTest::newRow("200-0") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=-1" << true; |
|
195 |
QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol-expire.cgi" << false; |
|
196 |
||
197 |
QTest::newRow("200-2") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?no-cache" << AlwaysFalse; |
|
198 |
QTest::newRow("200-3") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?no-cache" << false; |
|
199 |
QTest::newRow("200-4") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?no-cache" << false;//AlwaysTrue; |
|
200 |
QTest::newRow("200-5") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?no-cache" << false; |
|
201 |
||
202 |
QTest::newRow("304-0") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000" << true; |
|
203 |
||
204 |
QTest::newRow("304-1") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysFalse; |
|
205 |
QTest::newRow("304-2") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true; |
|
206 |
QTest::newRow("304-3") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysTrue; |
|
207 |
QTest::newRow("304-4") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
// see QTBUG-7060 |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
//QTest::newRow("nokia-boston") << QNetworkRequest::PreferNetwork << "http://waplabdc.nokia-boston.com/browser/users/venkat/cache/Cache_directives/private_1b.asp" << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
QTest::newRow("304-2b") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol200.cgi?private, max-age=1000" << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
QTest::newRow("304-4b") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol200.cgi?private, max-age=1000" << true; |
0 | 213 |
} |
214 |
||
215 |
void tst_QAbstractNetworkCache::cacheControl() |
|
216 |
{ |
|
217 |
check(); |
|
218 |
} |
|
219 |
||
220 |
void tst_QAbstractNetworkCache::check() |
|
221 |
{ |
|
222 |
QFETCH(QNetworkRequest::CacheLoadControl, cacheLoadControl); |
|
223 |
QFETCH(QString, url); |
|
224 |
QFETCH(bool, fetchFromCache); |
|
225 |
||
226 |
QNetworkAccessManager manager; |
|
227 |
NetworkDiskCache *diskCache = new NetworkDiskCache(&manager); |
|
228 |
manager.setCache(diskCache); |
|
229 |
QCOMPARE(diskCache->gotData, false); |
|
230 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
QUrl realUrl = url.contains("://") ? url : TESTFILE + url; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
QNetworkRequest request(realUrl); |
0 | 233 |
|
234 |
// prime the cache |
|
235 |
QNetworkReply *reply = manager.get(request); |
|
236 |
QSignalSpy downloaded1(reply, SIGNAL(finished())); |
|
237 |
QTRY_COMPARE(downloaded1.count(), 1); |
|
238 |
QCOMPARE(diskCache->gotData, false); |
|
239 |
QByteArray goodData = reply->readAll(); |
|
240 |
||
241 |
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, cacheLoadControl); |
|
242 |
||
243 |
// should be in the cache now |
|
244 |
QNetworkReply *reply2 = manager.get(request); |
|
245 |
QSignalSpy downloaded2(reply2, SIGNAL(finished())); |
|
246 |
QTRY_COMPARE(downloaded2.count(), 1); |
|
247 |
||
248 |
QByteArray secondData = reply2->readAll(); |
|
249 |
if (!fetchFromCache && cacheLoadControl == QNetworkRequest::AlwaysCache) { |
|
250 |
QCOMPARE(reply2->error(), QNetworkReply::ContentNotFoundError); |
|
251 |
QCOMPARE(secondData, QByteArray()); |
|
252 |
} else { |
|
253 |
if (reply2->error() != QNetworkReply::NoError) |
|
254 |
qDebug() << reply2->errorString(); |
|
255 |
QCOMPARE(reply2->error(), QNetworkReply::NoError); |
|
256 |
QCOMPARE(QString(secondData), QString(goodData)); |
|
257 |
QCOMPARE(secondData, goodData); |
|
258 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
259 |
} |
|
260 |
||
261 |
if (fetchFromCache) { |
|
262 |
QList<QByteArray> rawHeaderList = reply->rawHeaderList(); |
|
263 |
QList<QByteArray> rawHeaderList2 = reply2->rawHeaderList(); |
|
264 |
qSort(rawHeaderList); |
|
265 |
qSort(rawHeaderList2); |
|
266 |
||
267 |
// headers can change |
|
268 |
for (int i = 0; i < rawHeaderList.count(); ++i) { |
|
269 |
//qDebug() << i << rawHeaderList.value(i) << reply->rawHeader(rawHeaderList.value(i)); |
|
270 |
//qDebug() << i << rawHeaderList2.value(i) << reply2->rawHeader(rawHeaderList2.value(i)); |
|
271 |
//QCOMPARE(QString(rawHeaderList.value(i)), QString(rawHeaderList2.value(i))); |
|
272 |
//QCOMPARE(QString(reply->rawHeader(rawHeaderList.value(i))), QString(reply2->rawHeader(rawHeaderList2.value(i)))); |
|
273 |
} |
|
274 |
//QCOMPARE(rawHeaderList.count(), rawHeaderList2.count()); |
|
275 |
||
276 |
} |
|
277 |
QCOMPARE(diskCache->gotData, fetchFromCache); |
|
278 |
} |
|
279 |
||
280 |
void tst_QAbstractNetworkCache::deleteCache() |
|
281 |
{ |
|
282 |
QNetworkAccessManager manager; |
|
283 |
NetworkDiskCache *diskCache = new NetworkDiskCache(&manager); |
|
284 |
manager.setCache(diskCache); |
|
285 |
||
286 |
QString url = "httpcachetest_cachecontrol.cgi?max-age=1000"; |
|
287 |
QNetworkRequest request(QUrl(TESTFILE + url)); |
|
288 |
QNetworkReply *reply = manager.get(request); |
|
289 |
QSignalSpy downloaded1(reply, SIGNAL(finished())); |
|
290 |
manager.setCache(0); |
|
291 |
QTRY_COMPARE(downloaded1.count(), 1); |
|
292 |
} |
|
293 |
||
294 |
||
295 |
QTEST_MAIN(tst_QAbstractNetworkCache) |
|
296 |
#include "tst_qabstractnetworkcache.moc" |
|
297 |