author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 <QtCore/QCryptographicHash> |
|
45 |
#include <QtCore/QDataStream> |
|
46 |
#include <QtCore/QUrl> |
|
47 |
#include <QtCore/QEventLoop> |
|
48 |
#include <QtCore/QFile> |
|
49 |
#include <QtCore/QSharedPointer> |
|
50 |
#include <QtCore/QTemporaryFile> |
|
51 |
#include <QtNetwork/QTcpServer> |
|
52 |
#include <QtNetwork/QTcpSocket> |
|
53 |
#include <QtNetwork/QLocalSocket> |
|
54 |
#include <QtNetwork/QLocalServer> |
|
55 |
#include <QtNetwork/QHostInfo> |
|
56 |
#include <QtNetwork/QFtp> |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
57 |
#include <QtNetwork/QAbstractNetworkCache> |
0 | 58 |
#include <QtNetwork/qauthenticator.h> |
59 |
#include <QtNetwork/qnetworkaccessmanager.h> |
|
60 |
#include <QtNetwork/qnetworkrequest.h> |
|
61 |
#include <QtNetwork/qnetworkreply.h> |
|
62 |
#include <QtNetwork/qnetworkcookie.h> |
|
63 |
#ifndef QT_NO_OPENSSL |
|
64 |
#include <QtNetwork/qsslerror.h> |
|
65 |
#include <QtNetwork/qsslconfiguration.h> |
|
66 |
#endif |
|
67 |
||
68 |
#include <time.h> |
|
69 |
||
70 |
#include "private/qnetworkaccessmanager_p.h" |
|
71 |
||
72 |
#ifdef Q_OS_SYMBIAN |
|
73 |
#define SRCDIR "." |
|
74 |
#endif |
|
75 |
||
76 |
#include "../network-settings.h" |
|
77 |
||
78 |
||
79 |
Q_DECLARE_METATYPE(QNetworkReply*) |
|
80 |
Q_DECLARE_METATYPE(QAuthenticator*) |
|
81 |
Q_DECLARE_METATYPE(QNetworkProxy) |
|
82 |
Q_DECLARE_METATYPE(QNetworkProxyQuery) |
|
83 |
Q_DECLARE_METATYPE(QList<QNetworkProxy>) |
|
84 |
Q_DECLARE_METATYPE(QNetworkReply::NetworkError) |
|
85 |
||
86 |
class QNetworkReplyPtr: public QSharedPointer<QNetworkReply> |
|
87 |
{ |
|
88 |
public: |
|
89 |
inline QNetworkReplyPtr(QNetworkReply *ptr = 0) |
|
90 |
: QSharedPointer<QNetworkReply>(ptr) |
|
91 |
{ } |
|
92 |
||
93 |
inline operator QNetworkReply *() const { return data(); } |
|
94 |
}; |
|
95 |
||
96 |
class MyCookieJar; |
|
97 |
class tst_QNetworkReply: public QObject |
|
98 |
{ |
|
99 |
Q_OBJECT |
|
100 |
||
101 |
struct ProxyData { |
|
102 |
ProxyData(const QNetworkProxy &p, const QByteArray &t, bool auth) |
|
103 |
: tag(t), proxy(p), requiresAuthentication(auth) |
|
104 |
{ } |
|
105 |
QByteArray tag; |
|
106 |
QNetworkProxy proxy; |
|
107 |
bool requiresAuthentication; |
|
108 |
}; |
|
109 |
||
110 |
QEventLoop *loop; |
|
111 |
enum RunSimpleRequestReturn { Timeout = 0, Success, Failure }; |
|
112 |
int returnCode; |
|
113 |
QString testFileName; |
|
114 |
#if !defined Q_OS_WIN |
|
115 |
QString wronlyFileName; |
|
116 |
#endif |
|
117 |
QString uniqueExtension; |
|
118 |
QList<ProxyData> proxies; |
|
119 |
QNetworkAccessManager manager; |
|
120 |
MyCookieJar *cookieJar; |
|
121 |
#ifndef QT_NO_OPENSSL |
|
122 |
QSslConfiguration storedSslConfiguration; |
|
123 |
QList<QSslError> storedExpectedSslErrors; |
|
124 |
#endif |
|
125 |
||
126 |
public: |
|
127 |
tst_QNetworkReply(); |
|
128 |
~tst_QNetworkReply(); |
|
129 |
QString runSimpleRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, |
|
130 |
QNetworkReplyPtr &reply, const QByteArray &data = QByteArray()); |
|
131 |
||
132 |
public Q_SLOTS: |
|
133 |
void finished(); |
|
134 |
void gotError(); |
|
135 |
void authenticationRequired(QNetworkReply*,QAuthenticator*); |
|
136 |
void proxyAuthenticationRequired(const QNetworkProxy &,QAuthenticator*); |
|
137 |
||
138 |
#ifndef QT_NO_OPENSSL |
|
139 |
void sslErrors(QNetworkReply*,const QList<QSslError> &); |
|
140 |
void storeSslConfiguration(); |
|
141 |
void ignoreSslErrorListSlot(QNetworkReply *reply, const QList<QSslError> &); |
|
142 |
#endif |
|
143 |
||
144 |
protected Q_SLOTS: |
|
145 |
void nestedEventLoops_slot(); |
|
146 |
||
147 |
private Q_SLOTS: |
|
148 |
void init(); |
|
149 |
void cleanup(); |
|
150 |
void initTestCase(); |
|
151 |
void cleanupTestCase(); |
|
152 |
||
153 |
void stateChecking(); |
|
154 |
void invalidProtocol(); |
|
155 |
void getFromData_data(); |
|
156 |
void getFromData(); |
|
157 |
void getFromFile(); |
|
158 |
void getFromFileSpecial_data(); |
|
159 |
void getFromFileSpecial(); |
|
160 |
void getFromFtp_data(); |
|
161 |
void getFromFtp(); |
|
162 |
void getFromHttp_data(); |
|
163 |
void getFromHttp(); |
|
164 |
void getErrors_data(); |
|
165 |
void getErrors(); |
|
166 |
void putToFile_data(); |
|
167 |
void putToFile(); |
|
168 |
void putToFtp_data(); |
|
169 |
void putToFtp(); |
|
170 |
void putToHttp_data(); |
|
171 |
void putToHttp(); |
|
172 |
void postToHttp_data(); |
|
173 |
void postToHttp(); |
|
174 |
void deleteFromHttp_data(); |
|
175 |
void deleteFromHttp(); |
|
176 |
void putGetDeleteGetFromHttp_data(); |
|
177 |
void putGetDeleteGetFromHttp(); |
|
178 |
||
179 |
void ioGetFromData_data(); |
|
180 |
void ioGetFromData(); |
|
181 |
void ioGetFromFileSpecial_data(); |
|
182 |
void ioGetFromFileSpecial(); |
|
183 |
void ioGetFromFile_data(); |
|
184 |
void ioGetFromFile(); |
|
185 |
void ioGetFromFtp_data(); |
|
186 |
void ioGetFromFtp(); |
|
187 |
void ioGetFromFtpWithReuse(); |
|
188 |
void ioGetFromHttp(); |
|
189 |
||
190 |
void ioGetFromHttpWithReuseParallel(); |
|
191 |
void ioGetFromHttpWithReuseSequential(); |
|
192 |
void ioGetFromHttpWithAuth(); |
|
193 |
void ioGetFromHttpWithProxyAuth(); |
|
194 |
void ioGetFromHttpWithSocksProxy(); |
|
195 |
#ifndef QT_NO_OPENSSL |
|
196 |
void ioGetFromHttpsWithSslErrors(); |
|
197 |
void ioGetFromHttpsWithIgnoreSslErrors(); |
|
198 |
void ioGetFromHttpsWithSslHandshakeError(); |
|
199 |
#endif |
|
200 |
void ioGetFromHttpBrokenServer_data(); |
|
201 |
void ioGetFromHttpBrokenServer(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
202 |
void ioGetFromHttpWithCache_data(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
203 |
void ioGetFromHttpWithCache(); |
0 | 204 |
|
205 |
void ioGetWithManyProxies_data(); |
|
206 |
void ioGetWithManyProxies(); |
|
207 |
||
208 |
void ioPutToFileFromFile_data(); |
|
209 |
void ioPutToFileFromFile(); |
|
210 |
void ioPutToFileFromSocket_data(); |
|
211 |
void ioPutToFileFromSocket(); |
|
212 |
void ioPutToFileFromLocalSocket_data(); |
|
213 |
void ioPutToFileFromLocalSocket(); |
|
214 |
void ioPutToFileFromProcess_data(); |
|
215 |
void ioPutToFileFromProcess(); |
|
216 |
void ioPutToFtpFromFile_data(); |
|
217 |
void ioPutToFtpFromFile(); |
|
218 |
void ioPutToHttpFromFile_data(); |
|
219 |
void ioPutToHttpFromFile(); |
|
220 |
void ioPostToHttpFromFile_data(); |
|
221 |
void ioPostToHttpFromFile(); |
|
222 |
void ioPostToHttpFromSocket_data(); |
|
223 |
void ioPostToHttpFromSocket(); |
|
224 |
void ioPostToHttpFromMiddleOfFileToEnd(); |
|
225 |
void ioPostToHttpFromMiddleOfFileFiveBytes(); |
|
226 |
void ioPostToHttpFromMiddleOfQBufferFiveBytes(); |
|
227 |
void ioPostToHttpNoBufferFlag(); |
|
228 |
void ioPostToHttpUploadProgress(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
229 |
void ioPostToHttpEmptyUploadProgress(); |
0 | 230 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
void lastModifiedHeaderForFile(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
void lastModifiedHeaderForHttp(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
|
0 | 234 |
void rateControl_data(); |
235 |
void rateControl(); |
|
236 |
||
237 |
void downloadProgress_data(); |
|
238 |
void downloadProgress(); |
|
239 |
void uploadProgress_data(); |
|
240 |
void uploadProgress(); |
|
241 |
||
242 |
void chaining_data(); |
|
243 |
void chaining(); |
|
244 |
||
245 |
void receiveCookiesFromHttp_data(); |
|
246 |
void receiveCookiesFromHttp(); |
|
247 |
void sendCookies_data(); |
|
248 |
void sendCookies(); |
|
249 |
||
250 |
void nestedEventLoops(); |
|
251 |
||
252 |
void httpProxyCommands_data(); |
|
253 |
void httpProxyCommands(); |
|
254 |
void proxyChange(); |
|
255 |
void authorizationError_data(); |
|
256 |
void authorizationError(); |
|
257 |
||
258 |
void httpConnectionCount(); |
|
259 |
||
260 |
#ifndef QT_NO_OPENSSL |
|
261 |
void ioPostToHttpsUploadProgress(); |
|
262 |
void ignoreSslErrorsList_data(); |
|
263 |
void ignoreSslErrorsList(); |
|
264 |
void ignoreSslErrorsListWithSlot_data(); |
|
265 |
void ignoreSslErrorsListWithSlot(); |
|
266 |
#endif |
|
267 |
}; |
|
268 |
||
269 |
QT_BEGIN_NAMESPACE |
|
270 |
||
271 |
namespace QTest { |
|
272 |
template<> |
|
273 |
char *toString(const QNetworkReply::NetworkError& code) |
|
274 |
{ |
|
275 |
const QMetaObject *mo = &QNetworkReply::staticMetaObject; |
|
276 |
int index = mo->indexOfEnumerator("NetworkError"); |
|
277 |
if (index == -1) |
|
278 |
return qstrdup(""); |
|
279 |
||
280 |
QMetaEnum qme = mo->enumerator(index); |
|
281 |
return qstrdup(qme.valueToKey(code)); |
|
282 |
} |
|
283 |
||
284 |
template<> |
|
285 |
char *toString(const QNetworkCookie &cookie) |
|
286 |
{ |
|
287 |
return qstrdup(cookie.toRawForm()); |
|
288 |
} |
|
289 |
||
290 |
template<> |
|
291 |
char *toString(const QList<QNetworkCookie> &list) |
|
292 |
{ |
|
293 |
QString result = "QList("; |
|
294 |
bool first = true; |
|
295 |
foreach (QNetworkCookie cookie, list) { |
|
296 |
if (!first) |
|
297 |
result += ", "; |
|
298 |
first = false; |
|
299 |
result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm())); |
|
300 |
} |
|
301 |
||
302 |
return qstrdup(result.append(')').toLocal8Bit()); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
QT_END_NAMESPACE |
|
307 |
||
308 |
#define RUN_REQUEST(call) \ |
|
309 |
do { \ |
|
310 |
QString errorMsg = call; \ |
|
311 |
if (!errorMsg.isEmpty()) \ |
|
312 |
QFAIL(qPrintable(errorMsg)); \ |
|
313 |
} while (0); |
|
314 |
||
315 |
class MiniHttpServer: public QTcpServer |
|
316 |
{ |
|
317 |
Q_OBJECT |
|
318 |
QTcpSocket *client; |
|
319 |
||
320 |
public: |
|
321 |
QByteArray dataToTransmit; |
|
322 |
QByteArray receivedData; |
|
323 |
bool doClose; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
324 |
int totalConnections; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
325 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
326 |
MiniHttpServer(const QByteArray &data) : client(0), dataToTransmit(data), doClose(true), totalConnections(0) |
0 | 327 |
{ |
328 |
listen(); |
|
329 |
connect(this, SIGNAL(newConnection()), this, SLOT(doAccept())); |
|
330 |
} |
|
331 |
||
332 |
public slots: |
|
333 |
void doAccept() |
|
334 |
{ |
|
335 |
client = nextPendingConnection(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
336 |
++totalConnections; |
0 | 337 |
connect(client, SIGNAL(readyRead()), this, SLOT(sendData())); |
338 |
} |
|
339 |
||
340 |
void sendData() |
|
341 |
{ |
|
342 |
receivedData += client->readAll(); |
|
343 |
if (receivedData.contains("\r\n\r\n") || |
|
344 |
receivedData.contains("\n\n")) { |
|
345 |
client->write(dataToTransmit); |
|
346 |
if (doClose) { |
|
347 |
client->disconnectFromHost(); |
|
348 |
disconnect(client, 0, this, 0); |
|
349 |
client = 0; |
|
350 |
} |
|
351 |
} |
|
352 |
} |
|
353 |
}; |
|
354 |
||
355 |
class MyCookieJar: public QNetworkCookieJar |
|
356 |
{ |
|
357 |
public: |
|
358 |
inline QList<QNetworkCookie> allCookies() const |
|
359 |
{ return QNetworkCookieJar::allCookies(); } |
|
360 |
inline void setAllCookies(const QList<QNetworkCookie> &cookieList) |
|
361 |
{ QNetworkCookieJar::setAllCookies(cookieList); } |
|
362 |
}; |
|
363 |
||
364 |
class MyProxyFactory: public QNetworkProxyFactory |
|
365 |
{ |
|
366 |
public: |
|
367 |
int callCount; |
|
368 |
QList<QNetworkProxy> toReturn; |
|
369 |
QNetworkProxyQuery lastQuery; |
|
370 |
inline MyProxyFactory() { clear(); } |
|
371 |
||
372 |
inline void clear() |
|
373 |
{ |
|
374 |
callCount = 0; |
|
375 |
toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy; |
|
376 |
lastQuery = QNetworkProxyQuery(); |
|
377 |
} |
|
378 |
||
379 |
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query) |
|
380 |
{ |
|
381 |
lastQuery = query; |
|
382 |
++callCount; |
|
383 |
return toReturn; |
|
384 |
} |
|
385 |
}; |
|
386 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
387 |
class MyMemoryCache: public QAbstractNetworkCache |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
388 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
389 |
public: |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
390 |
typedef QPair<QNetworkCacheMetaData, QByteArray> CachedContent; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
391 |
typedef QHash<QByteArray, CachedContent> CacheData; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
392 |
CacheData cache; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
393 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
394 |
MyMemoryCache(QObject *parent) : QAbstractNetworkCache(parent) {} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
395 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
396 |
QNetworkCacheMetaData metaData(const QUrl &url) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
397 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
398 |
return cache.value(url.toEncoded()).first; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
399 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
400 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
401 |
void updateMetaData(const QNetworkCacheMetaData &metaData) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
402 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
403 |
cache[metaData.url().toEncoded()].first = metaData; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
404 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
405 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
406 |
QIODevice *data(const QUrl &url) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
407 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
408 |
CacheData::ConstIterator it = cache.find(url.toEncoded()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
409 |
if (it == cache.constEnd()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
410 |
return 0; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
411 |
QBuffer *io = new QBuffer(this); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
412 |
io->setData(it->second); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
413 |
io->open(QIODevice::ReadOnly); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
414 |
io->seek(0); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
415 |
return io; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
416 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
417 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
418 |
bool remove(const QUrl &url) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
419 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
420 |
cache.remove(url.toEncoded()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
421 |
return true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
422 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
423 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
424 |
qint64 cacheSize() const |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
425 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
426 |
qint64 total = 0; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
427 |
foreach (const CachedContent &entry, cache) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
428 |
total += entry.second.size(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
429 |
return total; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
430 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
431 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
432 |
QIODevice *prepare(const QNetworkCacheMetaData &) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
433 |
{ Q_ASSERT(0 && "Should not have tried to add to the cache"); return 0; } |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
434 |
void insert(QIODevice *) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
435 |
{ Q_ASSERT(0 && "Should not have tried to add to the cache"); } |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
436 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
437 |
void clear() { cache.clear(); } |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
438 |
}; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
439 |
Q_DECLARE_METATYPE(MyMemoryCache::CachedContent) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
440 |
Q_DECLARE_METATYPE(MyMemoryCache::CacheData) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
441 |
|
0 | 442 |
class DataReader: public QObject |
443 |
{ |
|
444 |
Q_OBJECT |
|
445 |
public: |
|
446 |
qint64 totalBytes; |
|
447 |
QByteArray data; |
|
448 |
QIODevice *device; |
|
449 |
bool accumulate; |
|
450 |
DataReader(QIODevice *dev, bool acc = true) : totalBytes(0), device(dev), accumulate(acc) |
|
451 |
{ |
|
452 |
connect(device, SIGNAL(readyRead()), SLOT(doRead())); |
|
453 |
} |
|
454 |
||
455 |
public slots: |
|
456 |
void doRead() |
|
457 |
{ |
|
458 |
QByteArray buffer; |
|
459 |
buffer.resize(device->bytesAvailable()); |
|
460 |
qint64 bytesRead = device->read(buffer.data(), device->bytesAvailable()); |
|
461 |
if (bytesRead == -1) { |
|
462 |
QTestEventLoop::instance().exitLoop(); |
|
463 |
return; |
|
464 |
} |
|
465 |
buffer.truncate(bytesRead); |
|
466 |
totalBytes += bytesRead; |
|
467 |
||
468 |
if (accumulate) |
|
469 |
data += buffer; |
|
470 |
} |
|
471 |
}; |
|
472 |
||
473 |
||
474 |
class SocketPair: public QObject |
|
475 |
{ |
|
476 |
Q_OBJECT |
|
477 |
public: |
|
478 |
QIODevice *endPoints[2]; |
|
479 |
||
480 |
SocketPair(QObject *parent = 0) |
|
481 |
: QObject(parent) |
|
482 |
{ |
|
483 |
endPoints[0] = endPoints[1] = 0; |
|
484 |
} |
|
485 |
||
486 |
bool create() |
|
487 |
{ |
|
488 |
QTcpServer server; |
|
489 |
server.listen(); |
|
490 |
||
491 |
QTcpSocket *active = new QTcpSocket(this); |
|
492 |
active->connectToHost("127.0.0.1", server.serverPort()); |
|
493 |
#ifndef Q_OS_SYMBIAN |
|
494 |
// need more time as working with embedded |
|
495 |
// device and testing from emualtor |
|
496 |
// things tend to get slower |
|
497 |
if (!active->waitForConnected(1000)) |
|
498 |
return false; |
|
499 |
||
500 |
if (!server.waitForNewConnection(1000)) |
|
501 |
return false; |
|
502 |
#else |
|
503 |
if (!active->waitForConnected(100)) |
|
504 |
return false; |
|
505 |
||
506 |
if (!server.waitForNewConnection(100)) |
|
507 |
return false; |
|
508 |
#endif |
|
509 |
QTcpSocket *passive = server.nextPendingConnection(); |
|
510 |
passive->setParent(this); |
|
511 |
||
512 |
endPoints[0] = active; |
|
513 |
endPoints[1] = passive; |
|
514 |
return true; |
|
515 |
} |
|
516 |
}; |
|
517 |
||
518 |
class FastSender: public QThread |
|
519 |
{ |
|
520 |
Q_OBJECT |
|
521 |
QSemaphore ready; |
|
522 |
qint64 wantedSize; |
|
523 |
int port; |
|
524 |
public: |
|
525 |
int transferRate; |
|
526 |
QWaitCondition cond; |
|
527 |
FastSender(qint64 size) |
|
528 |
: wantedSize(size), port(-1), transferRate(-1) |
|
529 |
{ |
|
530 |
start(); |
|
531 |
ready.acquire(); |
|
532 |
} |
|
533 |
||
534 |
inline int serverPort() const { return port; } |
|
535 |
||
536 |
protected: |
|
537 |
void run() |
|
538 |
{ |
|
539 |
QTcpServer server; |
|
540 |
server.listen(); |
|
541 |
port = server.serverPort(); |
|
542 |
ready.release(); |
|
543 |
||
544 |
server.waitForNewConnection(-1); |
|
545 |
QTcpSocket *client = server.nextPendingConnection(); |
|
546 |
||
547 |
// get the "request" packet |
|
548 |
if (!client->waitForReadyRead(2000)) { |
|
549 |
qDebug() << client->error() << "waiting for \"request\" packet"; |
|
550 |
return; |
|
551 |
} |
|
552 |
client->readAll(); // we're not interested in the actual contents |
|
553 |
||
554 |
enum { BlockSize = 256 }; |
|
555 |
QByteArray data; |
|
556 |
{ |
|
557 |
QDataStream stream(&data, QIODevice::WriteOnly); |
|
558 |
stream << QVariantMap() << QByteArray(BlockSize, 'a'); |
|
559 |
} |
|
560 |
qint32 size = data.size(); |
|
561 |
||
562 |
// write a bunch of bytes to fill up the buffers |
|
563 |
do { |
|
564 |
client->write((char*)&size, sizeof size); |
|
565 |
client->write(data); |
|
566 |
while (client->bytesToWrite() > 0) |
|
567 |
if (!client->waitForBytesWritten(0)) |
|
568 |
break; |
|
569 |
} while (client->bytesToWrite() == 0); |
|
570 |
||
571 |
// the kernel buffer is full |
|
572 |
// clean up QAbstractSocket's residue: |
|
573 |
while (client->bytesToWrite() > 0) |
|
574 |
if (!client->waitForBytesWritten(2000)) { |
|
575 |
qDebug() << client->error() << "cleaning up residue"; |
|
576 |
return; |
|
577 |
} |
|
578 |
||
579 |
// now write in "blocking mode" |
|
580 |
QTime timer; |
|
581 |
timer.start(); |
|
582 |
qint64 totalBytes = 0; |
|
583 |
while (totalBytes < wantedSize) { |
|
584 |
int bytesToWrite = wantedSize - totalBytes; |
|
585 |
Q_ASSERT(bytesToWrite); |
|
586 |
if (bytesToWrite > BlockSize) { |
|
587 |
bytesToWrite = BlockSize; |
|
588 |
} else { |
|
589 |
QDataStream stream(&data, QIODevice::WriteOnly); |
|
590 |
stream << QVariantMap() << QByteArray(bytesToWrite, 'b'); |
|
591 |
} |
|
592 |
size = data.size(); |
|
593 |
client->write((char*)&size, sizeof size); |
|
594 |
client->write(data); |
|
595 |
totalBytes += bytesToWrite; |
|
596 |
||
597 |
while (client->bytesToWrite() > 0) |
|
598 |
if (!client->waitForBytesWritten(2000)) { |
|
599 |
qDebug() << client->error() << "blocking write"; |
|
600 |
return; |
|
601 |
} |
|
602 |
// qDebug() << bytesToWrite << "bytes written now;" |
|
603 |
// << totalBytes << "total (" |
|
604 |
// << totalBytes*100/wantedSize << "% complete);" |
|
605 |
// << timer.elapsed() << "ms elapsed"; |
|
606 |
} |
|
607 |
||
608 |
transferRate = totalBytes * 1000 / timer.elapsed(); |
|
609 |
qDebug() << "flushed" << totalBytes << "bytes in" << timer.elapsed() << "ms: rate =" << transferRate; |
|
610 |
||
611 |
// write a "close connection" packet |
|
612 |
{ |
|
613 |
QDataStream stream(&data, QIODevice::WriteOnly); |
|
614 |
stream << QVariantMap() << QByteArray(); |
|
615 |
} |
|
616 |
size = data.size(); |
|
617 |
client->write((char*)&size, sizeof size); |
|
618 |
client->write(data); |
|
619 |
} |
|
620 |
}; |
|
621 |
||
622 |
class RateControlledReader: public QObject |
|
623 |
{ |
|
624 |
Q_OBJECT |
|
625 |
QIODevice *device; |
|
626 |
int bytesToRead; |
|
627 |
int interval; |
|
628 |
public: |
|
629 |
qint64 totalBytesRead; |
|
630 |
RateControlledReader(QIODevice *dev, int kbPerSec) |
|
631 |
: device(dev), totalBytesRead(0) |
|
632 |
{ |
|
633 |
// determine how often we have to wake up |
|
634 |
bytesToRead = kbPerSec * 1024 / 20; |
|
635 |
interval = 50; |
|
636 |
||
637 |
qDebug() << "RateControlledReader: going to read" << bytesToRead |
|
638 |
<< "bytes every" << interval << "ms"; |
|
639 |
qDebug() << "actual rate will be" |
|
640 |
<< (bytesToRead * 1000 / interval) << "bytes/sec (wanted" |
|
641 |
<< kbPerSec * 1024 << "bytes/sec)"; |
|
642 |
startTimer(interval); |
|
643 |
} |
|
644 |
||
645 |
protected: |
|
646 |
void timerEvent(QTimerEvent *) |
|
647 |
{ |
|
648 |
qint64 bytesRead = 0; |
|
649 |
QTime stopWatch; |
|
650 |
stopWatch.start(); |
|
651 |
do { |
|
652 |
if (device->bytesAvailable() == 0) |
|
653 |
if (stopWatch.elapsed() > 10 || !device->waitForReadyRead(5)) |
|
654 |
break; |
|
655 |
QByteArray data = device->read(bytesToRead - bytesRead); |
|
656 |
bytesRead += data.size(); |
|
657 |
} while (bytesRead < bytesToRead);// && stopWatch.elapsed() < interval/4); |
|
658 |
totalBytesRead += bytesRead; |
|
659 |
||
660 |
if (bytesRead < bytesToRead) |
|
661 |
qWarning() << bytesToRead - bytesRead << "bytes not read"; |
|
662 |
} |
|
663 |
}; |
|
664 |
||
665 |
||
666 |
tst_QNetworkReply::tst_QNetworkReply() |
|
667 |
{ |
|
668 |
Q_SET_DEFAULT_IAP |
|
669 |
||
670 |
testFileName = QDir::currentPath() + "/testfile"; |
|
671 |
#ifndef Q_OS_WINCE |
|
672 |
uniqueExtension = QString("%1%2%3").arg((qulonglong)this).arg(rand()).arg((qulonglong)time(0)); |
|
673 |
#else |
|
674 |
uniqueExtension = QString("%1%2").arg((qulonglong)this).arg(rand()); |
|
675 |
#endif |
|
676 |
cookieJar = new MyCookieJar; |
|
677 |
manager.setCookieJar(cookieJar); |
|
678 |
||
679 |
QHostInfo hostInfo = QHostInfo::fromName(QtNetworkSettings::serverName()); |
|
680 |
||
681 |
proxies << ProxyData(QNetworkProxy::NoProxy, "", false); |
|
682 |
||
683 |
if (hostInfo.error() == QHostInfo::NoError && !hostInfo.addresses().isEmpty()) { |
|
684 |
QString proxyserver = hostInfo.addresses().first().toString(); |
|
685 |
proxies << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3128), "+proxy", false) |
|
686 |
<< ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3129), "+proxyauth", true) |
|
687 |
// currently unsupported |
|
688 |
// << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3130), "+proxyauth-ntlm", true); |
|
689 |
<< ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1080), "+socks", false) |
|
690 |
<< ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1081), "+socksauth", true); |
|
691 |
} else { |
|
692 |
printf("==================================================================\n"); |
|
693 |
printf("Proxy could not be looked up. No proxy will be used while testing!\n"); |
|
694 |
printf("==================================================================\n"); |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
tst_QNetworkReply::~tst_QNetworkReply() |
|
699 |
{ |
|
700 |
} |
|
701 |
||
702 |
||
703 |
void tst_QNetworkReply::authenticationRequired(QNetworkReply*, QAuthenticator* auth) |
|
704 |
{ |
|
705 |
auth->setUser("httptest"); |
|
706 |
auth->setPassword("httptest"); |
|
707 |
} |
|
708 |
||
709 |
void tst_QNetworkReply::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator* auth) |
|
710 |
{ |
|
711 |
auth->setUser("qsockstest"); |
|
712 |
auth->setPassword("password"); |
|
713 |
} |
|
714 |
||
715 |
#ifndef QT_NO_OPENSSL |
|
716 |
void tst_QNetworkReply::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors) |
|
717 |
{ |
|
718 |
reply->ignoreSslErrors(); |
|
719 |
QVERIFY(!errors.isEmpty()); |
|
720 |
QVERIFY(!reply->sslConfiguration().isNull()); |
|
721 |
} |
|
722 |
||
723 |
void tst_QNetworkReply::storeSslConfiguration() |
|
724 |
{ |
|
725 |
storedSslConfiguration = QSslConfiguration(); |
|
726 |
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender()); |
|
727 |
if (reply) |
|
728 |
storedSslConfiguration = reply->sslConfiguration(); |
|
729 |
} |
|
730 |
#endif |
|
731 |
||
732 |
QString tst_QNetworkReply::runSimpleRequest(QNetworkAccessManager::Operation op, |
|
733 |
const QNetworkRequest &request, |
|
734 |
QNetworkReplyPtr &reply, |
|
735 |
const QByteArray &data) |
|
736 |
{ |
|
737 |
switch (op) { |
|
738 |
case QNetworkAccessManager::HeadOperation: |
|
739 |
reply = manager.head(request); |
|
740 |
break; |
|
741 |
||
742 |
case QNetworkAccessManager::GetOperation: |
|
743 |
reply = manager.get(request); |
|
744 |
break; |
|
745 |
||
746 |
case QNetworkAccessManager::PutOperation: |
|
747 |
reply = manager.put(request, data); |
|
748 |
break; |
|
749 |
||
750 |
case QNetworkAccessManager::PostOperation: |
|
751 |
reply = manager.post(request, data); |
|
752 |
break; |
|
753 |
||
754 |
case QNetworkAccessManager::DeleteOperation: |
|
755 |
reply = manager.deleteResource(request); |
|
756 |
break; |
|
757 |
||
758 |
default: |
|
759 |
Q_ASSERT_X(false, "tst_QNetworkReply", "Invalid/unknown operation requested"); |
|
760 |
} |
|
761 |
reply->setParent(this); |
|
762 |
connect(reply, SIGNAL(finished()), SLOT(finished())); |
|
763 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(gotError())); |
|
764 |
||
765 |
returnCode = Timeout; |
|
766 |
loop = new QEventLoop; |
|
767 |
QTimer::singleShot(20000, loop, SLOT(quit())); |
|
768 |
int code = returnCode == Timeout ? loop->exec() : returnCode; |
|
769 |
delete loop; |
|
770 |
loop = 0; |
|
771 |
||
772 |
switch (code) { |
|
773 |
case Failure: |
|
774 |
return "Request failed: " + reply->errorString(); |
|
775 |
case Timeout: |
|
776 |
return "Network timeout"; |
|
777 |
} |
|
778 |
return QString(); |
|
779 |
} |
|
780 |
||
781 |
void tst_QNetworkReply::finished() |
|
782 |
{ |
|
783 |
loop->exit(returnCode = Success); |
|
784 |
} |
|
785 |
||
786 |
void tst_QNetworkReply::gotError() |
|
787 |
{ |
|
788 |
loop->exit(returnCode = Failure); |
|
789 |
disconnect(QObject::sender(), SIGNAL(finished()), this, 0); |
|
790 |
} |
|
791 |
||
792 |
void tst_QNetworkReply::initTestCase() |
|
793 |
{ |
|
794 |
#if !defined Q_OS_WIN |
|
795 |
wronlyFileName = QDir::currentPath() + "/write-only"; |
|
796 |
QFile wr(wronlyFileName); |
|
797 |
QVERIFY(wr.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
798 |
wr.setPermissions(QFile::WriteOwner | QFile::WriteUser); |
|
799 |
wr.close(); |
|
800 |
#endif |
|
801 |
||
802 |
QDir::setSearchPaths("srcdir", QStringList() << SRCDIR); |
|
803 |
} |
|
804 |
||
805 |
void tst_QNetworkReply::cleanupTestCase() |
|
806 |
{ |
|
807 |
#if !defined Q_OS_WIN |
|
808 |
QFile::remove(wronlyFileName); |
|
809 |
#endif |
|
810 |
} |
|
811 |
||
812 |
void tst_QNetworkReply::init() |
|
813 |
{ |
|
814 |
cleanup(); |
|
815 |
} |
|
816 |
||
817 |
void tst_QNetworkReply::cleanup() |
|
818 |
{ |
|
819 |
QFile file(testFileName); |
|
820 |
QVERIFY(!file.exists() || file.remove()); |
|
821 |
||
822 |
// clear the internal cache |
|
823 |
QNetworkAccessManagerPrivate::clearCache(&manager); |
|
824 |
manager.setProxy(QNetworkProxy()); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
825 |
manager.setCache(0); |
0 | 826 |
|
827 |
// clear cookies |
|
828 |
cookieJar->setAllCookies(QList<QNetworkCookie>()); |
|
829 |
} |
|
830 |
||
831 |
void tst_QNetworkReply::stateChecking() |
|
832 |
{ |
|
833 |
QUrl url = QUrl("file:///"); |
|
834 |
QNetworkRequest req(url); // you can't open this file, I know |
|
835 |
QNetworkReplyPtr reply = manager.get(req); |
|
836 |
||
837 |
QVERIFY(reply.data()); |
|
838 |
QVERIFY(reply->isOpen()); |
|
839 |
QVERIFY(reply->isReadable()); |
|
840 |
QVERIFY(!reply->isWritable()); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
841 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
842 |
// both behaviours are OK since we might change underlying behaviour again |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
843 |
if (!reply->isFinished()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
844 |
QCOMPARE(reply->errorString(), QString("Unknown error")); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
845 |
else |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
846 |
QVERIFY(!reply->errorString().isEmpty()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
847 |
|
0 | 848 |
|
849 |
QCOMPARE(reply->manager(), &manager); |
|
850 |
QCOMPARE(reply->request(), req); |
|
851 |
QCOMPARE(int(reply->operation()), int(QNetworkAccessManager::GetOperation)); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
852 |
// error and not error are OK since we might change underlying behaviour again |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
853 |
if (!reply->isFinished()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
854 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
0 | 855 |
QCOMPARE(reply->url(), url); |
856 |
||
857 |
reply->abort(); |
|
858 |
} |
|
859 |
||
860 |
void tst_QNetworkReply::invalidProtocol() |
|
861 |
{ |
|
862 |
QUrl url = QUrl::fromEncoded("not-a-known-protocol://foo/bar"); |
|
863 |
QNetworkRequest req(url); |
|
864 |
QNetworkReplyPtr reply; |
|
865 |
||
866 |
QString errorMsg = "Request failed: Protocol \"not-a-known-protocol\" is unknown"; |
|
867 |
QString result = runSimpleRequest(QNetworkAccessManager::GetOperation, req, reply); |
|
868 |
QCOMPARE(result, errorMsg); |
|
869 |
||
870 |
QCOMPARE(reply->url(), url); |
|
871 |
QCOMPARE(reply->error(), QNetworkReply::ProtocolUnknownError); |
|
872 |
} |
|
873 |
||
874 |
void tst_QNetworkReply::getFromData_data() |
|
875 |
{ |
|
876 |
QTest::addColumn<QString>("request"); |
|
877 |
QTest::addColumn<QByteArray>("expected"); |
|
878 |
QTest::addColumn<QString>("mimeType"); |
|
879 |
||
880 |
const QString defaultMimeType("text/plain;charset=US-ASCII"); |
|
881 |
||
882 |
//QTest::newRow("empty") << "data:" << QByteArray() << defaultMimeType; |
|
883 |
QTest::newRow("empty2") << "data:," << QByteArray() << defaultMimeType; |
|
884 |
QTest::newRow("just-charset_1") << "data:charset=iso-8859-1," |
|
885 |
<< QByteArray() << "text/plain;charset=iso-8859-1"; |
|
886 |
QTest::newRow("just-charset_2") << "data:charset = iso-8859-1 ," |
|
887 |
<< QByteArray() << "text/plain;charset = iso-8859-1"; |
|
888 |
//QTest::newRow("just-media") << "data:text/xml" << QByteArray() << "text/xml"; |
|
889 |
QTest::newRow("just-media2") << "data:text/xml," << QByteArray() << "text/xml"; |
|
890 |
||
891 |
QTest::newRow("plain_1") << "data:,foo" << QByteArray("foo") << defaultMimeType; |
|
892 |
QTest::newRow("plain_2") << "data:text/html,Hello World" << QByteArray("Hello World") |
|
893 |
<< "text/html"; |
|
894 |
QTest::newRow("plain_3") << "data:text/html;charset=utf-8,Hello World" |
|
895 |
<< QByteArray("Hello World") << "text/html;charset=utf-8"; |
|
896 |
||
897 |
QTest::newRow("pct_1") << "data:,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A" |
|
898 |
<< QByteArray("<body contentEditable=true>\r\n") << defaultMimeType; |
|
899 |
QTest::newRow("pct_2") << "data:text/html;charset=utf-8,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A" |
|
900 |
<< QByteArray("<body contentEditable=true>\r\n") |
|
901 |
<< "text/html;charset=utf-8"; |
|
902 |
||
903 |
QTest::newRow("base64-empty_1") << "data:;base64," << QByteArray() << defaultMimeType; |
|
904 |
QTest::newRow("base64-empty_2") << "data:charset=utf-8;base64," << QByteArray() |
|
905 |
<< "text/plain;charset=utf-8"; |
|
906 |
QTest::newRow("base64-empty_3") << "data:text/html;charset=utf-8;base64," |
|
907 |
<< QByteArray() << "text/html;charset=utf-8"; |
|
908 |
||
909 |
QTest::newRow("base64_1") << "data:;base64,UXQgaXMgZ3JlYXQh" << QByteArray("Qt is great!") |
|
910 |
<< defaultMimeType; |
|
911 |
QTest::newRow("base64_2") << "data:charset=utf-8;base64,UXQgaXMgZ3JlYXQh" |
|
912 |
<< QByteArray("Qt is great!") << "text/plain;charset=utf-8"; |
|
913 |
QTest::newRow("base64_3") << "data:text/html;charset=utf-8;base64,UXQgaXMgZ3JlYXQh" |
|
914 |
<< QByteArray("Qt is great!") << "text/html;charset=utf-8"; |
|
915 |
||
916 |
QTest::newRow("pct-nul") << "data:,a%00g" << QByteArray("a\0g", 3) << defaultMimeType; |
|
917 |
QTest::newRow("base64-nul") << "data:;base64,YQBn" << QByteArray("a\0g", 3) << defaultMimeType; |
|
918 |
QTest::newRow("pct-nonutf8") << "data:,a%E1g" << QByteArray("a\xE1g", 3) << defaultMimeType; |
|
919 |
||
920 |
QTest::newRow("base64") |
|
921 |
<< QString::fromLatin1("data:application/xml;base64,PGUvPg==") |
|
922 |
<< QByteArray("<e/>") |
|
923 |
<< "application/xml"; |
|
924 |
||
925 |
QTest::newRow("base64, no media type") |
|
926 |
<< QString::fromLatin1("data:;base64,PGUvPg==") |
|
927 |
<< QByteArray("<e/>") |
|
928 |
<< defaultMimeType; |
|
929 |
||
930 |
QTest::newRow("Percent encoding") |
|
931 |
<< QString::fromLatin1("data:application/xml,%3Ce%2F%3E") |
|
932 |
<< QByteArray("<e/>") |
|
933 |
<< "application/xml"; |
|
934 |
||
935 |
QTest::newRow("Percent encoding, no media type") |
|
936 |
<< QString::fromLatin1("data:,%3Ce%2F%3E") |
|
937 |
<< QByteArray("<e/>") |
|
938 |
<< defaultMimeType; |
|
939 |
||
940 |
QTest::newRow("querychars") |
|
941 |
<< QString::fromLatin1("data:,foo?x=0&y=0") |
|
942 |
<< QByteArray("foo?x=0&y=0") |
|
943 |
<< defaultMimeType; |
|
944 |
||
945 |
QTest::newRow("css") << "data:text/css,div%20{%20border-right:%20solid;%20}" |
|
946 |
<< QByteArray("div { border-right: solid; }") |
|
947 |
<< "text/css"; |
|
948 |
} |
|
949 |
||
950 |
void tst_QNetworkReply::getFromData() |
|
951 |
{ |
|
952 |
QFETCH(QString, request); |
|
953 |
QFETCH(QByteArray, expected); |
|
954 |
QFETCH(QString, mimeType); |
|
955 |
||
956 |
QUrl url = QUrl::fromEncoded(request.toLatin1()); |
|
957 |
QNetworkRequest req(url); |
|
958 |
QNetworkReplyPtr reply; |
|
959 |
||
960 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, req, reply)); |
|
961 |
||
962 |
QCOMPARE(reply->url(), url); |
|
963 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
964 |
||
965 |
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toString(), mimeType); |
|
966 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(expected.size())); |
|
967 |
QCOMPARE(reply->readAll(), expected); |
|
968 |
} |
|
969 |
||
970 |
void tst_QNetworkReply::getFromFile() |
|
971 |
{ |
|
972 |
// create the file: |
|
973 |
QTemporaryFile file(QDir::currentPath() + "/temp-XXXXXX"); |
|
974 |
file.setAutoRemove(true); |
|
975 |
QVERIFY(file.open()); |
|
976 |
||
977 |
QNetworkRequest request(QUrl::fromLocalFile(file.fileName())); |
|
978 |
QNetworkReplyPtr reply; |
|
979 |
||
980 |
static const char fileData[] = "This is some data that is in the file.\r\n"; |
|
981 |
QByteArray data = QByteArray::fromRawData(fileData, sizeof fileData - 1); |
|
982 |
QVERIFY(file.write(data) == data.size()); |
|
983 |
file.flush(); |
|
984 |
QCOMPARE(file.size(), qint64(data.size())); |
|
985 |
||
986 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
987 |
||
988 |
QCOMPARE(reply->url(), request.url()); |
|
989 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
990 |
||
991 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size()); |
|
992 |
QCOMPARE(reply->readAll(), data); |
|
993 |
||
994 |
// make the file bigger |
|
995 |
file.resize(0); |
|
996 |
const int multiply = (128 * 1024) / (sizeof fileData - 1); |
|
997 |
for (int i = 0; i < multiply; ++i) |
|
998 |
file.write(fileData, sizeof fileData - 1); |
|
999 |
file.flush(); |
|
1000 |
||
1001 |
// run again |
|
1002 |
reply = 0; |
|
1003 |
||
1004 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
1005 |
QCOMPARE(reply->url(), request.url()); |
|
1006 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1007 |
||
1008 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size()); |
|
1009 |
QCOMPARE(qint64(reply->readAll().size()), file.size()); |
|
1010 |
} |
|
1011 |
||
1012 |
void tst_QNetworkReply::getFromFileSpecial_data() |
|
1013 |
{ |
|
1014 |
QTest::addColumn<QString>("fileName"); |
|
1015 |
QTest::addColumn<QString>("url"); |
|
1016 |
||
1017 |
QTest::newRow("resource") << ":/resource" << "qrc:/resource"; |
|
1018 |
QTest::newRow("search-path") << "srcdir:/rfc3252.txt" << "srcdir:/rfc3252.txt"; |
|
1019 |
QTest::newRow("bigfile-path") << "srcdir:/bigfile" << "srcdir:/bigfile"; |
|
1020 |
} |
|
1021 |
||
1022 |
void tst_QNetworkReply::getFromFileSpecial() |
|
1023 |
{ |
|
1024 |
QFETCH(QString, fileName); |
|
1025 |
QFETCH(QString, url); |
|
1026 |
||
1027 |
// open the resource so we can find out its size |
|
1028 |
QFile resource(fileName); |
|
1029 |
QVERIFY(resource.open(QIODevice::ReadOnly)); |
|
1030 |
||
1031 |
QNetworkRequest request; |
|
1032 |
QNetworkReplyPtr reply; |
|
1033 |
request.setUrl(url); |
|
1034 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
1035 |
||
1036 |
QCOMPARE(reply->url(), request.url()); |
|
1037 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1038 |
||
1039 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), resource.size()); |
|
1040 |
QCOMPARE(reply->readAll(), resource.readAll()); |
|
1041 |
} |
|
1042 |
||
1043 |
void tst_QNetworkReply::getFromFtp_data() |
|
1044 |
{ |
|
1045 |
QTest::addColumn<QString>("referenceName"); |
|
1046 |
QTest::addColumn<QString>("url"); |
|
1047 |
||
1048 |
QTest::newRow("rfc3252.txt") << SRCDIR "/rfc3252.txt" << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"; |
|
1049 |
QTest::newRow("bigfile") << SRCDIR "/bigfile" << "ftp://" + QtNetworkSettings::serverName() + "/qtest/bigfile"; |
|
1050 |
} |
|
1051 |
||
1052 |
void tst_QNetworkReply::getFromFtp() |
|
1053 |
{ |
|
1054 |
QFETCH(QString, referenceName); |
|
1055 |
QFETCH(QString, url); |
|
1056 |
||
1057 |
QFile reference(referenceName); |
|
1058 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1059 |
||
1060 |
QNetworkRequest request(url); |
|
1061 |
QNetworkReplyPtr reply; |
|
1062 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
1063 |
||
1064 |
QCOMPARE(reply->url(), request.url()); |
|
1065 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1066 |
||
1067 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1068 |
QCOMPARE(reply->readAll(), reference.readAll()); |
|
1069 |
} |
|
1070 |
||
1071 |
void tst_QNetworkReply::getFromHttp_data() |
|
1072 |
{ |
|
1073 |
QTest::addColumn<QString>("referenceName"); |
|
1074 |
QTest::addColumn<QString>("url"); |
|
1075 |
||
1076 |
QTest::newRow("success-internal") << SRCDIR "/rfc3252.txt" << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"; |
|
1077 |
QTest::newRow("success-external") << SRCDIR "/rfc3252.txt" << "http://www.ietf.org/rfc/rfc3252.txt"; |
|
1078 |
QTest::newRow("bigfile-internal") << SRCDIR "/bigfile" << "http://" + QtNetworkSettings::serverName() + "/qtest/bigfile"; |
|
1079 |
} |
|
1080 |
||
1081 |
void tst_QNetworkReply::getFromHttp() |
|
1082 |
{ |
|
1083 |
QFETCH(QString, referenceName); |
|
1084 |
QFETCH(QString, url); |
|
1085 |
||
1086 |
QFile reference(referenceName); |
|
1087 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1088 |
||
1089 |
QNetworkRequest request(url); |
|
1090 |
QNetworkReplyPtr reply; |
|
1091 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
1092 |
||
1093 |
QCOMPARE(reply->url(), request.url()); |
|
1094 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1095 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1096 |
QCOMPARE(reply->size(), reference.size()); |
|
1097 |
// only compare when the header is set. |
|
1098 |
if (reply->header(QNetworkRequest::ContentLengthHeader).isValid()) |
|
1099 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1100 |
QCOMPARE(reply->readAll(), reference.readAll()); |
|
1101 |
} |
|
1102 |
||
1103 |
void tst_QNetworkReply::getErrors_data() |
|
1104 |
{ |
|
1105 |
QTest::addColumn<QString>("url"); |
|
1106 |
QTest::addColumn<int>("error"); |
|
1107 |
QTest::addColumn<int>("httpStatusCode"); |
|
1108 |
QTest::addColumn<bool>("dataIsEmpty"); |
|
1109 |
||
1110 |
// file: errors |
|
1111 |
QTest::newRow("file-host") << "file://this-host-doesnt-exist.troll.no/foo.txt" |
|
1112 |
#if !defined Q_OS_WIN |
|
1113 |
<< int(QNetworkReply::ProtocolInvalidOperationError) << 0 << true; |
|
1114 |
#else |
|
1115 |
<< int(QNetworkReply::ContentNotFoundError) << 0 << true; |
|
1116 |
#endif |
|
1117 |
QTest::newRow("file-no-path") << "file://localhost" |
|
1118 |
<< int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true; |
|
1119 |
QTest::newRow("file-is-dir") << QUrl::fromLocalFile(QDir::currentPath()).toString() |
|
1120 |
<< int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true; |
|
1121 |
QTest::newRow("file-exist") << QUrl::fromLocalFile(QDir::currentPath() + "/this-file-doesnt-exist.txt").toString() |
|
1122 |
<< int(QNetworkReply::ContentNotFoundError) << 0 << true; |
|
1123 |
#if !defined Q_OS_WIN && !defined(Q_OS_SYMBIAN) |
|
1124 |
QTest::newRow("file-is-wronly") << QUrl::fromLocalFile(wronlyFileName).toString() |
|
1125 |
<< int(QNetworkReply::ContentAccessDenied) << 0 << true; |
|
1126 |
#endif |
|
1127 |
if (QFile::exists("/etc/shadow")) |
|
1128 |
QTest::newRow("file-permissions") << "file:/etc/shadow" |
|
1129 |
<< int(QNetworkReply::ContentAccessDenied) << 0 << true; |
|
1130 |
||
1131 |
// ftp: errors |
|
1132 |
QTest::newRow("ftp-host") << "ftp://this-host-doesnt-exist.troll.no/foo.txt" |
|
1133 |
<< int(QNetworkReply::HostNotFoundError) << 0 << true; |
|
1134 |
QTest::newRow("ftp-no-path") << "ftp://" + QtNetworkSettings::serverName() |
|
1135 |
<< int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true; |
|
1136 |
QTest::newRow("ftp-is-dir") << "ftp://" + QtNetworkSettings::serverName() + "/qtest" |
|
1137 |
<< int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true; |
|
1138 |
QTest::newRow("ftp-dir-not-readable") << "ftp://" + QtNetworkSettings::serverName() + "/pub/dir-not-readable/foo.txt" |
|
1139 |
<< int(QNetworkReply::ContentAccessDenied) << 0 << true; |
|
1140 |
QTest::newRow("ftp-file-not-readable") << "ftp://" + QtNetworkSettings::serverName() + "/pub/file-not-readable.txt" |
|
1141 |
<< int(QNetworkReply::ContentAccessDenied) << 0 << true; |
|
1142 |
QTest::newRow("ftp-exist") << "ftp://" + QtNetworkSettings::serverName() + "/pub/this-file-doesnt-exist.txt" |
|
1143 |
<< int(QNetworkReply::ContentNotFoundError) << 0 << true; |
|
1144 |
||
1145 |
// http: errors |
|
1146 |
QTest::newRow("http-host") << "http://this-host-will-never-exist.troll.no/" |
|
1147 |
<< int(QNetworkReply::HostNotFoundError) << 0 << true; |
|
1148 |
QTest::newRow("http-exist") << "http://" + QtNetworkSettings::serverName() + "/this-file-doesnt-exist.txt" |
|
1149 |
<< int(QNetworkReply::ContentNotFoundError) << 404 << false; |
|
1150 |
QTest::newRow("http-authentication") << "http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth" |
|
1151 |
<< int(QNetworkReply::AuthenticationRequiredError) << 401 << false; |
|
1152 |
} |
|
1153 |
||
1154 |
void tst_QNetworkReply::getErrors() |
|
1155 |
{ |
|
1156 |
QFETCH(QString, url); |
|
1157 |
QNetworkRequest request(url); |
|
1158 |
QNetworkReplyPtr reply = manager.get(request); |
|
1159 |
reply->setParent(this); // we have expect-fails |
|
1160 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1161 |
if (!reply->isFinished()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1162 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
0 | 1163 |
|
1164 |
// now run the request: |
|
1165 |
connect(reply, SIGNAL(finished()), |
|
1166 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1167 |
QTestEventLoop::instance().enterLoop(10); |
|
1168 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1169 |
//qDebug() << reply->errorString(); |
|
1170 |
||
1171 |
QFETCH(int, error); |
|
1172 |
QEXPECT_FAIL("ftp-is-dir", "QFtp cannot provide enough detail", Abort); |
|
1173 |
// the line below is not necessary |
|
1174 |
QEXPECT_FAIL("ftp-dir-not-readable", "QFtp cannot provide enough detail", Abort); |
|
1175 |
QCOMPARE(reply->error(), QNetworkReply::NetworkError(error)); |
|
1176 |
||
1177 |
QTEST(reply->readAll().isEmpty(), "dataIsEmpty"); |
|
1178 |
||
1179 |
QVERIFY(reply->isFinished()); |
|
1180 |
QVERIFY(!reply->isRunning()); |
|
1181 |
||
1182 |
QFETCH(int, httpStatusCode); |
|
1183 |
if (httpStatusCode != 0) { |
|
1184 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), httpStatusCode); |
|
1185 |
} |
|
1186 |
} |
|
1187 |
||
1188 |
static inline QByteArray md5sum(const QByteArray &data) |
|
1189 |
{ |
|
1190 |
return QCryptographicHash::hash(data, QCryptographicHash::Md5); |
|
1191 |
} |
|
1192 |
||
1193 |
void tst_QNetworkReply::putToFile_data() |
|
1194 |
{ |
|
1195 |
QTest::addColumn<QByteArray>("data"); |
|
1196 |
QTest::addColumn<QByteArray>("md5sum"); |
|
1197 |
||
1198 |
QByteArray data; |
|
1199 |
data = ""; |
|
1200 |
QTest::newRow("empty") << data << md5sum(data); |
|
1201 |
||
1202 |
data = "This is a normal message."; |
|
1203 |
QTest::newRow("generic") << data << md5sum(data); |
|
1204 |
||
1205 |
data = "This is a message to show that Qt rocks!\r\n\n"; |
|
1206 |
QTest::newRow("small") << data << md5sum(data); |
|
1207 |
||
1208 |
data = QByteArray("abcd\0\1\2\abcd",12); |
|
1209 |
QTest::newRow("with-nul") << data << md5sum(data); |
|
1210 |
||
1211 |
data = QByteArray(4097, '\4'); |
|
1212 |
QTest::newRow("4k+1") << data << md5sum(data); |
|
1213 |
||
1214 |
data = QByteArray(128*1024+1, '\177'); |
|
1215 |
QTest::newRow("128k+1") << data << md5sum(data); |
|
1216 |
} |
|
1217 |
||
1218 |
void tst_QNetworkReply::putToFile() |
|
1219 |
{ |
|
1220 |
QFile file(testFileName); |
|
1221 |
||
1222 |
QUrl url = QUrl::fromLocalFile(file.fileName()); |
|
1223 |
QNetworkRequest request(url); |
|
1224 |
QNetworkReplyPtr reply; |
|
1225 |
||
1226 |
QFETCH(QByteArray, data); |
|
1227 |
||
1228 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data)); |
|
1229 |
||
1230 |
QCOMPARE(reply->url(), url); |
|
1231 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1232 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
1233 |
QVERIFY(reply->readAll().isEmpty()); |
|
1234 |
||
1235 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
1236 |
QCOMPARE(file.size(), qint64(data.size())); |
|
1237 |
QByteArray contents = file.readAll(); |
|
1238 |
QCOMPARE(contents, data); |
|
1239 |
} |
|
1240 |
||
1241 |
void tst_QNetworkReply::putToFtp_data() |
|
1242 |
{ |
|
1243 |
putToFile_data(); |
|
1244 |
} |
|
1245 |
||
1246 |
void tst_QNetworkReply::putToFtp() |
|
1247 |
{ |
|
1248 |
QUrl url("ftp://" + QtNetworkSettings::serverName()); |
|
1249 |
url.setPath(QString("/qtest/upload/qnetworkaccess-putToFtp-%1-%2") |
|
1250 |
.arg(QTest::currentDataTag()) |
|
1251 |
.arg(uniqueExtension)); |
|
1252 |
||
1253 |
QNetworkRequest request(url); |
|
1254 |
QNetworkReplyPtr reply; |
|
1255 |
||
1256 |
QFETCH(QByteArray, data); |
|
1257 |
||
1258 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data)); |
|
1259 |
||
1260 |
QCOMPARE(reply->url(), url); |
|
1261 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1262 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
1263 |
QVERIFY(reply->readAll().isEmpty()); |
|
1264 |
||
1265 |
// download the file again from FTP to make sure it was uploaded |
|
1266 |
// correctly |
|
1267 |
QFtp ftp; |
|
1268 |
ftp.connectToHost(url.host()); |
|
1269 |
ftp.login(); |
|
1270 |
ftp.get(url.path()); |
|
1271 |
||
1272 |
QObject::connect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1273 |
QTestEventLoop::instance().enterLoop(10); |
|
1274 |
QObject::disconnect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1275 |
||
1276 |
QByteArray uploaded = ftp.readAll(); |
|
1277 |
QCOMPARE(uploaded.size(), data.size()); |
|
1278 |
QCOMPARE(uploaded, data); |
|
1279 |
||
1280 |
ftp.close(); |
|
1281 |
QObject::connect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1282 |
QTestEventLoop::instance().enterLoop(10); |
|
1283 |
QObject::disconnect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1284 |
} |
|
1285 |
||
1286 |
void tst_QNetworkReply::putToHttp_data() |
|
1287 |
{ |
|
1288 |
putToFile_data(); |
|
1289 |
} |
|
1290 |
||
1291 |
void tst_QNetworkReply::putToHttp() |
|
1292 |
{ |
|
1293 |
QUrl url("http://" + QtNetworkSettings::serverName()); |
|
1294 |
url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2") |
|
1295 |
.arg(QTest::currentDataTag()) |
|
1296 |
.arg(uniqueExtension)); |
|
1297 |
||
1298 |
QNetworkRequest request(url); |
|
1299 |
QNetworkReplyPtr reply; |
|
1300 |
||
1301 |
QFETCH(QByteArray, data); |
|
1302 |
||
1303 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data)); |
|
1304 |
||
1305 |
QCOMPARE(reply->url(), url); |
|
1306 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1307 |
||
1308 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 201); // 201 Created |
|
1309 |
||
1310 |
// download the file again from HTTP to make sure it was uploaded |
|
1311 |
// correctly. HTTP/0.9 is enough |
|
1312 |
QTcpSocket socket; |
|
1313 |
socket.connectToHost(QtNetworkSettings::serverName(), 80); |
|
1314 |
socket.write("GET " + url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) + "\r\n"); |
|
1315 |
if (!socket.waitForDisconnected(10000)) |
|
1316 |
QFAIL("Network timeout"); |
|
1317 |
||
1318 |
QByteArray uploadedData = socket.readAll(); |
|
1319 |
QCOMPARE(uploadedData, data); |
|
1320 |
} |
|
1321 |
||
1322 |
void tst_QNetworkReply::postToHttp_data() |
|
1323 |
{ |
|
1324 |
putToFile_data(); |
|
1325 |
} |
|
1326 |
||
1327 |
void tst_QNetworkReply::postToHttp() |
|
1328 |
{ |
|
1329 |
QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi"); |
|
1330 |
||
1331 |
QNetworkRequest request(url); |
|
1332 |
QNetworkReplyPtr reply; |
|
1333 |
||
1334 |
QFETCH(QByteArray, data); |
|
1335 |
||
1336 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data)); |
|
1337 |
||
1338 |
QCOMPARE(reply->url(), url); |
|
1339 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1340 |
||
1341 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok |
|
1342 |
||
1343 |
QFETCH(QByteArray, md5sum); |
|
1344 |
QByteArray uploadedData = reply->readAll().trimmed(); |
|
1345 |
QCOMPARE(uploadedData, md5sum.toHex()); |
|
1346 |
} |
|
1347 |
||
1348 |
void tst_QNetworkReply::deleteFromHttp_data() |
|
1349 |
{ |
|
1350 |
QTest::addColumn<QUrl>("url"); |
|
1351 |
QTest::addColumn<int>("resultCode"); |
|
1352 |
QTest::addColumn<QNetworkReply::NetworkError>("error"); |
|
1353 |
||
1354 |
// for status codes to expect, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html |
|
1355 |
||
1356 |
QTest::newRow("405-method-not-allowed") << QUrl("http://" + QtNetworkSettings::serverName() + "/index.html") << 405 << QNetworkReply::ContentOperationNotPermittedError; |
|
1357 |
QTest::newRow("200-ok") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?200-ok") << 200 << QNetworkReply::NoError; |
|
1358 |
QTest::newRow("202-accepted") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?202-accepted") << 202 << QNetworkReply::NoError; |
|
1359 |
QTest::newRow("204-no-content") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?204-no-content") << 204 << QNetworkReply::NoError; |
|
1360 |
QTest::newRow("404-not-found") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?404-not-found") << 404 << QNetworkReply::ContentNotFoundError; |
|
1361 |
} |
|
1362 |
||
1363 |
void tst_QNetworkReply::deleteFromHttp() |
|
1364 |
{ |
|
1365 |
QFETCH(QUrl, url); |
|
1366 |
QFETCH(int, resultCode); |
|
1367 |
QFETCH(QNetworkReply::NetworkError, error); |
|
1368 |
QNetworkRequest request(url); |
|
1369 |
QNetworkReplyPtr reply; |
|
1370 |
runSimpleRequest(QNetworkAccessManager::DeleteOperation, request, reply, 0); |
|
1371 |
QCOMPARE(reply->url(), url); |
|
1372 |
QCOMPARE(reply->error(), error); |
|
1373 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), resultCode); |
|
1374 |
} |
|
1375 |
||
1376 |
void tst_QNetworkReply::putGetDeleteGetFromHttp_data() |
|
1377 |
{ |
|
1378 |
QTest::addColumn<QUrl>("putUrl"); |
|
1379 |
QTest::addColumn<int>("putResultCode"); |
|
1380 |
QTest::addColumn<QNetworkReply::NetworkError>("putError"); |
|
1381 |
QTest::addColumn<QUrl>("deleteUrl"); |
|
1382 |
QTest::addColumn<int>("deleteResultCode"); |
|
1383 |
QTest::addColumn<QNetworkReply::NetworkError>("deleteError"); |
|
1384 |
QTest::addColumn<QUrl>("get2Url"); |
|
1385 |
QTest::addColumn<int>("get2ResultCode"); |
|
1386 |
QTest::addColumn<QNetworkReply::NetworkError>("get2Error"); |
|
1387 |
||
1388 |
QUrl url("http://" + QtNetworkSettings::serverName()); |
|
1389 |
url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2") |
|
1390 |
.arg(QTest::currentDataTag()) |
|
1391 |
.arg(uniqueExtension)); |
|
1392 |
||
1393 |
// first use case: put, get (to check it is there), delete, get (to check it is not there anymore) |
|
1394 |
QTest::newRow("success") << url << 201 << QNetworkReply::NoError << url << 204 << QNetworkReply::NoError << url << 404 << QNetworkReply::ContentNotFoundError; |
|
1395 |
||
1396 |
QUrl wrongUrl("http://" + QtNetworkSettings::serverName()); |
|
1397 |
wrongUrl.setPath(QString("/dav/qnetworkaccess-thisURLisNotAvailable")); |
|
1398 |
||
1399 |
// second use case: put, get (to check it is there), delete wrong URL, get (to check it is still there) |
|
1400 |
QTest::newRow("delete-error") << url << 201 << QNetworkReply::NoError << wrongUrl << 404 << QNetworkReply::ContentNotFoundError << url << 200 << QNetworkReply::NoError; |
|
1401 |
||
1402 |
} |
|
1403 |
||
1404 |
void tst_QNetworkReply::putGetDeleteGetFromHttp() |
|
1405 |
{ |
|
1406 |
QFETCH(QUrl, putUrl); |
|
1407 |
QFETCH(int, putResultCode); |
|
1408 |
QFETCH(QNetworkReply::NetworkError, putError); |
|
1409 |
QFETCH(QUrl, deleteUrl); |
|
1410 |
QFETCH(int, deleteResultCode); |
|
1411 |
QFETCH(QNetworkReply::NetworkError, deleteError); |
|
1412 |
QFETCH(QUrl, get2Url); |
|
1413 |
QFETCH(int, get2ResultCode); |
|
1414 |
QFETCH(QNetworkReply::NetworkError, get2Error); |
|
1415 |
||
1416 |
QNetworkRequest putRequest(putUrl); |
|
1417 |
QNetworkRequest deleteRequest(deleteUrl); |
|
1418 |
QNetworkRequest get2Request(get2Url); |
|
1419 |
QNetworkReplyPtr reply; |
|
1420 |
||
1421 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, putRequest, reply, 0)); |
|
1422 |
QCOMPARE(reply->error(), putError); |
|
1423 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), putResultCode); |
|
1424 |
||
1425 |
runSimpleRequest(QNetworkAccessManager::GetOperation, putRequest, reply, 0); |
|
1426 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1427 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1428 |
||
1429 |
runSimpleRequest(QNetworkAccessManager::DeleteOperation, deleteRequest, reply, 0); |
|
1430 |
QCOMPARE(reply->error(), deleteError); |
|
1431 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), deleteResultCode); |
|
1432 |
||
1433 |
runSimpleRequest(QNetworkAccessManager::GetOperation, get2Request, reply, 0); |
|
1434 |
QCOMPARE(reply->error(), get2Error); |
|
1435 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), get2ResultCode); |
|
1436 |
||
1437 |
} |
|
1438 |
||
1439 |
void tst_QNetworkReply::ioGetFromData_data() |
|
1440 |
{ |
|
1441 |
QTest::addColumn<QString>("urlStr"); |
|
1442 |
QTest::addColumn<QByteArray>("data"); |
|
1443 |
||
1444 |
QTest::newRow("data-empty") << "data:," << QByteArray(); |
|
1445 |
QTest::newRow("data-literal") << "data:,foo" << QByteArray("foo"); |
|
1446 |
QTest::newRow("data-pct") << "data:,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A" |
|
1447 |
<< QByteArray("<body contentEditable=true>\r\n"); |
|
1448 |
QTest::newRow("data-base64") << "data:;base64,UXQgaXMgZ3JlYXQh" << QByteArray("Qt is great!"); |
|
1449 |
} |
|
1450 |
||
1451 |
void tst_QNetworkReply::ioGetFromData() |
|
1452 |
{ |
|
1453 |
QFETCH(QString, urlStr); |
|
1454 |
||
1455 |
QUrl url = QUrl::fromEncoded(urlStr.toLatin1()); |
|
1456 |
QNetworkRequest request(url); |
|
1457 |
||
1458 |
QNetworkReplyPtr reply = manager.get(request); |
|
1459 |
DataReader reader(reply); |
|
1460 |
||
1461 |
connect(reply, SIGNAL(finished()), |
|
1462 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1463 |
QTestEventLoop::instance().enterLoop(10); |
|
1464 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1465 |
||
1466 |
QCOMPARE(reply->url(), request.url()); |
|
1467 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1468 |
||
1469 |
QFETCH(QByteArray, data); |
|
1470 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toInt(), data.size()); |
|
1471 |
QCOMPARE(reader.data.size(), data.size()); |
|
1472 |
QCOMPARE(reader.data, data); |
|
1473 |
} |
|
1474 |
||
1475 |
void tst_QNetworkReply::ioGetFromFileSpecial_data() |
|
1476 |
{ |
|
1477 |
getFromFileSpecial_data(); |
|
1478 |
} |
|
1479 |
||
1480 |
void tst_QNetworkReply::ioGetFromFileSpecial() |
|
1481 |
{ |
|
1482 |
QFETCH(QString, fileName); |
|
1483 |
QFETCH(QString, url); |
|
1484 |
||
1485 |
QFile resource(fileName); |
|
1486 |
QVERIFY(resource.open(QIODevice::ReadOnly)); |
|
1487 |
||
1488 |
QNetworkRequest request; |
|
1489 |
request.setUrl(url); |
|
1490 |
QNetworkReplyPtr reply = manager.get(request); |
|
1491 |
DataReader reader(reply); |
|
1492 |
||
1493 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1494 |
QTestEventLoop::instance().enterLoop(10); |
|
1495 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1496 |
||
1497 |
QCOMPARE(reply->url(), request.url()); |
|
1498 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1499 |
||
1500 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), resource.size()); |
|
1501 |
QCOMPARE(qint64(reader.data.size()), resource.size()); |
|
1502 |
QCOMPARE(reader.data, resource.readAll()); |
|
1503 |
} |
|
1504 |
||
1505 |
void tst_QNetworkReply::ioGetFromFile_data() |
|
1506 |
{ |
|
1507 |
putToFile_data(); |
|
1508 |
} |
|
1509 |
||
1510 |
void tst_QNetworkReply::ioGetFromFile() |
|
1511 |
{ |
|
1512 |
QTemporaryFile file(QDir::currentPath() + "/temp-XXXXXX"); |
|
1513 |
file.setAutoRemove(true); |
|
1514 |
QVERIFY(file.open()); |
|
1515 |
||
1516 |
QFETCH(QByteArray, data); |
|
1517 |
QVERIFY(file.write(data) == data.size()); |
|
1518 |
file.flush(); |
|
1519 |
QCOMPARE(file.size(), qint64(data.size())); |
|
1520 |
||
1521 |
QNetworkRequest request(QUrl::fromLocalFile(file.fileName())); |
|
1522 |
QNetworkReplyPtr reply = manager.get(request); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1523 |
QVERIFY(reply->isFinished()); // a file should immediatly be done |
0 | 1524 |
DataReader reader(reply); |
1525 |
||
1526 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1527 |
QTestEventLoop::instance().enterLoop(10); |
|
1528 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1529 |
||
1530 |
QCOMPARE(reply->url(), request.url()); |
|
1531 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1532 |
||
1533 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size()); |
|
1534 |
QCOMPARE(qint64(reader.data.size()), file.size()); |
|
1535 |
QCOMPARE(reader.data, data); |
|
1536 |
} |
|
1537 |
||
1538 |
void tst_QNetworkReply::ioGetFromFtp_data() |
|
1539 |
{ |
|
1540 |
QTest::addColumn<QString>("fileName"); |
|
1541 |
QTest::addColumn<qint64>("expectedSize"); |
|
1542 |
||
1543 |
QTest::newRow("bigfile") << "bigfile" << Q_INT64_C(519240); |
|
1544 |
||
1545 |
QFile file(SRCDIR "/rfc3252.txt"); |
|
1546 |
QTest::newRow("rfc3252.txt") << "rfc3252.txt" << file.size(); |
|
1547 |
} |
|
1548 |
||
1549 |
void tst_QNetworkReply::ioGetFromFtp() |
|
1550 |
{ |
|
1551 |
QFETCH(QString, fileName); |
|
1552 |
QFile reference(fileName); |
|
1553 |
reference.open(QIODevice::ReadOnly); // will fail for bigfile |
|
1554 |
||
1555 |
QNetworkRequest request("ftp://" + QtNetworkSettings::serverName() + "/qtest/" + fileName); |
|
1556 |
QNetworkReplyPtr reply = manager.get(request); |
|
1557 |
DataReader reader(reply); |
|
1558 |
||
1559 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1560 |
QTestEventLoop::instance().enterLoop(10); |
|
1561 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1562 |
||
1563 |
QCOMPARE(reply->url(), request.url()); |
|
1564 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1565 |
||
1566 |
QFETCH(qint64, expectedSize); |
|
1567 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), expectedSize); |
|
1568 |
QCOMPARE(qint64(reader.data.size()), expectedSize); |
|
1569 |
||
1570 |
if (reference.isOpen()) |
|
1571 |
QCOMPARE(reader.data, reference.readAll()); |
|
1572 |
} |
|
1573 |
||
1574 |
void tst_QNetworkReply::ioGetFromFtpWithReuse() |
|
1575 |
{ |
|
1576 |
QString fileName = SRCDIR "/rfc3252.txt"; |
|
1577 |
QFile reference(fileName); |
|
1578 |
reference.open(QIODevice::ReadOnly); |
|
1579 |
||
1580 |
QNetworkRequest request(QUrl("ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1581 |
||
1582 |
// two concurrent (actually, consecutive) gets: |
|
1583 |
QNetworkReplyPtr reply1 = manager.get(request); |
|
1584 |
DataReader reader1(reply1); |
|
1585 |
QNetworkReplyPtr reply2 = manager.get(request); |
|
1586 |
DataReader reader2(reply2); |
|
1587 |
QSignalSpy spy(reply1, SIGNAL(finished())); |
|
1588 |
||
1589 |
connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1590 |
QTestEventLoop::instance().enterLoop(10); |
|
1591 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1592 |
if (spy.count() == 0) { |
|
1593 |
connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1594 |
QTestEventLoop::instance().enterLoop(10); |
|
1595 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1596 |
} |
|
1597 |
||
1598 |
QCOMPARE(reply1->url(), request.url()); |
|
1599 |
QCOMPARE(reply2->url(), request.url()); |
|
1600 |
QCOMPARE(reply1->error(), QNetworkReply::NoError); |
|
1601 |
QCOMPARE(reply2->error(), QNetworkReply::NoError); |
|
1602 |
||
1603 |
QCOMPARE(qint64(reader1.data.size()), reference.size()); |
|
1604 |
QCOMPARE(qint64(reader2.data.size()), reference.size()); |
|
1605 |
QCOMPARE(reply1->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1606 |
QCOMPARE(reply2->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1607 |
||
1608 |
QByteArray referenceData = reference.readAll(); |
|
1609 |
QCOMPARE(reader1.data, referenceData); |
|
1610 |
QCOMPARE(reader2.data, referenceData); |
|
1611 |
} |
|
1612 |
||
1613 |
void tst_QNetworkReply::ioGetFromHttp() |
|
1614 |
{ |
|
1615 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1616 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1617 |
||
1618 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1619 |
QNetworkReplyPtr reply = manager.get(request); |
|
1620 |
DataReader reader(reply); |
|
1621 |
||
1622 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1623 |
QTestEventLoop::instance().enterLoop(10); |
|
1624 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1625 |
||
1626 |
QCOMPARE(reply->url(), request.url()); |
|
1627 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1628 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1629 |
||
1630 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1631 |
QCOMPARE(qint64(reader.data.size()), reference.size()); |
|
1632 |
||
1633 |
QCOMPARE(reader.data, reference.readAll()); |
|
1634 |
} |
|
1635 |
||
1636 |
void tst_QNetworkReply::ioGetFromHttpWithReuseParallel() |
|
1637 |
{ |
|
1638 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1639 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1640 |
||
1641 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1642 |
QNetworkReplyPtr reply1 = manager.get(request); |
|
1643 |
QNetworkReplyPtr reply2 = manager.get(request); |
|
1644 |
DataReader reader1(reply1); |
|
1645 |
DataReader reader2(reply2); |
|
1646 |
QSignalSpy spy(reply1, SIGNAL(finished())); |
|
1647 |
||
1648 |
connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1649 |
QTestEventLoop::instance().enterLoop(10); |
|
1650 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1651 |
if (spy.count() == 0) { |
|
1652 |
connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1653 |
QTestEventLoop::instance().enterLoop(10); |
|
1654 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1655 |
} |
|
1656 |
||
1657 |
QCOMPARE(reply1->url(), request.url()); |
|
1658 |
QCOMPARE(reply2->url(), request.url()); |
|
1659 |
QCOMPARE(reply1->error(), QNetworkReply::NoError); |
|
1660 |
QCOMPARE(reply2->error(), QNetworkReply::NoError); |
|
1661 |
QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1662 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1663 |
||
1664 |
QCOMPARE(reply1->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1665 |
QCOMPARE(reply2->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1666 |
QCOMPARE(qint64(reader1.data.size()), reference.size()); |
|
1667 |
QCOMPARE(qint64(reader2.data.size()), reference.size()); |
|
1668 |
||
1669 |
QByteArray referenceData = reference.readAll(); |
|
1670 |
QCOMPARE(reader1.data, referenceData); |
|
1671 |
QCOMPARE(reader2.data, referenceData); |
|
1672 |
} |
|
1673 |
||
1674 |
void tst_QNetworkReply::ioGetFromHttpWithReuseSequential() |
|
1675 |
{ |
|
1676 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1677 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1678 |
||
1679 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1680 |
{ |
|
1681 |
QNetworkReplyPtr reply = manager.get(request); |
|
1682 |
DataReader reader(reply); |
|
1683 |
||
1684 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1685 |
QTestEventLoop::instance().enterLoop(10); |
|
1686 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1687 |
||
1688 |
QCOMPARE(reply->url(), request.url()); |
|
1689 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1690 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1691 |
||
1692 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1693 |
QCOMPARE(qint64(reader.data.size()), reference.size()); |
|
1694 |
||
1695 |
QCOMPARE(reader.data, reference.readAll()); |
|
1696 |
} |
|
1697 |
||
1698 |
reference.seek(0); |
|
1699 |
// rinse and repeat: |
|
1700 |
{ |
|
1701 |
QNetworkReplyPtr reply = manager.get(request); |
|
1702 |
DataReader reader(reply); |
|
1703 |
||
1704 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1705 |
QTestEventLoop::instance().enterLoop(10); |
|
1706 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1707 |
||
1708 |
QCOMPARE(reply->url(), request.url()); |
|
1709 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
1710 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1711 |
||
1712 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size()); |
|
1713 |
QCOMPARE(qint64(reader.data.size()), reference.size()); |
|
1714 |
||
1715 |
QCOMPARE(reader.data, reference.readAll()); |
|
1716 |
} |
|
1717 |
} |
|
1718 |
||
1719 |
void tst_QNetworkReply::ioGetFromHttpWithAuth() |
|
1720 |
{ |
|
1721 |
qRegisterMetaType<QNetworkReply *>(); // for QSignalSpy |
|
1722 |
qRegisterMetaType<QAuthenticator *>(); |
|
1723 |
||
1724 |
// This test sends three requests |
|
1725 |
// The first two in parallel |
|
1726 |
// The third after the first two finished |
|
1727 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1728 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1729 |
||
1730 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt")); |
|
1731 |
{ |
|
1732 |
QNetworkReplyPtr reply1 = manager.get(request); |
|
1733 |
QNetworkReplyPtr reply2 = manager.get(request); |
|
1734 |
DataReader reader1(reply1); |
|
1735 |
DataReader reader2(reply2); |
|
1736 |
QSignalSpy finishedspy(reply1, SIGNAL(finished())); |
|
1737 |
||
1738 |
QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1739 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
1740 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1741 |
||
1742 |
connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1743 |
QTestEventLoop::instance().enterLoop(10); |
|
1744 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1745 |
if (finishedspy.count() == 0) { |
|
1746 |
connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1747 |
QTestEventLoop::instance().enterLoop(10); |
|
1748 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1749 |
} |
|
1750 |
manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
1751 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1752 |
||
1753 |
QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1754 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1755 |
QByteArray referenceData = reference.readAll(); |
|
1756 |
QCOMPARE(reader1.data, referenceData); |
|
1757 |
QCOMPARE(reader2.data, referenceData); |
|
1758 |
||
1759 |
QCOMPARE(authspy.count(), 1); |
|
1760 |
} |
|
1761 |
||
1762 |
reference.seek(0); |
|
1763 |
// rinse and repeat: |
|
1764 |
{ |
|
1765 |
QNetworkReplyPtr reply = manager.get(request); |
|
1766 |
DataReader reader(reply); |
|
1767 |
||
1768 |
QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1769 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
1770 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1771 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1772 |
QTestEventLoop::instance().enterLoop(10); |
|
1773 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1774 |
manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
1775 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
1776 |
||
1777 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1778 |
QCOMPARE(reader.data, reference.readAll()); |
|
1779 |
||
1780 |
QCOMPARE(authspy.count(), 0); |
|
1781 |
} |
|
1782 |
} |
|
1783 |
||
1784 |
void tst_QNetworkReply::ioGetFromHttpWithProxyAuth() |
|
1785 |
{ |
|
1786 |
qRegisterMetaType<QNetworkProxy>(); // for QSignalSpy |
|
1787 |
qRegisterMetaType<QAuthenticator *>(); |
|
1788 |
||
1789 |
// This test sends three requests |
|
1790 |
// The first two in parallel |
|
1791 |
// The third after the first two finished |
|
1792 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1793 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1794 |
||
1795 |
QNetworkProxy proxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
1796 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1797 |
{ |
|
1798 |
manager.setProxy(proxy); |
|
1799 |
QNetworkReplyPtr reply1 = manager.get(request); |
|
1800 |
QNetworkReplyPtr reply2 = manager.get(request); |
|
1801 |
manager.setProxy(QNetworkProxy()); |
|
1802 |
||
1803 |
DataReader reader1(reply1); |
|
1804 |
DataReader reader2(reply2); |
|
1805 |
QSignalSpy finishedspy(reply1, SIGNAL(finished())); |
|
1806 |
||
1807 |
QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1808 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1809 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1810 |
||
1811 |
connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1812 |
QTestEventLoop::instance().enterLoop(10); |
|
1813 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1814 |
if (finishedspy.count() == 0) { |
|
1815 |
connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1816 |
QTestEventLoop::instance().enterLoop(10); |
|
1817 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1818 |
} |
|
1819 |
manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1820 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1821 |
||
1822 |
QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1823 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1824 |
QByteArray referenceData = reference.readAll(); |
|
1825 |
QCOMPARE(reader1.data, referenceData); |
|
1826 |
QCOMPARE(reader2.data, referenceData); |
|
1827 |
||
1828 |
QCOMPARE(authspy.count(), 1); |
|
1829 |
} |
|
1830 |
||
1831 |
reference.seek(0); |
|
1832 |
// rinse and repeat: |
|
1833 |
{ |
|
1834 |
manager.setProxy(proxy); |
|
1835 |
QNetworkReplyPtr reply = manager.get(request); |
|
1836 |
DataReader reader(reply); |
|
1837 |
manager.setProxy(QNetworkProxy()); |
|
1838 |
||
1839 |
QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1840 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1841 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1842 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1843 |
QTestEventLoop::instance().enterLoop(10); |
|
1844 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1845 |
manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1846 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1847 |
||
1848 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1849 |
QCOMPARE(reader.data, reference.readAll()); |
|
1850 |
||
1851 |
QCOMPARE(authspy.count(), 0); |
|
1852 |
} |
|
1853 |
} |
|
1854 |
||
1855 |
void tst_QNetworkReply::ioGetFromHttpWithSocksProxy() |
|
1856 |
{ |
|
1857 |
// HTTP caching proxies are tested by the above function |
|
1858 |
// test SOCKSv5 proxies too |
|
1859 |
||
1860 |
qRegisterMetaType<QNetworkProxy>(); // for QSignalSpy |
|
1861 |
qRegisterMetaType<QAuthenticator *>(); |
|
1862 |
||
1863 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1864 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1865 |
||
1866 |
QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080); |
|
1867 |
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1868 |
{ |
|
1869 |
manager.setProxy(proxy); |
|
1870 |
QNetworkReplyPtr reply = manager.get(request); |
|
1871 |
DataReader reader(reply); |
|
1872 |
manager.setProxy(QNetworkProxy()); |
|
1873 |
||
1874 |
QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1875 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1876 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1877 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1878 |
QTestEventLoop::instance().enterLoop(10); |
|
1879 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1880 |
manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1881 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1882 |
||
1883 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1884 |
QCOMPARE(reader.data, reference.readAll()); |
|
1885 |
||
1886 |
QCOMPARE(authspy.count(), 0); |
|
1887 |
} |
|
1888 |
||
1889 |
// set an invalid proxy just to make sure that we can't load |
|
1890 |
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1079); |
|
1891 |
{ |
|
1892 |
manager.setProxy(proxy); |
|
1893 |
QNetworkReplyPtr reply = manager.get(request); |
|
1894 |
DataReader reader(reply); |
|
1895 |
manager.setProxy(QNetworkProxy()); |
|
1896 |
||
1897 |
QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1898 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1899 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1900 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1901 |
QTestEventLoop::instance().enterLoop(10); |
|
1902 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1903 |
manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1904 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1905 |
||
1906 |
QVERIFY(!reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid()); |
|
1907 |
QVERIFY(reader.data.isEmpty()); |
|
1908 |
||
1909 |
QVERIFY(int(reply->error()) > 0); |
|
1910 |
QEXPECT_FAIL("", "QTcpSocket doesn't return enough information yet", Continue); |
|
1911 |
QCOMPARE(int(reply->error()), int(QNetworkReply::ProxyConnectionRefusedError)); |
|
1912 |
||
1913 |
QCOMPARE(authspy.count(), 0); |
|
1914 |
} |
|
1915 |
} |
|
1916 |
||
1917 |
#ifndef QT_NO_OPENSSL |
|
1918 |
void tst_QNetworkReply::ioGetFromHttpsWithSslErrors() |
|
1919 |
{ |
|
1920 |
qRegisterMetaType<QNetworkReply*>(); // for QSignalSpy |
|
1921 |
qRegisterMetaType<QList<QSslError> >(); |
|
1922 |
||
1923 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1924 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1925 |
||
1926 |
QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1927 |
QNetworkReplyPtr reply = manager.get(request); |
|
1928 |
DataReader reader(reply); |
|
1929 |
||
1930 |
QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
1931 |
connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), |
|
1932 |
SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
1933 |
connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration())); |
|
1934 |
||
1935 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1936 |
QTestEventLoop::instance().enterLoop(10); |
|
1937 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1938 |
manager.disconnect(SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), |
|
1939 |
this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
1940 |
||
1941 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1942 |
QCOMPARE(reader.data, reference.readAll()); |
|
1943 |
||
1944 |
QCOMPARE(sslspy.count(), 1); |
|
1945 |
||
1946 |
QVERIFY(!storedSslConfiguration.isNull()); |
|
1947 |
QVERIFY(!reply->sslConfiguration().isNull()); |
|
1948 |
} |
|
1949 |
||
1950 |
void tst_QNetworkReply::ioGetFromHttpsWithIgnoreSslErrors() |
|
1951 |
{ |
|
1952 |
// same as above, except that we call ignoreSslErrors and don't connect |
|
1953 |
// to the sslErrors() signal (which is *still* emitted) |
|
1954 |
||
1955 |
qRegisterMetaType<QNetworkReply*>(); // for QSignalSpy |
|
1956 |
qRegisterMetaType<QList<QSslError> >(); |
|
1957 |
||
1958 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1959 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1960 |
||
1961 |
QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")); |
|
1962 |
||
1963 |
QNetworkReplyPtr reply = manager.get(request); |
|
1964 |
reply->ignoreSslErrors(); |
|
1965 |
DataReader reader(reply); |
|
1966 |
||
1967 |
QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
1968 |
connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration())); |
|
1969 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1970 |
QTestEventLoop::instance().enterLoop(10); |
|
1971 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1972 |
||
1973 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
1974 |
QCOMPARE(reader.data, reference.readAll()); |
|
1975 |
||
1976 |
QCOMPARE(sslspy.count(), 1); |
|
1977 |
||
1978 |
QVERIFY(!storedSslConfiguration.isNull()); |
|
1979 |
QVERIFY(!reply->sslConfiguration().isNull()); |
|
1980 |
} |
|
1981 |
||
1982 |
void tst_QNetworkReply::ioGetFromHttpsWithSslHandshakeError() |
|
1983 |
{ |
|
1984 |
qRegisterMetaType<QNetworkReply*>(); // for QSignalSpy |
|
1985 |
qRegisterMetaType<QList<QSslError> >(); |
|
1986 |
||
1987 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
1988 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
1989 |
||
1990 |
QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + ":80")); |
|
1991 |
||
1992 |
QNetworkReplyPtr reply = manager.get(request); |
|
1993 |
reply->ignoreSslErrors(); |
|
1994 |
DataReader reader(reply); |
|
1995 |
||
1996 |
QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
1997 |
connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration())); |
|
1998 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1999 |
QTestEventLoop::instance().enterLoop(10); |
|
2000 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2001 |
||
2002 |
QCOMPARE(reply->error(), QNetworkReply::SslHandshakeFailedError); |
|
2003 |
QCOMPARE(sslspy.count(), 0); |
|
2004 |
} |
|
2005 |
#endif |
|
2006 |
||
2007 |
void tst_QNetworkReply::ioGetFromHttpBrokenServer_data() |
|
2008 |
{ |
|
2009 |
QTest::addColumn<QByteArray>("dataToSend"); |
|
2010 |
QTest::addColumn<bool>("doDisconnect"); |
|
2011 |
||
2012 |
QTest::newRow("no-newline") << QByteArray("Hello World") << false; |
|
2013 |
||
2014 |
// these are OK now, we just eat the lonely newlines |
|
2015 |
//QTest::newRow("just-newline") << QByteArray("\r\n") << false; |
|
2016 |
//QTest::newRow("just-2newline") << QByteArray("\r\n\r\n") << false; |
|
2017 |
||
2018 |
QTest::newRow("with-newlines") << QByteArray("Long first line\r\nLong second line") << false; |
|
2019 |
QTest::newRow("with-newlines2") << QByteArray("\r\nSecond line") << false; |
|
2020 |
QTest::newRow("with-newlines3") << QByteArray("ICY\r\nSecond line") << false; |
|
2021 |
QTest::newRow("invalid-version") << QByteArray("HTTP/123 200 \r\n") << false; |
|
2022 |
QTest::newRow("invalid-version2") << QByteArray("HTTP/a.\033 200 \r\n") << false; |
|
2023 |
QTest::newRow("invalid-reply-code") << QByteArray("HTTP/1.0 fuu \r\n") << false; |
|
2024 |
||
2025 |
QTest::newRow("empty+disconnect") << QByteArray() << true; |
|
2026 |
||
2027 |
QTest::newRow("no-newline+disconnect") << QByteArray("Hello World") << true; |
|
2028 |
QTest::newRow("just-newline+disconnect") << QByteArray("\r\n") << true; |
|
2029 |
QTest::newRow("just-2newline+disconnect") << QByteArray("\r\n\r\n") << true; |
|
2030 |
QTest::newRow("with-newlines+disconnect") << QByteArray("Long first line\r\nLong second line") << true; |
|
2031 |
QTest::newRow("with-newlines2+disconnect") << QByteArray("\r\nSecond line") << true; |
|
2032 |
QTest::newRow("with-newlines3+disconnect") << QByteArray("ICY\r\nSecond line") << true; |
|
2033 |
||
2034 |
QTest::newRow("invalid-version+disconnect") << QByteArray("HTTP/123 200 ") << true; |
|
2035 |
QTest::newRow("invalid-version2+disconnect") << QByteArray("HTTP/a.\033 200 ") << true; |
|
2036 |
QTest::newRow("invalid-reply-code+disconnect") << QByteArray("HTTP/1.0 fuu ") << true; |
|
2037 |
} |
|
2038 |
||
2039 |
void tst_QNetworkReply::ioGetFromHttpBrokenServer() |
|
2040 |
{ |
|
2041 |
QFETCH(QByteArray, dataToSend); |
|
2042 |
QFETCH(bool, doDisconnect); |
|
2043 |
MiniHttpServer server(dataToSend); |
|
2044 |
server.doClose = doDisconnect; |
|
2045 |
||
2046 |
QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort()))); |
|
2047 |
QNetworkReplyPtr reply = manager.get(request); |
|
2048 |
||
2049 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2050 |
QTestEventLoop::instance().enterLoop(10); |
|
2051 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2052 |
||
2053 |
QCOMPARE(reply->url(), request.url()); |
|
2054 |
QVERIFY(reply->error() != QNetworkReply::NoError); |
|
2055 |
} |
|
2056 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2057 |
void tst_QNetworkReply::ioGetFromHttpWithCache_data() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2058 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2059 |
qRegisterMetaType<MyMemoryCache::CachedContent>(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2060 |
QTest::addColumn<QByteArray>("dataToSend"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2061 |
QTest::addColumn<QString>("body"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2062 |
QTest::addColumn<MyMemoryCache::CachedContent>("cachedReply"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2063 |
QTest::addColumn<int>("cacheMode"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2064 |
QTest::addColumn<bool>("loadedFromCache"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2065 |
QTest::addColumn<bool>("networkUsed"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2066 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2067 |
QByteArray reply200 = |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2068 |
"HTTP/1.0 200\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2069 |
"Connection: keep-alive\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2070 |
"Content-Type: text/plain\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2071 |
"Cache-control: no-cache\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2072 |
"Content-length: 8\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2073 |
"\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2074 |
"Reloaded"; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2075 |
QByteArray reply304 = |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2076 |
"HTTP/1.0 304 Use Cache\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2077 |
"Connection: keep-alive\r\n" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2078 |
"\r\n"; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2079 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2080 |
QTest::newRow("not-cached,always-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2081 |
<< reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::AlwaysNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2082 |
QTest::newRow("not-cached,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2083 |
<< reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::PreferNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2084 |
QTest::newRow("not-cached,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2085 |
<< reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::PreferCache) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2086 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2087 |
QDateTime present = QDateTime::currentDateTime().toUTC(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2088 |
QDateTime past = present.addSecs(-3600); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2089 |
QDateTime future = present.addSecs(3600); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2090 |
static const char dateFormat[] = "ddd, dd MMM yyyy hh:mm:ss 'GMT'"; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2091 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2092 |
QNetworkCacheMetaData::RawHeaderList rawHeaders; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2093 |
MyMemoryCache::CachedContent content; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2094 |
content.second = "Not-reloaded"; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2095 |
content.first.setLastModified(past); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2096 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2097 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2098 |
// Set to expired |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2099 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2100 |
rawHeaders.clear(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2101 |
rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2102 |
<< QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=0"); // isn't used in cache loading |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2103 |
content.first.setRawHeaders(rawHeaders); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2104 |
content.first.setLastModified(past); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2105 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2106 |
QTest::newRow("expired,200,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2107 |
<< reply200 << "Reloaded" << content << int(QNetworkRequest::PreferNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2108 |
QTest::newRow("expired,200,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2109 |
<< reply200 << "Reloaded" << content << int(QNetworkRequest::PreferCache) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2110 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2111 |
QTest::newRow("expired,304,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2112 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << true << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2113 |
QTest::newRow("expired,304,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2114 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << true << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2115 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2116 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2117 |
// Set to not-expired |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2118 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2119 |
rawHeaders.clear(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2120 |
rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2121 |
<< QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=7200"); // isn't used in cache loading |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2122 |
content.first.setRawHeaders(rawHeaders); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2123 |
content.first.setExpirationDate(future); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2124 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2125 |
QTest::newRow("not-expired,200,always-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2126 |
<< reply200 << "Reloaded" << content << int(QNetworkRequest::AlwaysNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2127 |
QTest::newRow("not-expired,200,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2128 |
<< reply200 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2129 |
QTest::newRow("not-expired,200,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2130 |
<< reply200 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2131 |
QTest::newRow("not-expired,200,always-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2132 |
<< reply200 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2133 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2134 |
QTest::newRow("not-expired,304,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2135 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2136 |
QTest::newRow("not-expired,304,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2137 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2138 |
QTest::newRow("not-expired,304,always-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2139 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2140 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2141 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2142 |
// Set must-revalidate now |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2143 |
// |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2144 |
rawHeaders.clear(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2145 |
rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2146 |
<< QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=7200, must-revalidate"); // must-revalidate is used |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2147 |
content.first.setRawHeaders(rawHeaders); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2148 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2149 |
QTest::newRow("must-revalidate,200,always-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2150 |
<< reply200 << "Reloaded" << content << int(QNetworkRequest::AlwaysNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2151 |
QTest::newRow("must-revalidate,200,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2152 |
<< reply200 << "Reloaded" << content << int(QNetworkRequest::PreferNetwork) << false << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2153 |
QTest::newRow("must-revalidate,200,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2154 |
<< reply200 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2155 |
QTest::newRow("must-revalidate,200,always-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2156 |
<< reply200 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2157 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2158 |
QTest::newRow("must-revalidate,304,prefer-network") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2159 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << true << true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2160 |
QTest::newRow("must-revalidate,304,prefer-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2161 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2162 |
QTest::newRow("must-revalidate,304,always-cache") |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2163 |
<< reply304 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << true << false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2164 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2165 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2166 |
void tst_QNetworkReply::ioGetFromHttpWithCache() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2167 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2168 |
QFETCH(QByteArray, dataToSend); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2169 |
MiniHttpServer server(dataToSend); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2170 |
server.doClose = false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2171 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2172 |
MyMemoryCache *memoryCache = new MyMemoryCache(&manager); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2173 |
manager.setCache(memoryCache); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2174 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2175 |
QFETCH(MyMemoryCache::CachedContent, cachedReply); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2176 |
QUrl url = "http://localhost:" + QString::number(server.serverPort()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2177 |
cachedReply.first.setUrl(url); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2178 |
if (!cachedReply.second.isNull()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2179 |
memoryCache->cache.insert(url.toEncoded(), cachedReply); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2180 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2181 |
QFETCH(int, cacheMode); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2182 |
QNetworkRequest request(url); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2183 |
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, cacheMode); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2184 |
request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2185 |
QNetworkReplyPtr reply = manager.get(request); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2186 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2187 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2188 |
QTestEventLoop::instance().enterLoop(10); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2189 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2190 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2191 |
QTEST(reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool(), "loadedFromCache"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2192 |
QTEST(server.totalConnections > 0, "networkUsed"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2193 |
QFETCH(QString, body); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2194 |
QCOMPARE(reply->readAll().constData(), qPrintable(body)); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2195 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
2196 |
|
0 | 2197 |
void tst_QNetworkReply::ioGetWithManyProxies_data() |
2198 |
{ |
|
2199 |
QTest::addColumn<QList<QNetworkProxy> >("proxyList"); |
|
2200 |
QTest::addColumn<QNetworkProxy>("proxyUsed"); |
|
2201 |
QTest::addColumn<QString>("url"); |
|
2202 |
QTest::addColumn<QNetworkReply::NetworkError>("expectedError"); |
|
2203 |
||
2204 |
QList<QNetworkProxy> proxyList; |
|
2205 |
||
2206 |
// All of the other functions test DefaultProxy |
|
2207 |
// So let's test something else |
|
2208 |
||
2209 |
// Simple tests that work: |
|
2210 |
||
2211 |
// HTTP request with HTTP caching proxy |
|
2212 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2213 |
QTest::newRow("http-on-http") |
|
2214 |
<< proxyList << proxyList.at(0) |
|
2215 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2216 |
<< QNetworkReply::NoError; |
|
2217 |
||
2218 |
// HTTP request with HTTP transparent proxy |
|
2219 |
proxyList.clear(); |
|
2220 |
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129); |
|
2221 |
QTest::newRow("http-on-http2") |
|
2222 |
<< proxyList << proxyList.at(0) |
|
2223 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2224 |
<< QNetworkReply::NoError; |
|
2225 |
||
2226 |
// HTTP request with SOCKS transparent proxy |
|
2227 |
proxyList.clear(); |
|
2228 |
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2229 |
QTest::newRow("http-on-socks") |
|
2230 |
<< proxyList << proxyList.at(0) |
|
2231 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2232 |
<< QNetworkReply::NoError; |
|
2233 |
||
2234 |
// FTP request with FTP caching proxy |
|
2235 |
proxyList.clear(); |
|
2236 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
2237 |
QTest::newRow("ftp-on-ftp") |
|
2238 |
<< proxyList << proxyList.at(0) |
|
2239 |
<< "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2240 |
<< QNetworkReply::NoError; |
|
2241 |
||
2242 |
// The following test doesn't work because QFtp is too limited |
|
2243 |
// It can only talk to its own kind of proxies |
|
2244 |
||
2245 |
// FTP request with SOCKSv5 transparent proxy |
|
2246 |
proxyList.clear(); |
|
2247 |
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2248 |
QTest::newRow("ftp-on-socks") |
|
2249 |
<< proxyList << proxyList.at(0) |
|
2250 |
<< "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2251 |
<< QNetworkReply::NoError; |
|
2252 |
||
2253 |
#ifndef QT_NO_OPENSSL |
|
2254 |
// HTTPS with HTTP transparent proxy |
|
2255 |
proxyList.clear(); |
|
2256 |
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129); |
|
2257 |
QTest::newRow("https-on-http") |
|
2258 |
<< proxyList << proxyList.at(0) |
|
2259 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2260 |
<< QNetworkReply::NoError; |
|
2261 |
||
2262 |
// HTTPS request with SOCKS transparent proxy |
|
2263 |
proxyList.clear(); |
|
2264 |
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2265 |
QTest::newRow("https-on-socks") |
|
2266 |
<< proxyList << proxyList.at(0) |
|
2267 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2268 |
<< QNetworkReply::NoError; |
|
2269 |
#endif |
|
2270 |
||
2271 |
// Tests that fail: |
|
2272 |
||
2273 |
// HTTP request with FTP caching proxy |
|
2274 |
proxyList.clear(); |
|
2275 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
2276 |
QTest::newRow("http-on-ftp") |
|
2277 |
<< proxyList << QNetworkProxy() |
|
2278 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2279 |
<< QNetworkReply::ProxyNotFoundError; |
|
2280 |
||
2281 |
// FTP request with HTTP caching proxy |
|
2282 |
proxyList.clear(); |
|
2283 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2284 |
QTest::newRow("ftp-on-http") |
|
2285 |
<< proxyList << QNetworkProxy() |
|
2286 |
<< "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2287 |
<< QNetworkReply::ProxyNotFoundError; |
|
2288 |
||
2289 |
// FTP request with HTTP caching proxies |
|
2290 |
proxyList.clear(); |
|
2291 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2292 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3130); |
|
2293 |
QTest::newRow("ftp-on-multiple-http") |
|
2294 |
<< proxyList << QNetworkProxy() |
|
2295 |
<< "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2296 |
<< QNetworkReply::ProxyNotFoundError; |
|
2297 |
||
2298 |
#ifndef QT_NO_OPENSSL |
|
2299 |
// HTTPS with HTTP caching proxy |
|
2300 |
proxyList.clear(); |
|
2301 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2302 |
QTest::newRow("https-on-httptransparent") |
|
2303 |
<< proxyList << QNetworkProxy() |
|
2304 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2305 |
<< QNetworkReply::ProxyNotFoundError; |
|
2306 |
||
2307 |
// HTTPS with FTP caching proxy |
|
2308 |
proxyList.clear(); |
|
2309 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
2310 |
QTest::newRow("https-on-ftp") |
|
2311 |
<< proxyList << QNetworkProxy() |
|
2312 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2313 |
<< QNetworkReply::ProxyNotFoundError; |
|
2314 |
#endif |
|
2315 |
||
2316 |
// Complex requests: |
|
2317 |
||
2318 |
// HTTP request with more than one HTTP proxy |
|
2319 |
proxyList.clear(); |
|
2320 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2321 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3130); |
|
2322 |
QTest::newRow("http-on-multiple-http") |
|
2323 |
<< proxyList << proxyList.at(0) |
|
2324 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2325 |
<< QNetworkReply::NoError; |
|
2326 |
||
2327 |
// HTTP request with HTTP + SOCKS |
|
2328 |
proxyList.clear(); |
|
2329 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2330 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2331 |
QTest::newRow("http-on-http+socks") |
|
2332 |
<< proxyList << proxyList.at(0) |
|
2333 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2334 |
<< QNetworkReply::NoError; |
|
2335 |
||
2336 |
// HTTP request with FTP + HTTP + SOCKS |
|
2337 |
proxyList.clear(); |
|
2338 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
2339 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2340 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2341 |
QTest::newRow("http-on-ftp+http+socks") |
|
2342 |
<< proxyList << proxyList.at(1) // second proxy should be used |
|
2343 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2344 |
<< QNetworkReply::NoError; |
|
2345 |
||
2346 |
// HTTP request with NoProxy + HTTP |
|
2347 |
proxyList.clear(); |
|
2348 |
proxyList << QNetworkProxy(QNetworkProxy::NoProxy) |
|
2349 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2350 |
QTest::newRow("http-on-noproxy+http") |
|
2351 |
<< proxyList << proxyList.at(0) |
|
2352 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2353 |
<< QNetworkReply::NoError; |
|
2354 |
||
2355 |
// HTTP request with FTP + NoProxy |
|
2356 |
proxyList.clear(); |
|
2357 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
2358 |
<< QNetworkProxy(QNetworkProxy::NoProxy); |
|
2359 |
QTest::newRow("http-on-ftp+noproxy") |
|
2360 |
<< proxyList << proxyList.at(1) // second proxy should be used |
|
2361 |
<< "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2362 |
<< QNetworkReply::NoError; |
|
2363 |
||
2364 |
// FTP request with HTTP Caching + FTP |
|
2365 |
proxyList.clear(); |
|
2366 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2367 |
<< QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
2368 |
QTest::newRow("ftp-on-http+ftp") |
|
2369 |
<< proxyList << proxyList.at(1) // second proxy should be used |
|
2370 |
<< "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2371 |
<< QNetworkReply::NoError; |
|
2372 |
||
2373 |
#ifndef QT_NO_OPENSSL |
|
2374 |
// HTTPS request with HTTP Caching + HTTP transparent |
|
2375 |
proxyList.clear(); |
|
2376 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2377 |
<< QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129); |
|
2378 |
QTest::newRow("https-on-httpcaching+http") |
|
2379 |
<< proxyList << proxyList.at(1) // second proxy should be used |
|
2380 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2381 |
<< QNetworkReply::NoError; |
|
2382 |
||
2383 |
// HTTPS request with FTP + HTTP C + HTTP T |
|
2384 |
proxyList.clear(); |
|
2385 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
2386 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2387 |
<< QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129); |
|
2388 |
QTest::newRow("https-on-ftp+httpcaching+http") |
|
2389 |
<< proxyList << proxyList.at(2) // skip the first two |
|
2390 |
<< "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt" |
|
2391 |
<< QNetworkReply::NoError; |
|
2392 |
#endif |
|
2393 |
} |
|
2394 |
||
2395 |
void tst_QNetworkReply::ioGetWithManyProxies() |
|
2396 |
{ |
|
2397 |
// Test proxy factories |
|
2398 |
||
2399 |
qRegisterMetaType<QNetworkProxy>(); // for QSignalSpy |
|
2400 |
qRegisterMetaType<QAuthenticator *>(); |
|
2401 |
||
2402 |
QFile reference(SRCDIR "/rfc3252.txt"); |
|
2403 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
2404 |
||
2405 |
// set the proxy factory: |
|
2406 |
QFETCH(QList<QNetworkProxy>, proxyList); |
|
2407 |
MyProxyFactory *proxyFactory = new MyProxyFactory; |
|
2408 |
proxyFactory->toReturn = proxyList; |
|
2409 |
manager.setProxyFactory(proxyFactory); |
|
2410 |
||
2411 |
QFETCH(QString, url); |
|
2412 |
QUrl theUrl(url); |
|
2413 |
QNetworkRequest request(theUrl); |
|
2414 |
QNetworkReplyPtr reply = manager.get(request); |
|
2415 |
DataReader reader(reply); |
|
2416 |
||
2417 |
QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2418 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
2419 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2420 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2421 |
#ifndef QT_NO_OPENSSL |
|
2422 |
connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), |
|
2423 |
SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
2424 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2425 |
QTestEventLoop::instance().enterLoop(15); |
0 | 2426 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
2427 |
||
2428 |
manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
2429 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2430 |
#ifndef QT_NO_OPENSSL |
|
2431 |
manager.disconnect(SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), |
|
2432 |
this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); |
|
2433 |
#endif |
|
2434 |
||
2435 |
QFETCH(QNetworkReply::NetworkError, expectedError); |
|
2436 |
QEXPECT_FAIL("ftp-on-socks", "QFtp is too limited and won't accept non-FTP proxies", Abort); |
|
2437 |
QCOMPARE(reply->error(), expectedError); |
|
2438 |
||
2439 |
// Verify that the factory was called properly |
|
2440 |
QCOMPARE(proxyFactory->callCount, 1); |
|
2441 |
QCOMPARE(proxyFactory->lastQuery, QNetworkProxyQuery(theUrl)); |
|
2442 |
||
2443 |
if (expectedError == QNetworkReply::NoError) { |
|
2444 |
// request succeeded |
|
2445 |
QCOMPARE(reader.data, reference.readAll()); |
|
2446 |
||
2447 |
// now verify that the proxies worked: |
|
2448 |
QFETCH(QNetworkProxy, proxyUsed); |
|
2449 |
if (proxyUsed.type() == QNetworkProxy::NoProxy) { |
|
2450 |
QCOMPARE(authspy.count(), 0); |
|
2451 |
} else { |
|
2452 |
if (QByteArray(QTest::currentDataTag()).startsWith("ftp-")) |
|
2453 |
return; // No authentication with current FTP or with FTP proxies |
|
2454 |
QCOMPARE(authspy.count(), 1); |
|
2455 |
QCOMPARE(qvariant_cast<QNetworkProxy>(authspy.at(0).at(0)), proxyUsed); |
|
2456 |
} |
|
2457 |
} else { |
|
2458 |
// request failed |
|
2459 |
QCOMPARE(authspy.count(), 0); |
|
2460 |
} |
|
2461 |
} |
|
2462 |
||
2463 |
void tst_QNetworkReply::ioPutToFileFromFile_data() |
|
2464 |
{ |
|
2465 |
QTest::addColumn<QString>("fileName"); |
|
2466 |
||
2467 |
QTest::newRow("empty") << SRCDIR "/empty"; |
|
2468 |
QTest::newRow("real-file") << SRCDIR "/rfc3252.txt"; |
|
2469 |
QTest::newRow("resource") << ":/resource"; |
|
2470 |
QTest::newRow("search-path") << "srcdir:/rfc3252.txt"; |
|
2471 |
} |
|
2472 |
||
2473 |
void tst_QNetworkReply::ioPutToFileFromFile() |
|
2474 |
{ |
|
2475 |
QFETCH(QString, fileName); |
|
2476 |
QFile sourceFile(fileName); |
|
2477 |
QFile targetFile(testFileName); |
|
2478 |
||
2479 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2480 |
||
2481 |
QUrl url = QUrl::fromLocalFile(targetFile.fileName()); |
|
2482 |
QNetworkRequest request(url); |
|
2483 |
QNetworkReplyPtr reply = manager.put(request, &sourceFile); |
|
2484 |
||
2485 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2486 |
QTestEventLoop::instance().enterLoop(10); |
|
2487 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2488 |
||
2489 |
QCOMPARE(reply->url(), url); |
|
2490 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2491 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
2492 |
QVERIFY(reply->readAll().isEmpty()); |
|
2493 |
||
2494 |
QVERIFY(sourceFile.atEnd()); |
|
2495 |
sourceFile.seek(0); // reset it to the beginning |
|
2496 |
||
2497 |
QVERIFY(targetFile.open(QIODevice::ReadOnly)); |
|
2498 |
QCOMPARE(targetFile.size(), sourceFile.size()); |
|
2499 |
QCOMPARE(targetFile.readAll(), sourceFile.readAll()); |
|
2500 |
} |
|
2501 |
||
2502 |
void tst_QNetworkReply::ioPutToFileFromSocket_data() |
|
2503 |
{ |
|
2504 |
putToFile_data(); |
|
2505 |
} |
|
2506 |
||
2507 |
void tst_QNetworkReply::ioPutToFileFromSocket() |
|
2508 |
{ |
|
2509 |
QFile file(testFileName); |
|
2510 |
||
2511 |
QUrl url = QUrl::fromLocalFile(file.fileName()); |
|
2512 |
QNetworkRequest request(url); |
|
2513 |
||
2514 |
QFETCH(QByteArray, data); |
|
2515 |
SocketPair socketpair; |
|
2516 |
socketpair.create(); |
|
2517 |
QVERIFY(socketpair.endPoints[0] && socketpair.endPoints[1]); |
|
2518 |
||
2519 |
socketpair.endPoints[0]->write(data); |
|
2520 |
QNetworkReplyPtr reply = manager.put(QNetworkRequest(url), socketpair.endPoints[1]); |
|
2521 |
socketpair.endPoints[0]->close(); |
|
2522 |
||
2523 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2524 |
QTestEventLoop::instance().enterLoop(10); |
|
2525 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2526 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2527 |
||
2528 |
QCOMPARE(reply->url(), url); |
|
2529 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2530 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
2531 |
QVERIFY(reply->readAll().isEmpty()); |
|
2532 |
||
2533 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
2534 |
QCOMPARE(file.size(), qint64(data.size())); |
|
2535 |
QByteArray contents = file.readAll(); |
|
2536 |
QCOMPARE(contents, data); |
|
2537 |
} |
|
2538 |
||
2539 |
void tst_QNetworkReply::ioPutToFileFromLocalSocket_data() |
|
2540 |
{ |
|
2541 |
putToFile_data(); |
|
2542 |
} |
|
2543 |
||
2544 |
void tst_QNetworkReply::ioPutToFileFromLocalSocket() |
|
2545 |
{ |
|
2546 |
QString socketname = "networkreplytest"; |
|
2547 |
QLocalServer server; |
|
2548 |
if (!server.listen(socketname)) { |
|
2549 |
if (QFile::exists(server.fullServerName())) |
|
2550 |
QFile::remove(server.fullServerName()); |
|
2551 |
QVERIFY(server.listen(socketname)); |
|
2552 |
} |
|
2553 |
QLocalSocket active; |
|
2554 |
active.connectToServer(socketname); |
|
2555 |
QVERIFY2(server.waitForNewConnection(10), server.errorString().toLatin1().constData()); |
|
2556 |
QVERIFY2(active.waitForConnected(10), active.errorString().toLatin1().constData()); |
|
2557 |
QVERIFY2(server.hasPendingConnections(), server.errorString().toLatin1().constData()); |
|
2558 |
QLocalSocket *passive = server.nextPendingConnection(); |
|
2559 |
||
2560 |
QFile file(testFileName); |
|
2561 |
QUrl url = QUrl::fromLocalFile(file.fileName()); |
|
2562 |
QNetworkRequest request(url); |
|
2563 |
||
2564 |
QFETCH(QByteArray, data); |
|
2565 |
active.write(data); |
|
2566 |
active.close(); |
|
2567 |
QNetworkReplyPtr reply = manager.put(QNetworkRequest(url), passive); |
|
2568 |
passive->setParent(reply); |
|
2569 |
||
2570 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2571 |
QTestEventLoop::instance().enterLoop(10); |
|
2572 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2573 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2574 |
||
2575 |
QCOMPARE(reply->url(), url); |
|
2576 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2577 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
2578 |
QVERIFY(reply->readAll().isEmpty()); |
|
2579 |
||
2580 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
2581 |
QCOMPARE(file.size(), qint64(data.size())); |
|
2582 |
QByteArray contents = file.readAll(); |
|
2583 |
QCOMPARE(contents, data); |
|
2584 |
} |
|
2585 |
||
2586 |
void tst_QNetworkReply::ioPutToFileFromProcess_data() |
|
2587 |
{ |
|
2588 |
putToFile_data(); |
|
2589 |
} |
|
2590 |
||
2591 |
void tst_QNetworkReply::ioPutToFileFromProcess() |
|
2592 |
{ |
|
2593 |
#if defined(Q_OS_WINCE) || defined (Q_OS_SYMBIAN) |
|
2594 |
QSKIP("Currently no stdin/out supported for Windows CE / Symbian OS", SkipAll); |
|
2595 |
#endif |
|
2596 |
||
2597 |
#ifdef Q_OS_WIN |
|
2598 |
if (qstrcmp(QTest::currentDataTag(), "small") == 0) |
|
2599 |
QSKIP("When passing a CR-LF-LF sequence through Windows stdio, it gets converted, " |
|
2600 |
"so this test fails. Disabled on Windows", SkipSingle); |
|
2601 |
#endif |
|
2602 |
||
2603 |
#if defined(QT_NO_PROCESS) |
|
2604 |
QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll); |
|
2605 |
#else |
|
2606 |
QFile file(testFileName); |
|
2607 |
||
2608 |
QUrl url = QUrl::fromLocalFile(file.fileName()); |
|
2609 |
QNetworkRequest request(url); |
|
2610 |
||
2611 |
QFETCH(QByteArray, data); |
|
2612 |
QProcess process; |
|
2613 |
process.start("echo/echo all"); |
|
2614 |
process.write(data); |
|
2615 |
process.closeWriteChannel(); |
|
2616 |
||
2617 |
QNetworkReplyPtr reply = manager.put(QNetworkRequest(url), &process); |
|
2618 |
||
2619 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2620 |
QTestEventLoop::instance().enterLoop(10); |
|
2621 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2622 |
||
2623 |
QCOMPARE(reply->url(), url); |
|
2624 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2625 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
2626 |
QVERIFY(reply->readAll().isEmpty()); |
|
2627 |
||
2628 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
2629 |
QCOMPARE(file.size(), qint64(data.size())); |
|
2630 |
QByteArray contents = file.readAll(); |
|
2631 |
QCOMPARE(contents, data); |
|
2632 |
#endif |
|
2633 |
} |
|
2634 |
||
2635 |
void tst_QNetworkReply::ioPutToFtpFromFile_data() |
|
2636 |
{ |
|
2637 |
ioPutToFileFromFile_data(); |
|
2638 |
} |
|
2639 |
||
2640 |
void tst_QNetworkReply::ioPutToFtpFromFile() |
|
2641 |
{ |
|
2642 |
QFETCH(QString, fileName); |
|
2643 |
QFile sourceFile(fileName); |
|
2644 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2645 |
||
2646 |
QUrl url("ftp://" + QtNetworkSettings::serverName()); |
|
2647 |
url.setPath(QString("/qtest/upload/qnetworkaccess-ioPutToFtpFromFile-%1-%2") |
|
2648 |
.arg(QTest::currentDataTag()) |
|
2649 |
.arg(uniqueExtension)); |
|
2650 |
||
2651 |
QNetworkRequest request(url); |
|
2652 |
QNetworkReplyPtr reply = manager.put(request, &sourceFile); |
|
2653 |
||
2654 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2655 |
QTestEventLoop::instance().enterLoop(10); |
|
2656 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2657 |
||
2658 |
QCOMPARE(reply->url(), url); |
|
2659 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2660 |
QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
2661 |
QVERIFY(reply->readAll().isEmpty()); |
|
2662 |
||
2663 |
QVERIFY(sourceFile.atEnd()); |
|
2664 |
sourceFile.seek(0); // reset it to the beginning |
|
2665 |
||
2666 |
// download the file again from FTP to make sure it was uploaded |
|
2667 |
// correctly |
|
2668 |
QFtp ftp; |
|
2669 |
ftp.connectToHost(url.host()); |
|
2670 |
ftp.login(); |
|
2671 |
ftp.get(url.path()); |
|
2672 |
||
2673 |
QObject::connect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2674 |
QTestEventLoop::instance().enterLoop(3); |
|
2675 |
QObject::disconnect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2676 |
||
2677 |
QByteArray uploaded = ftp.readAll(); |
|
2678 |
QCOMPARE(qint64(uploaded.size()), sourceFile.size()); |
|
2679 |
QCOMPARE(uploaded, sourceFile.readAll()); |
|
2680 |
||
2681 |
ftp.close(); |
|
2682 |
QObject::connect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2683 |
QTestEventLoop::instance().enterLoop(10); |
|
2684 |
QObject::disconnect(&ftp, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2685 |
} |
|
2686 |
||
2687 |
void tst_QNetworkReply::ioPutToHttpFromFile_data() |
|
2688 |
{ |
|
2689 |
ioPutToFileFromFile_data(); |
|
2690 |
} |
|
2691 |
||
2692 |
void tst_QNetworkReply::ioPutToHttpFromFile() |
|
2693 |
{ |
|
2694 |
QFETCH(QString, fileName); |
|
2695 |
QFile sourceFile(fileName); |
|
2696 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2697 |
||
2698 |
QUrl url("http://" + QtNetworkSettings::serverName()); |
|
2699 |
url.setPath(QString("/dav/qnetworkaccess-ioPutToHttpFromFile-%1-%2") |
|
2700 |
.arg(QTest::currentDataTag()) |
|
2701 |
.arg(uniqueExtension)); |
|
2702 |
||
2703 |
QNetworkRequest request(url); |
|
2704 |
QNetworkReplyPtr reply = manager.put(request, &sourceFile); |
|
2705 |
||
2706 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2707 |
QTestEventLoop::instance().enterLoop(10); |
|
2708 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2709 |
||
2710 |
QCOMPARE(reply->url(), url); |
|
2711 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2712 |
||
2713 |
// verify that the HTTP status code is 201 Created |
|
2714 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 201); |
|
2715 |
||
2716 |
QVERIFY(sourceFile.atEnd()); |
|
2717 |
sourceFile.seek(0); // reset it to the beginning |
|
2718 |
||
2719 |
// download the file again from HTTP to make sure it was uploaded |
|
2720 |
// correctly |
|
2721 |
reply = manager.get(request); |
|
2722 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2723 |
QTestEventLoop::instance().enterLoop(10); |
|
2724 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2725 |
||
2726 |
QCOMPARE(reply->url(), url); |
|
2727 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2728 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok |
|
2729 |
||
2730 |
QCOMPARE(reply->readAll(), sourceFile.readAll()); |
|
2731 |
} |
|
2732 |
||
2733 |
void tst_QNetworkReply::ioPostToHttpFromFile_data() |
|
2734 |
{ |
|
2735 |
ioPutToFileFromFile_data(); |
|
2736 |
} |
|
2737 |
||
2738 |
void tst_QNetworkReply::ioPostToHttpFromFile() |
|
2739 |
{ |
|
2740 |
QFETCH(QString, fileName); |
|
2741 |
QFile sourceFile(fileName); |
|
2742 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2743 |
||
2744 |
QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi"); |
|
2745 |
QNetworkRequest request(url); |
|
2746 |
QNetworkReplyPtr reply = manager.post(request, &sourceFile); |
|
2747 |
||
2748 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2749 |
QTestEventLoop::instance().enterLoop(10); |
|
2750 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2751 |
||
2752 |
QCOMPARE(reply->url(), url); |
|
2753 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2754 |
||
2755 |
// verify that the HTTP status code is 200 Ok |
|
2756 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
2757 |
||
2758 |
QVERIFY(sourceFile.atEnd()); |
|
2759 |
sourceFile.seek(0); // reset it to the beginning |
|
2760 |
||
2761 |
QCOMPARE(reply->readAll().trimmed(), md5sum(sourceFile.readAll()).toHex()); |
|
2762 |
} |
|
2763 |
||
2764 |
void tst_QNetworkReply::ioPostToHttpFromSocket_data() |
|
2765 |
{ |
|
2766 |
QTest::addColumn<QByteArray>("data"); |
|
2767 |
QTest::addColumn<QByteArray>("md5sum"); |
|
2768 |
QTest::addColumn<QUrl>("url"); |
|
2769 |
QTest::addColumn<QNetworkProxy>("proxy"); |
|
2770 |
QTest::addColumn<int>("authenticationRequiredCount"); |
|
2771 |
QTest::addColumn<int>("proxyAuthenticationRequiredCount"); |
|
2772 |
||
2773 |
for (int i = 0; i < proxies.count(); ++i) |
|
2774 |
for (int auth = 0; auth < 2; ++auth) { |
|
2775 |
QUrl url; |
|
2776 |
if (auth) |
|
2777 |
url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; |
|
2778 |
else |
|
2779 |
url = "http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi"; |
|
2780 |
||
2781 |
QNetworkProxy proxy = proxies.at(i).proxy; |
|
2782 |
QByteArray testsuffix = QByteArray(auth ? "+auth" : "") + proxies.at(i).tag; |
|
2783 |
int proxyauthcount = proxies.at(i).requiresAuthentication; |
|
2784 |
||
2785 |
QByteArray data; |
|
2786 |
data = ""; |
|
2787 |
QTest::newRow("empty" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2788 |
||
2789 |
data = "This is a normal message."; |
|
2790 |
QTest::newRow("generic" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2791 |
||
2792 |
data = "This is a message to show that Qt rocks!\r\n\n"; |
|
2793 |
QTest::newRow("small" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2794 |
||
2795 |
data = QByteArray("abcd\0\1\2\abcd",12); |
|
2796 |
QTest::newRow("with-nul" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2797 |
||
2798 |
data = QByteArray(4097, '\4'); |
|
2799 |
QTest::newRow("4k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2800 |
||
2801 |
data = QByteArray(128*1024+1, '\177'); |
|
2802 |
QTest::newRow("128k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; |
|
2803 |
} |
|
2804 |
} |
|
2805 |
||
2806 |
void tst_QNetworkReply::ioPostToHttpFromSocket() |
|
2807 |
{ |
|
2808 |
qRegisterMetaType<QNetworkProxy>(); // for QSignalSpy |
|
2809 |
qRegisterMetaType<QAuthenticator *>(); |
|
2810 |
qRegisterMetaType<QNetworkReply *>(); |
|
2811 |
||
2812 |
QFETCH(QByteArray, data); |
|
2813 |
QFETCH(QUrl, url); |
|
2814 |
QFETCH(QNetworkProxy, proxy); |
|
2815 |
SocketPair socketpair; |
|
2816 |
socketpair.create(); |
|
2817 |
QVERIFY(socketpair.endPoints[0] && socketpair.endPoints[1]); |
|
2818 |
||
2819 |
socketpair.endPoints[0]->write(data); |
|
2820 |
||
2821 |
QNetworkRequest request(url); |
|
2822 |
manager.setProxy(proxy); |
|
2823 |
QNetworkReplyPtr reply = manager.post(QNetworkRequest(url), socketpair.endPoints[1]); |
|
2824 |
socketpair.endPoints[0]->close(); |
|
2825 |
||
2826 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2827 |
connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
2828 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2829 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2830 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2831 |
||
2832 |
QSignalSpy authenticationRequiredSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2833 |
QSignalSpy proxyAuthenticationRequiredSpy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2834 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2835 |
QTestEventLoop::instance().enterLoop(12); |
0 | 2836 |
disconnect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
2837 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
2838 |
disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2839 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2840 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2841 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2842 |
||
2843 |
QCOMPARE(reply->url(), url); |
|
2844 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
2845 |
// verify that the HTTP status code is 200 Ok |
|
2846 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
2847 |
||
2848 |
QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); |
|
2849 |
||
2850 |
QTEST(authenticationRequiredSpy.count(), "authenticationRequiredCount"); |
|
2851 |
QTEST(proxyAuthenticationRequiredSpy.count(), "proxyAuthenticationRequiredCount"); |
|
2852 |
} |
|
2853 |
||
2854 |
// this tests checks if rewinding the POST-data to some place in the middle |
|
2855 |
// worked. |
|
2856 |
void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileToEnd() |
|
2857 |
{ |
|
2858 |
QFile sourceFile(SRCDIR "/rfc3252.txt"); |
|
2859 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2860 |
// seeking to the middle |
|
2861 |
sourceFile.seek(sourceFile.size() / 2); |
|
2862 |
||
2863 |
QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; |
|
2864 |
QNetworkRequest request(url); |
|
2865 |
QNetworkReplyPtr reply = manager.post(QNetworkRequest(url), &sourceFile); |
|
2866 |
||
2867 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2868 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2869 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2870 |
||
2871 |
QTestEventLoop::instance().enterLoop(2); |
|
2872 |
disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2873 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2874 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2875 |
||
2876 |
// compare half data |
|
2877 |
sourceFile.seek(sourceFile.size() / 2); |
|
2878 |
QByteArray data = sourceFile.readAll(); |
|
2879 |
QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); |
|
2880 |
} |
|
2881 |
||
2882 |
void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileFiveBytes() |
|
2883 |
{ |
|
2884 |
QFile sourceFile(SRCDIR "/rfc3252.txt"); |
|
2885 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
2886 |
// seeking to the middle |
|
2887 |
sourceFile.seek(sourceFile.size() / 2); |
|
2888 |
||
2889 |
QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; |
|
2890 |
QNetworkRequest request(url); |
|
2891 |
// only send 5 bytes |
|
2892 |
request.setHeader(QNetworkRequest::ContentLengthHeader, 5); |
|
2893 |
QVERIFY(request.header(QNetworkRequest::ContentLengthHeader).isValid()); |
|
2894 |
QNetworkReplyPtr reply = manager.post(request, &sourceFile); |
|
2895 |
||
2896 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2897 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2898 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2899 |
||
2900 |
QTestEventLoop::instance().enterLoop(2); |
|
2901 |
disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2902 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2903 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2904 |
||
2905 |
// compare half data |
|
2906 |
sourceFile.seek(sourceFile.size() / 2); |
|
2907 |
QByteArray data = sourceFile.read(5); |
|
2908 |
QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); |
|
2909 |
} |
|
2910 |
||
2911 |
void tst_QNetworkReply::ioPostToHttpFromMiddleOfQBufferFiveBytes() |
|
2912 |
{ |
|
2913 |
// test needed since a QBuffer goes with a different codepath than the QFile |
|
2914 |
// tested in ioPostToHttpFromMiddleOfFileFiveBytes |
|
2915 |
QBuffer uploadBuffer; |
|
2916 |
uploadBuffer.open(QIODevice::ReadWrite); |
|
2917 |
uploadBuffer.write("1234567890"); |
|
2918 |
uploadBuffer.seek(5); |
|
2919 |
||
2920 |
QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; |
|
2921 |
QNetworkRequest request(url); |
|
2922 |
QNetworkReplyPtr reply = manager.post(request, &uploadBuffer); |
|
2923 |
||
2924 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2925 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2926 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2927 |
||
2928 |
QTestEventLoop::instance().enterLoop(2); |
|
2929 |
disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2930 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2931 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
2932 |
||
2933 |
// compare half data |
|
2934 |
uploadBuffer.seek(5); |
|
2935 |
QByteArray data = uploadBuffer.read(5); |
|
2936 |
QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); |
|
2937 |
} |
|
2938 |
||
2939 |
||
2940 |
void tst_QNetworkReply::ioPostToHttpNoBufferFlag() |
|
2941 |
{ |
|
2942 |
QByteArray data = QByteArray("daaaaaaataaaaaaa"); |
|
2943 |
// create a sequential QIODevice by feeding the data into a local TCP server |
|
2944 |
SocketPair socketpair; |
|
2945 |
socketpair.create(); |
|
2946 |
QVERIFY(socketpair.endPoints[0] && socketpair.endPoints[1]); |
|
2947 |
socketpair.endPoints[0]->write(data); |
|
2948 |
||
2949 |
QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; |
|
2950 |
QNetworkRequest request(url); |
|
2951 |
// disallow buffering |
|
2952 |
request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true); |
|
2953 |
request.setHeader(QNetworkRequest::ContentLengthHeader, data.size()); |
|
2954 |
QNetworkReplyPtr reply = manager.post(request, socketpair.endPoints[1]); |
|
2955 |
socketpair.endPoints[0]->close(); |
|
2956 |
||
2957 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
2958 |
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2959 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2960 |
||
2961 |
QTestEventLoop::instance().enterLoop(2); |
|
2962 |
disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
|
2963 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
2964 |
||
2965 |
// verify: error code is QNetworkReply::ContentReSendError |
|
2966 |
QCOMPARE(reply->error(), QNetworkReply::ContentReSendError); |
|
2967 |
} |
|
2968 |
||
2969 |
#ifndef QT_NO_OPENSSL |
|
2970 |
class SslServer : public QTcpServer { |
|
2971 |
Q_OBJECT |
|
2972 |
public: |
|
2973 |
SslServer() : socket(0) {}; |
|
2974 |
void incomingConnection(int socketDescriptor) { |
|
2975 |
QSslSocket *serverSocket = new QSslSocket; |
|
2976 |
serverSocket->setParent(this); |
|
2977 |
||
2978 |
if (serverSocket->setSocketDescriptor(socketDescriptor)) { |
|
2979 |
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(encryptedSlot())); |
|
2980 |
serverSocket->setProtocol(QSsl::AnyProtocol); |
|
2981 |
connect(serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), serverSocket, SLOT(ignoreSslErrors())); |
|
2982 |
serverSocket->setLocalCertificate (SRCDIR "/certs/server.pem"); |
|
2983 |
serverSocket->setPrivateKey (SRCDIR "/certs/server.key"); |
|
2984 |
serverSocket->startServerEncryption(); |
|
2985 |
} else { |
|
2986 |
delete serverSocket; |
|
2987 |
} |
|
2988 |
} |
|
2989 |
signals: |
|
2990 |
void newEncryptedConnection(); |
|
2991 |
public slots: |
|
2992 |
void encryptedSlot() { |
|
2993 |
socket = (QSslSocket*) sender(); |
|
2994 |
emit newEncryptedConnection(); |
|
2995 |
} |
|
2996 |
public: |
|
2997 |
QSslSocket *socket; |
|
2998 |
}; |
|
2999 |
||
3000 |
// very similar to ioPostToHttpUploadProgress but for SSL |
|
3001 |
void tst_QNetworkReply::ioPostToHttpsUploadProgress() |
|
3002 |
{ |
|
3003 |
QFile sourceFile(SRCDIR "/bigfile"); |
|
3004 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
3005 |
||
3006 |
// emulate a minimal https server |
|
3007 |
SslServer server; |
|
3008 |
server.listen(QHostAddress(QHostAddress::LocalHost), 0); |
|
3009 |
||
3010 |
// create the request |
|
3011 |
QUrl url = QUrl(QString("https://127.0.0.1:%1/").arg(server.serverPort())); |
|
3012 |
QNetworkRequest request(url); |
|
3013 |
QNetworkReplyPtr reply = manager.post(request, &sourceFile); |
|
3014 |
QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); |
|
3015 |
connect(&server, SIGNAL(newEncryptedConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3016 |
connect(reply, SIGNAL(sslErrors(const QList<QSslError>&)), reply, SLOT(ignoreSslErrors())); |
|
3017 |
||
3018 |
// get the request started and the incoming socket connected |
|
3019 |
QTestEventLoop::instance().enterLoop(10); |
|
3020 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3021 |
QTcpSocket *incomingSocket = server.socket; |
|
3022 |
QVERIFY(incomingSocket); |
|
3023 |
disconnect(&server, SIGNAL(newEncryptedConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3024 |
||
3025 |
||
3026 |
incomingSocket->setReadBufferSize(1*1024); |
|
3027 |
QTestEventLoop::instance().enterLoop(2); |
|
3028 |
// some progress should have been made |
|
3029 |
QList<QVariant> args = spy.last(); |
|
3030 |
qDebug() << "tst_QNetworkReply::ioPostToHttpsUploadProgress" |
|
3031 |
<< args.at(0).toLongLong() |
|
3032 |
<< sourceFile.size() |
|
3033 |
<< spy.size(); |
|
3034 |
QVERIFY(!args.isEmpty()); |
|
3035 |
QVERIFY(args.at(0).toLongLong() > 0); |
|
3036 |
// FIXME this is where it messes up |
|
3037 |
||
3038 |
QEXPECT_FAIL("", "Either the readBufferSize of QSslSocket is broken or we do upload too much. Hm.", Abort); |
|
3039 |
QVERIFY(args.at(0).toLongLong() != sourceFile.size()); |
|
3040 |
||
3041 |
incomingSocket->setReadBufferSize(32*1024); |
|
3042 |
incomingSocket->read(16*1024); |
|
3043 |
QTestEventLoop::instance().enterLoop(2); |
|
3044 |
// some more progress than before |
|
3045 |
QList<QVariant> args2 = spy.last(); |
|
3046 |
QVERIFY(!args2.isEmpty()); |
|
3047 |
QVERIFY(args2.at(0).toLongLong() > args.at(0).toLongLong()); |
|
3048 |
||
3049 |
// set the read buffer to unlimited |
|
3050 |
incomingSocket->setReadBufferSize(0); |
|
3051 |
QTestEventLoop::instance().enterLoop(10); |
|
3052 |
// progress should be finished |
|
3053 |
QList<QVariant> args3 = spy.last(); |
|
3054 |
QVERIFY(!args3.isEmpty()); |
|
3055 |
QVERIFY(args3.at(0).toLongLong() > args2.at(0).toLongLong()); |
|
3056 |
QCOMPARE(args3.at(0).toLongLong(), args3.at(1).toLongLong()); |
|
3057 |
QCOMPARE(args3.at(0).toLongLong(), sourceFile.size()); |
|
3058 |
||
3059 |
// after sending this, the QNAM should emit finished() |
|
3060 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3061 |
incomingSocket->write("HTTP/1.0 200 OK\r\n"); |
|
3062 |
incomingSocket->write("Content-Length: 0\r\n"); |
|
3063 |
incomingSocket->write("\r\n"); |
|
3064 |
QTestEventLoop::instance().enterLoop(10); |
|
3065 |
// not timeouted -> finished() was emitted |
|
3066 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3067 |
||
3068 |
incomingSocket->close(); |
|
3069 |
server.close(); |
|
3070 |
} |
|
3071 |
#endif |
|
3072 |
||
3073 |
void tst_QNetworkReply::ioPostToHttpUploadProgress() |
|
3074 |
{ |
|
3075 |
QFile sourceFile(SRCDIR "/bigfile"); |
|
3076 |
QVERIFY(sourceFile.open(QIODevice::ReadOnly)); |
|
3077 |
||
3078 |
// emulate a minimal http server |
|
3079 |
QTcpServer server; |
|
3080 |
server.listen(QHostAddress(QHostAddress::LocalHost), 0); |
|
3081 |
||
3082 |
// create the request |
|
3083 |
QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort())); |
|
3084 |
QNetworkRequest request(url); |
|
3085 |
QNetworkReplyPtr reply = manager.post(request, &sourceFile); |
|
3086 |
QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); |
|
3087 |
connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3088 |
||
3089 |
// get the request started and the incoming socket connected |
|
3090 |
QTestEventLoop::instance().enterLoop(10); |
|
3091 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3092 |
QTcpSocket *incomingSocket = server.nextPendingConnection(); |
|
3093 |
QVERIFY(incomingSocket); |
|
3094 |
disconnect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3095 |
||
3096 |
incomingSocket->setReadBufferSize(1*1024); |
|
3097 |
QTestEventLoop::instance().enterLoop(2); |
|
3098 |
// some progress should have been made |
|
3099 |
QList<QVariant> args = spy.last(); |
|
3100 |
QVERIFY(!args.isEmpty()); |
|
3101 |
QVERIFY(args.at(0).toLongLong() > 0); |
|
3102 |
||
3103 |
incomingSocket->setReadBufferSize(32*1024); |
|
3104 |
incomingSocket->read(16*1024); |
|
3105 |
QTestEventLoop::instance().enterLoop(2); |
|
3106 |
// some more progress than before |
|
3107 |
QList<QVariant> args2 = spy.last(); |
|
3108 |
QVERIFY(!args2.isEmpty()); |
|
3109 |
QVERIFY(args2.at(0).toLongLong() > args.at(0).toLongLong()); |
|
3110 |
||
3111 |
// set the read buffer to unlimited |
|
3112 |
incomingSocket->setReadBufferSize(0); |
|
3113 |
QTestEventLoop::instance().enterLoop(10); |
|
3114 |
// progress should be finished |
|
3115 |
QList<QVariant> args3 = spy.last(); |
|
3116 |
QVERIFY(!args3.isEmpty()); |
|
3117 |
QVERIFY(args3.at(0).toLongLong() > args2.at(0).toLongLong()); |
|
3118 |
QCOMPARE(args3.at(0).toLongLong(), args3.at(1).toLongLong()); |
|
3119 |
QCOMPARE(args3.at(0).toLongLong(), sourceFile.size()); |
|
3120 |
||
3121 |
// after sending this, the QNAM should emit finished() |
|
3122 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3123 |
incomingSocket->write("HTTP/1.0 200 OK\r\n"); |
|
3124 |
incomingSocket->write("Content-Length: 0\r\n"); |
|
3125 |
incomingSocket->write("\r\n"); |
|
3126 |
QTestEventLoop::instance().enterLoop(10); |
|
3127 |
// not timeouted -> finished() was emitted |
|
3128 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3129 |
||
3130 |
incomingSocket->close(); |
|
3131 |
server.close(); |
|
3132 |
} |
|
3133 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3134 |
void tst_QNetworkReply::ioPostToHttpEmptyUploadProgress() |
0 | 3135 |
{ |
3136 |
QByteArray ba; |
|
3137 |
ba.resize(0); |
|
3138 |
QBuffer buffer(&ba,0); |
|
3139 |
QVERIFY(buffer.open(QIODevice::ReadOnly)); |
|
3140 |
||
3141 |
// emulate a minimal http server |
|
3142 |
QTcpServer server; |
|
3143 |
server.listen(QHostAddress(QHostAddress::LocalHost), 0); |
|
3144 |
||
3145 |
// create the request |
|
3146 |
QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort())); |
|
3147 |
QNetworkRequest request(url); |
|
3148 |
QNetworkReplyPtr reply = manager.post(request, &buffer); |
|
3149 |
QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); |
|
3150 |
connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3151 |
||
3152 |
||
3153 |
// get the request started and the incoming socket connected |
|
3154 |
QTestEventLoop::instance().enterLoop(10); |
|
3155 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3156 |
QTcpSocket *incomingSocket = server.nextPendingConnection(); |
|
3157 |
QVERIFY(incomingSocket); |
|
3158 |
||
3159 |
// after sending this, the QNAM should emit finished() |
|
3160 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3161 |
incomingSocket->write("HTTP/1.0 200 OK\r\n"); |
|
3162 |
incomingSocket->write("Content-Length: 0\r\n"); |
|
3163 |
incomingSocket->write("\r\n"); |
|
3164 |
incomingSocket->flush(); |
|
3165 |
QTestEventLoop::instance().enterLoop(10); |
|
3166 |
// not timeouted -> finished() was emitted |
|
3167 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3168 |
||
3169 |
// final check: only 1 uploadProgress has been emitted |
|
3170 |
QVERIFY(spy.length() == 1); |
|
3171 |
QList<QVariant> args = spy.last(); |
|
3172 |
QVERIFY(!args.isEmpty()); |
|
3173 |
QCOMPARE(args.at(0).toLongLong(), buffer.size()); |
|
3174 |
QCOMPARE(args.at(0).toLongLong(), buffer.size()); |
|
3175 |
||
3176 |
incomingSocket->close(); |
|
3177 |
server.close(); |
|
3178 |
} |
|
3179 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3180 |
void tst_QNetworkReply::lastModifiedHeaderForFile() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3181 |
{ |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3182 |
QFileInfo fileInfo(SRCDIR "/bigfile"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3183 |
QVERIFY(fileInfo.exists()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3184 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3185 |
QUrl url = QUrl::fromLocalFile(fileInfo.filePath()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3186 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3187 |
QNetworkRequest request(url); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3188 |
QNetworkReplyPtr reply = manager.head(request); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3189 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3190 |
QTestEventLoop::instance().enterLoop(10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3191 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3192 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3193 |
QDateTime header = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3194 |
QCOMPARE(header, fileInfo.lastModified()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3195 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3196 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3197 |
void tst_QNetworkReply::lastModifiedHeaderForHttp() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3198 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3199 |
// Tue, 22 May 2007 12:04:57 GMT according to webserver |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3200 |
QUrl url = "http://" + QtNetworkSettings::serverName() + "/gif/fluke.gif"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3201 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3202 |
QNetworkRequest request(url); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3203 |
QNetworkReplyPtr reply = manager.head(request); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3204 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3205 |
QTestEventLoop::instance().enterLoop(10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3206 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3207 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3208 |
QDateTime header = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3209 |
QDateTime realDate = QDateTime::fromString("2007-05-22T12:04:57", Qt::ISODate); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3210 |
realDate.setTimeSpec(Qt::UTC); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3211 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3212 |
QCOMPARE(header, realDate); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3213 |
} |
0 | 3214 |
|
3215 |
void tst_QNetworkReply::rateControl_data() |
|
3216 |
{ |
|
3217 |
QTest::addColumn<int>("rate"); |
|
3218 |
||
3219 |
QTest::newRow("15") << 15; |
|
3220 |
QTest::newRow("40") << 40; |
|
3221 |
QTest::newRow("73") << 73; |
|
3222 |
QTest::newRow("80") << 80; |
|
3223 |
QTest::newRow("125") << 125; |
|
3224 |
QTest::newRow("250") << 250; |
|
3225 |
QTest::newRow("1024") << 1024; |
|
3226 |
} |
|
3227 |
||
3228 |
void tst_QNetworkReply::rateControl() |
|
3229 |
{ |
|
3230 |
QSKIP("Test disabled -- only for manual purposes", SkipAll); |
|
3231 |
// this function tests that we aren't reading from the network |
|
3232 |
// faster than the data is being consumed. |
|
3233 |
QFETCH(int, rate); |
|
3234 |
||
3235 |
// ask for 20 seconds worth of data |
|
3236 |
FastSender sender(20 * rate * 1024); |
|
3237 |
||
3238 |
QNetworkRequest request("debugpipe://localhost:" + QString::number(sender.serverPort())); |
|
3239 |
QNetworkReplyPtr reply = manager.get(request); |
|
3240 |
reply->setReadBufferSize(32768); |
|
3241 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3242 |
||
3243 |
RateControlledReader reader(reply, rate); |
|
3244 |
||
3245 |
// this test is designed to run for 25 seconds at most |
|
3246 |
QTime loopTime; |
|
3247 |
loopTime.start(); |
|
3248 |
QTestEventLoop::instance().enterLoop(40); |
|
3249 |
int elapsedTime = loopTime.elapsed(); |
|
3250 |
||
3251 |
qDebug() << "tst_QNetworkReply::rateControl" << "send rate:" << sender.transferRate; |
|
3252 |
qDebug() << "tst_QNetworkReply::rateControl" << "receive rate:" << reader.totalBytesRead * 1000 / elapsedTime |
|
3253 |
<< "(it received" << reader.totalBytesRead << "bytes in" << elapsedTime << "ms)"; |
|
3254 |
||
3255 |
sender.wait(); |
|
3256 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3257 |
||
3258 |
QCOMPARE(reply->url(), request.url()); |
|
3259 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
3260 |
||
3261 |
QVERIFY(sender.transferRate != -1); |
|
3262 |
int minRate = rate * 1024 * 9 / 10; |
|
3263 |
int maxRate = rate * 1024 * 11 / 10; |
|
3264 |
QVERIFY(sender.transferRate >= minRate); |
|
3265 |
QVERIFY(sender.transferRate <= maxRate); |
|
3266 |
} |
|
3267 |
||
3268 |
void tst_QNetworkReply::downloadProgress_data() |
|
3269 |
{ |
|
3270 |
QTest::addColumn<int>("loopCount"); |
|
3271 |
||
3272 |
QTest::newRow("empty") << 0; |
|
3273 |
QTest::newRow("small") << 4; |
|
3274 |
#ifndef Q_OS_SYMBIAN |
|
3275 |
QTest::newRow("big") << 4096; |
|
3276 |
#else |
|
3277 |
// it can run even with 4096 |
|
3278 |
// but it takes lot time |
|
3279 |
//especially on emulator |
|
3280 |
QTest::newRow("big") << 1024; |
|
3281 |
#endif |
|
3282 |
} |
|
3283 |
||
3284 |
void tst_QNetworkReply::downloadProgress() |
|
3285 |
{ |
|
3286 |
QTcpServer server; |
|
3287 |
QVERIFY(server.listen()); |
|
3288 |
||
3289 |
QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1"); |
|
3290 |
QNetworkReplyPtr reply = manager.get(request); |
|
3291 |
QSignalSpy spy(reply, SIGNAL(downloadProgress(qint64,qint64))); |
|
3292 |
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), |
|
3293 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3294 |
QVERIFY(spy.isValid()); |
|
3295 |
QVERIFY(!reply->isFinished()); |
|
3296 |
QVERIFY(reply->isRunning()); |
|
3297 |
||
3298 |
QCoreApplication::instance()->processEvents(); |
|
3299 |
if (!server.hasPendingConnections()) |
|
3300 |
server.waitForNewConnection(1000); |
|
3301 |
QVERIFY(server.hasPendingConnections()); |
|
3302 |
QCOMPARE(spy.count(), 0); |
|
3303 |
||
3304 |
QByteArray data(128, 'a'); |
|
3305 |
QTcpSocket *sender = server.nextPendingConnection(); |
|
3306 |
QVERIFY(sender); |
|
3307 |
||
3308 |
QFETCH(int, loopCount); |
|
3309 |
for (int i = 1; i <= loopCount; ++i) { |
|
3310 |
sender->write(data); |
|
3311 |
QVERIFY2(sender->waitForBytesWritten(2000), "Network timeout"); |
|
3312 |
||
3313 |
spy.clear(); |
|
3314 |
QTestEventLoop::instance().enterLoop(2); |
|
3315 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3316 |
QVERIFY(spy.count() > 0); |
|
3317 |
QVERIFY(!reply->isFinished()); |
|
3318 |
QVERIFY(reply->isRunning()); |
|
3319 |
||
3320 |
QList<QVariant> args = spy.last(); |
|
3321 |
QCOMPARE(args.at(0).toInt(), i*data.size()); |
|
3322 |
QCOMPARE(args.at(1).toInt(), -1); |
|
3323 |
} |
|
3324 |
||
3325 |
// close the connection: |
|
3326 |
delete sender; |
|
3327 |
||
3328 |
spy.clear(); |
|
3329 |
QTestEventLoop::instance().enterLoop(2); |
|
3330 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
3331 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3332 |
QVERIFY(spy.count() > 0); |
|
3333 |
QVERIFY(!reply->isRunning()); |
|
3334 |
QVERIFY(reply->isFinished()); |
|
3335 |
||
3336 |
QList<QVariant> args = spy.last(); |
|
3337 |
QCOMPARE(args.at(0).toInt(), loopCount * data.size()); |
|
3338 |
QCOMPARE(args.at(1).toInt(), loopCount * data.size()); |
|
3339 |
} |
|
3340 |
||
3341 |
void tst_QNetworkReply::uploadProgress_data() |
|
3342 |
{ |
|
3343 |
putToFile_data(); |
|
3344 |
} |
|
3345 |
||
3346 |
void tst_QNetworkReply::uploadProgress() |
|
3347 |
{ |
|
3348 |
QFETCH(QByteArray, data); |
|
3349 |
QTcpServer server; |
|
3350 |
QVERIFY(server.listen()); |
|
3351 |
||
3352 |
QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1"); |
|
3353 |
QNetworkReplyPtr reply = manager.put(request, data); |
|
3354 |
QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); |
|
3355 |
QSignalSpy finished(reply, SIGNAL(finished())); |
|
3356 |
QVERIFY(spy.isValid()); |
|
3357 |
QVERIFY(finished.isValid()); |
|
3358 |
||
3359 |
QCoreApplication::instance()->processEvents(); |
|
3360 |
if (!server.hasPendingConnections()) |
|
3361 |
server.waitForNewConnection(1000); |
|
3362 |
QVERIFY(server.hasPendingConnections()); |
|
3363 |
||
3364 |
QTcpSocket *receiver = server.nextPendingConnection(); |
|
3365 |
if (finished.count() == 0) { |
|
3366 |
// it's not finished yet, so wait for it to be |
|
3367 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3368 |
QTestEventLoop::instance().enterLoop(2); |
|
3369 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3370 |
} |
|
3371 |
delete receiver; |
|
3372 |
||
3373 |
QVERIFY(finished.count() > 0); |
|
3374 |
QVERIFY(spy.count() > 0); |
|
3375 |
||
3376 |
QList<QVariant> args = spy.last(); |
|
3377 |
QCOMPARE(args.at(0).toInt(), data.size()); |
|
3378 |
QCOMPARE(args.at(1).toInt(), data.size()); |
|
3379 |
} |
|
3380 |
||
3381 |
void tst_QNetworkReply::chaining_data() |
|
3382 |
{ |
|
3383 |
putToFile_data(); |
|
3384 |
} |
|
3385 |
||
3386 |
void tst_QNetworkReply::chaining() |
|
3387 |
{ |
|
3388 |
QTemporaryFile sourceFile(QDir::currentPath() + "/temp-XXXXXX"); |
|
3389 |
sourceFile.setAutoRemove(true); |
|
3390 |
QVERIFY(sourceFile.open()); |
|
3391 |
||
3392 |
QFETCH(QByteArray, data); |
|
3393 |
QVERIFY(sourceFile.write(data) == data.size()); |
|
3394 |
sourceFile.flush(); |
|
3395 |
QCOMPARE(sourceFile.size(), qint64(data.size())); |
|
3396 |
||
3397 |
QNetworkRequest request(QUrl::fromLocalFile(sourceFile.fileName())); |
|
3398 |
QNetworkReplyPtr getReply = manager.get(request); |
|
3399 |
||
3400 |
QFile targetFile(testFileName); |
|
3401 |
QUrl url = QUrl::fromLocalFile(targetFile.fileName()); |
|
3402 |
request.setUrl(url); |
|
3403 |
QNetworkReplyPtr putReply = manager.put(request, getReply); |
|
3404 |
||
3405 |
connect(putReply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3406 |
QTestEventLoop::instance().enterLoop(10); |
|
3407 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3408 |
||
3409 |
QCOMPARE(getReply->url(), QUrl::fromLocalFile(sourceFile.fileName())); |
|
3410 |
QCOMPARE(getReply->error(), QNetworkReply::NoError); |
|
3411 |
QCOMPARE(getReply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), sourceFile.size()); |
|
3412 |
||
3413 |
QCOMPARE(putReply->url(), url); |
|
3414 |
QCOMPARE(putReply->error(), QNetworkReply::NoError); |
|
3415 |
QCOMPARE(putReply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0)); |
|
3416 |
QVERIFY(putReply->readAll().isEmpty()); |
|
3417 |
||
3418 |
QVERIFY(sourceFile.atEnd()); |
|
3419 |
sourceFile.seek(0); // reset it to the beginning |
|
3420 |
||
3421 |
QVERIFY(targetFile.open(QIODevice::ReadOnly)); |
|
3422 |
QCOMPARE(targetFile.size(), sourceFile.size()); |
|
3423 |
QCOMPARE(targetFile.readAll(), sourceFile.readAll()); |
|
3424 |
} |
|
3425 |
||
3426 |
void tst_QNetworkReply::receiveCookiesFromHttp_data() |
|
3427 |
{ |
|
3428 |
QTest::addColumn<QString>("cookieString"); |
|
3429 |
QTest::addColumn<QList<QNetworkCookie> >("expectedCookiesFromHttp"); |
|
3430 |
QTest::addColumn<QList<QNetworkCookie> >("expectedCookiesInJar"); |
|
3431 |
||
3432 |
QTest::newRow("empty") << "" << QList<QNetworkCookie>() << QList<QNetworkCookie>(); |
|
3433 |
||
3434 |
QList<QNetworkCookie> header, jar; |
|
3435 |
QNetworkCookie cookie("a", "b"); |
|
3436 |
header << cookie; |
|
3437 |
cookie.setDomain(QtNetworkSettings::serverName()); |
|
3438 |
cookie.setPath("/qtest/cgi-bin/"); |
|
3439 |
jar << cookie; |
|
3440 |
QTest::newRow("simple-cookie") << "a=b" << header << jar; |
|
3441 |
||
3442 |
header << QNetworkCookie("c", "d"); |
|
3443 |
cookie.setName("c"); |
|
3444 |
cookie.setValue("d"); |
|
3445 |
jar << cookie; |
|
3446 |
QTest::newRow("two-cookies") << "a=b, c=d" << header << jar; |
|
3447 |
QTest::newRow("two-cookies-2") << "a=b\nc=d" << header << jar; |
|
3448 |
||
3449 |
header.clear(); |
|
3450 |
jar.clear(); |
|
3451 |
cookie = QNetworkCookie("a", "b"); |
|
3452 |
cookie.setPath("/not/part-of-path"); |
|
3453 |
header << cookie; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3454 |
cookie.setDomain(QtNetworkSettings::serverName()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3455 |
jar << cookie; |
0 | 3456 |
QTest::newRow("invalid-cookie-path") << "a=b; path=/not/part-of-path" << header << jar; |
3457 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3458 |
jar.clear(); |
0 | 3459 |
cookie = QNetworkCookie("a", "b"); |
3460 |
cookie.setDomain(".example.com"); |
|
3461 |
header.clear(); |
|
3462 |
header << cookie; |
|
3463 |
QTest::newRow("invalid-cookie-domain") << "a=b; domain=.example.com" << header << jar; |
|
3464 |
} |
|
3465 |
||
3466 |
void tst_QNetworkReply::receiveCookiesFromHttp() |
|
3467 |
{ |
|
3468 |
QFETCH(QString, cookieString); |
|
3469 |
||
3470 |
QByteArray data = cookieString.toLatin1() + '\n'; |
|
3471 |
QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/set-cookie.cgi"); |
|
3472 |
QNetworkRequest request(url); |
|
3473 |
QNetworkReplyPtr reply; |
|
3474 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data)); |
|
3475 |
||
3476 |
QCOMPARE(reply->url(), url); |
|
3477 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
3478 |
||
3479 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok |
|
3480 |
||
3481 |
QList<QNetworkCookie> setCookies = |
|
3482 |
qvariant_cast<QList<QNetworkCookie> >(reply->header(QNetworkRequest::SetCookieHeader)); |
|
3483 |
QTEST(setCookies, "expectedCookiesFromHttp"); |
|
3484 |
QTEST(cookieJar->allCookies(), "expectedCookiesInJar"); |
|
3485 |
} |
|
3486 |
||
3487 |
void tst_QNetworkReply::sendCookies_data() |
|
3488 |
{ |
|
3489 |
QTest::addColumn<QList<QNetworkCookie> >("cookiesToSet"); |
|
3490 |
QTest::addColumn<QString>("expectedCookieString"); |
|
3491 |
||
3492 |
QList<QNetworkCookie> list; |
|
3493 |
QTest::newRow("empty") << list << ""; |
|
3494 |
||
3495 |
QNetworkCookie cookie("a", "b"); |
|
3496 |
cookie.setPath("/"); |
|
3497 |
cookie.setDomain("example.com"); |
|
3498 |
list << cookie; |
|
3499 |
QTest::newRow("no-match-domain") << list << ""; |
|
3500 |
||
3501 |
cookie.setDomain(QtNetworkSettings::serverName()); |
|
3502 |
cookie.setPath("/something/else"); |
|
3503 |
list << cookie; |
|
3504 |
QTest::newRow("no-match-path") << list << ""; |
|
3505 |
||
3506 |
cookie.setPath("/"); |
|
3507 |
list << cookie; |
|
3508 |
QTest::newRow("simple-cookie") << list << "a=b"; |
|
3509 |
||
3510 |
cookie.setPath("/qtest"); |
|
3511 |
cookie.setValue("longer"); |
|
3512 |
list << cookie; |
|
3513 |
QTest::newRow("two-cookies") << list << "a=longer; a=b"; |
|
3514 |
||
3515 |
list.clear(); |
|
3516 |
cookie = QNetworkCookie("a", "b"); |
|
3517 |
cookie.setPath("/"); |
|
3518 |
cookie.setDomain("." + QtNetworkSettings::serverDomainName()); |
|
3519 |
list << cookie; |
|
3520 |
QTest::newRow("domain-match") << list << "a=b"; |
|
3521 |
||
3522 |
// but it shouldn't match this: |
|
3523 |
cookie.setDomain(QtNetworkSettings::serverDomainName()); |
|
3524 |
list << cookie; |
|
3525 |
QTest::newRow("domain-match-2") << list << "a=b"; |
|
3526 |
} |
|
3527 |
||
3528 |
void tst_QNetworkReply::sendCookies() |
|
3529 |
{ |
|
3530 |
QFETCH(QString, expectedCookieString); |
|
3531 |
QFETCH(QList<QNetworkCookie>, cookiesToSet); |
|
3532 |
cookieJar->setAllCookies(cookiesToSet); |
|
3533 |
||
3534 |
QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/get-cookie.cgi"); |
|
3535 |
QNetworkRequest request(url); |
|
3536 |
QNetworkReplyPtr reply; |
|
3537 |
RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply)); |
|
3538 |
||
3539 |
QCOMPARE(reply->url(), url); |
|
3540 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
3541 |
||
3542 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok |
|
3543 |
||
3544 |
QCOMPARE(QString::fromLatin1(reply->readAll()).trimmed(), expectedCookieString); |
|
3545 |
} |
|
3546 |
||
3547 |
void tst_QNetworkReply::nestedEventLoops_slot() |
|
3548 |
{ |
|
3549 |
QEventLoop subloop; |
|
3550 |
||
3551 |
// 16 seconds: fluke times out in 15 seconds, which triggers a QTcpSocket error |
|
3552 |
QTimer::singleShot(16000, &subloop, SLOT(quit())); |
|
3553 |
subloop.exec(); |
|
3554 |
||
3555 |
QTestEventLoop::instance().exitLoop(); |
|
3556 |
} |
|
3557 |
||
3558 |
void tst_QNetworkReply::nestedEventLoops() |
|
3559 |
{ |
|
3560 |
// Slightly fragile test, it may not be testing anything |
|
3561 |
// This is certifying that we're not running into the same issue |
|
3562 |
// that QHttp had (task 200432): the QTcpSocket connection is |
|
3563 |
// closed by the remote end because of the kept-alive HTTP |
|
3564 |
// connection timed out. |
|
3565 |
// |
|
3566 |
// The exact time required for this to happen is not exactly |
|
3567 |
// defined. Our server (Apache httpd) times out after 15 |
|
3568 |
// seconds. (see above) |
|
3569 |
||
3570 |
qDebug("Takes 16 seconds to run, please wait"); |
|
3571 |
qRegisterMetaType<QNetworkReply::NetworkError>(); |
|
3572 |
||
3573 |
QUrl url("http://" + QtNetworkSettings::serverName()); |
|
3574 |
QNetworkRequest request(url); |
|
3575 |
QNetworkReplyPtr reply = manager.get(request); |
|
3576 |
||
3577 |
QSignalSpy finishedspy(reply, SIGNAL(finished())); |
|
3578 |
QSignalSpy errorspy(reply, SIGNAL(error(QNetworkReply::NetworkError))); |
|
3579 |
||
3580 |
connect(reply, SIGNAL(finished()), SLOT(nestedEventLoops_slot())); |
|
3581 |
QTestEventLoop::instance().enterLoop(20); |
|
3582 |
QVERIFY2(!QTestEventLoop::instance().timeout(), "Network timeout"); |
|
3583 |
||
3584 |
QCOMPARE(finishedspy.count(), 1); |
|
3585 |
QCOMPARE(errorspy.count(), 0); |
|
3586 |
} |
|
3587 |
||
3588 |
void tst_QNetworkReply::httpProxyCommands_data() |
|
3589 |
{ |
|
3590 |
QTest::addColumn<QUrl>("url"); |
|
3591 |
QTest::addColumn<QByteArray>("responseToSend"); |
|
3592 |
QTest::addColumn<QString>("expectedCommand"); |
|
3593 |
||
3594 |
QTest::newRow("http") |
|
3595 |
<< QUrl("http://0.0.0.0:4443/http-request") |
|
3596 |
<< QByteArray("HTTP/1.0 200 OK\r\nProxy-Connection: close\r\nContent-Length: 1\r\n\r\n1") |
|
3597 |
<< "GET http://0.0.0.0:4443/http-request HTTP/1."; |
|
3598 |
#ifndef QT_NO_OPENSSL |
|
3599 |
QTest::newRow("https") |
|
3600 |
<< QUrl("https://0.0.0.0:4443/https-request") |
|
3601 |
<< QByteArray("HTTP/1.0 200 Connection Established\r\n\r\n") |
|
3602 |
<< "CONNECT 0.0.0.0:4443 HTTP/1."; |
|
3603 |
#endif |
|
3604 |
} |
|
3605 |
||
3606 |
void tst_QNetworkReply::httpProxyCommands() |
|
3607 |
{ |
|
3608 |
QFETCH(QUrl, url); |
|
3609 |
QFETCH(QByteArray, responseToSend); |
|
3610 |
QFETCH(QString, expectedCommand); |
|
3611 |
||
3612 |
MiniHttpServer proxyServer(responseToSend); |
|
3613 |
QNetworkProxy proxy(QNetworkProxy::HttpProxy, "127.0.0.1", proxyServer.serverPort()); |
|
3614 |
||
3615 |
manager.setProxy(proxy); |
|
3616 |
QNetworkReplyPtr reply = manager.get(QNetworkRequest(url)); |
|
3617 |
manager.setProxy(QNetworkProxy()); |
|
3618 |
||
3619 |
// wait for the finished signal |
|
3620 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3621 |
||
3622 |
QTestEventLoop::instance().enterLoop(1); |
|
3623 |
||
3624 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3625 |
||
3626 |
//qDebug() << reply->error() << reply->errorString(); |
|
3627 |
||
3628 |
// we don't really care if the request succeeded |
|
3629 |
// especially since it won't succeed in the HTTPS case |
|
3630 |
// so just check that the command was correct |
|
3631 |
||
3632 |
QString receivedHeader = proxyServer.receivedData.left(expectedCommand.length()); |
|
3633 |
QCOMPARE(receivedHeader, expectedCommand); |
|
3634 |
} |
|
3635 |
||
3636 |
void tst_QNetworkReply::proxyChange() |
|
3637 |
{ |
|
3638 |
MiniHttpServer proxyServer( |
|
3639 |
"HTTP/1.0 200 OK\r\nProxy-Connection: keep-alive\r\n" |
|
3640 |
"Content-Length: 1\r\n\r\n1"); |
|
3641 |
QNetworkProxy dummyProxy(QNetworkProxy::HttpProxy, "127.0.0.1", proxyServer.serverPort()); |
|
3642 |
QNetworkRequest req(QUrl("http://" + QtNetworkSettings::serverName())); |
|
3643 |
proxyServer.doClose = false; |
|
3644 |
||
3645 |
manager.setProxy(dummyProxy); |
|
3646 |
QNetworkReplyPtr reply1 = manager.get(req); |
|
3647 |
QSignalSpy finishedspy(reply1, SIGNAL(finished())); |
|
3648 |
||
3649 |
manager.setProxy(QNetworkProxy()); |
|
3650 |
QNetworkReplyPtr reply2 = manager.get(req); |
|
3651 |
||
3652 |
connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3653 |
#ifdef Q_OS_SYMBIAN |
|
3654 |
// we need more time as: |
|
3655 |
// 1. running from the emulator |
|
3656 |
// 2. not perfect POSIX implementation |
|
3657 |
// 3. embedded device |
|
3658 |
QTestEventLoop::instance().enterLoop(20); |
|
3659 |
#else |
|
3660 |
QTestEventLoop::instance().enterLoop(10); |
|
3661 |
#endif |
|
3662 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3663 |
||
3664 |
if (finishedspy.count() == 0) { |
|
3665 |
// wait for the second reply as well |
|
3666 |
connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3667 |
QTestEventLoop::instance().enterLoop(1); |
|
3668 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3669 |
} |
|
3670 |
||
3671 |
// verify that the replies succeeded |
|
3672 |
QCOMPARE(reply1->error(), QNetworkReply::NoError); |
|
3673 |
QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
3674 |
QVERIFY(reply1->size() == 1); |
|
3675 |
||
3676 |
QCOMPARE(reply2->error(), QNetworkReply::NoError); |
|
3677 |
QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); |
|
3678 |
QVERIFY(reply2->size() > 1); |
|
3679 |
||
3680 |
// now try again and get an error |
|
3681 |
// this verifies that we reuse the already-open connection |
|
3682 |
||
3683 |
proxyServer.doClose = true; |
|
3684 |
proxyServer.dataToTransmit = |
|
3685 |
"HTTP/1.0 403 Forbidden\r\nProxy-Connection: close\r\n" |
|
3686 |
"Content-Length: 1\r\n\r\n1"; |
|
3687 |
||
3688 |
manager.setProxy(dummyProxy); |
|
3689 |
QNetworkReplyPtr reply3 = manager.get(req); |
|
3690 |
connect(reply3, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3691 |
QTestEventLoop::instance().enterLoop(1); |
|
3692 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3693 |
||
3694 |
QVERIFY(int(reply3->error()) > 0); |
|
3695 |
} |
|
3696 |
||
3697 |
void tst_QNetworkReply::authorizationError_data() |
|
3698 |
{ |
|
3699 |
||
3700 |
QTest::addColumn<QString>("url"); |
|
3701 |
QTest::addColumn<int>("errorSignalCount"); |
|
3702 |
QTest::addColumn<int>("finishedSignalCount"); |
|
3703 |
QTest::addColumn<int>("error"); |
|
3704 |
QTest::addColumn<int>("httpStatusCode"); |
|
3705 |
QTest::addColumn<QString>("httpBody"); |
|
3706 |
||
3707 |
QTest::newRow("unknown-authorization-method") << "http://" + QtNetworkSettings::serverName() + |
|
3708 |
"/cgi-bin/http-unknown-authentication-method.cgi?401-authorization-required" << 1 << 1 |
|
3709 |
<< int(QNetworkReply::AuthenticationRequiredError) << 401 << "authorization required"; |
|
3710 |
QTest::newRow("unknown-proxy-authorization-method") << "http://" + QtNetworkSettings::serverName() + |
|
3711 |
"/cgi-bin/http-unknown-authentication-method.cgi?407-proxy-authorization-required" << 1 << 1 |
|
3712 |
<< int(QNetworkReply::ProxyAuthenticationRequiredError) << 407 |
|
3713 |
<< "authorization required"; |
|
3714 |
} |
|
3715 |
||
3716 |
void tst_QNetworkReply::authorizationError() |
|
3717 |
{ |
|
3718 |
QFETCH(QString, url); |
|
3719 |
QNetworkRequest request(url); |
|
3720 |
QNetworkReplyPtr reply = manager.get(request); |
|
3721 |
||
3722 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
|
3723 |
||
3724 |
qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError"); |
|
3725 |
QSignalSpy errorSpy(reply, SIGNAL(error(QNetworkReply::NetworkError))); |
|
3726 |
QSignalSpy finishedSpy(reply, SIGNAL(finished())); |
|
3727 |
// now run the request: |
|
3728 |
connect(reply, SIGNAL(finished()), |
|
3729 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3730 |
QTestEventLoop::instance().enterLoop(10); |
|
3731 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3732 |
||
3733 |
QFETCH(int, errorSignalCount); |
|
3734 |
QCOMPARE(errorSpy.count(), errorSignalCount); |
|
3735 |
QFETCH(int, finishedSignalCount); |
|
3736 |
QCOMPARE(finishedSpy.count(), finishedSignalCount); |
|
3737 |
QFETCH(int, error); |
|
3738 |
QCOMPARE(reply->error(), QNetworkReply::NetworkError(error)); |
|
3739 |
||
3740 |
QFETCH(int, httpStatusCode); |
|
3741 |
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), httpStatusCode); |
|
3742 |
||
3743 |
QFETCH(QString, httpBody); |
|
3744 |
QCOMPARE(QString(reply->readAll()), httpBody); |
|
3745 |
} |
|
3746 |
||
3747 |
void tst_QNetworkReply::httpConnectionCount() |
|
3748 |
{ |
|
3749 |
QTcpServer server; |
|
3750 |
QVERIFY(server.listen()); |
|
3751 |
QCoreApplication::instance()->processEvents(); |
|
3752 |
||
3753 |
for (int i = 0; i < 10; i++) { |
|
3754 |
QNetworkRequest request (QUrl("http://127.0.0.1:" + QString::number(server.serverPort()) + "/" + QString::number(i))); |
|
3755 |
QNetworkReply* reply = manager.get(request); |
|
3756 |
reply->setParent(this); |
|
3757 |
} |
|
3758 |
||
3759 |
int pendingConnectionCount = 0; |
|
3760 |
QTime time; |
|
3761 |
time.start(); |
|
3762 |
||
3763 |
while(pendingConnectionCount <= 20) { |
|
3764 |
QTestEventLoop::instance().enterLoop(1); |
|
3765 |
QTcpSocket *socket = server.nextPendingConnection(); |
|
3766 |
while (socket != 0) { |
|
3767 |
pendingConnectionCount++; |
|
3768 |
socket->setParent(&server); |
|
3769 |
socket = server.nextPendingConnection(); |
|
3770 |
} |
|
3771 |
||
3772 |
// at max. wait 10 sec |
|
3773 |
if (time.elapsed() > 10000) |
|
3774 |
break; |
|
3775 |
} |
|
3776 |
||
3777 |
#ifdef Q_OS_SYMBIAN |
|
3778 |
// see in qhttpnetworkconnection.cpp |
|
3779 |
// hardcoded defaultChannelCount = 3 |
|
3780 |
QCOMPARE(pendingConnectionCount, 3); |
|
3781 |
#else |
|
3782 |
QCOMPARE(pendingConnectionCount, 6); |
|
3783 |
#endif |
|
3784 |
} |
|
3785 |
||
3786 |
#ifndef QT_NO_OPENSSL |
|
3787 |
void tst_QNetworkReply::ignoreSslErrorsList_data() |
|
3788 |
{ |
|
3789 |
QTest::addColumn<QString>("url"); |
|
3790 |
QTest::addColumn<QList<QSslError> >("expectedSslErrors"); |
|
3791 |
QTest::addColumn<QNetworkReply::NetworkError>("expectedNetworkError"); |
|
3792 |
||
3793 |
QList<QSslError> expectedSslErrors; |
|
3794 |
// apparently, because of some weird behaviour of SRCDIR, the file name below needs to start with a slash |
|
3795 |
QList<QSslCertificate> certs = QSslCertificate::fromPath(QLatin1String(SRCDIR "/certs/qt-test-server-cacert.pem")); |
|
3796 |
QSslError rightError(QSslError::SelfSignedCertificate, certs.at(0)); |
|
3797 |
QSslError wrongError(QSslError::SelfSignedCertificate); |
|
3798 |
||
3799 |
QTest::newRow("SSL-failure-empty-list") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError; |
|
3800 |
expectedSslErrors.append(wrongError); |
|
3801 |
QTest::newRow("SSL-failure-wrong-error") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError; |
|
3802 |
expectedSslErrors.append(rightError); |
|
3803 |
QTest::newRow("allErrorsInExpectedList1") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::NoError; |
|
3804 |
expectedSslErrors.removeAll(wrongError); |
|
3805 |
QTest::newRow("allErrorsInExpectedList2") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::NoError; |
|
3806 |
expectedSslErrors.removeAll(rightError); |
|
3807 |
QTest::newRow("SSL-failure-empty-list-again") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError; |
|
3808 |
} |
|
3809 |
||
3810 |
void tst_QNetworkReply::ignoreSslErrorsList() |
|
3811 |
{ |
|
3812 |
QFETCH(QString, url); |
|
3813 |
QNetworkRequest request(url); |
|
3814 |
QNetworkReplyPtr reply = manager.get(request); |
|
3815 |
||
3816 |
QFETCH(QList<QSslError>, expectedSslErrors); |
|
3817 |
reply->ignoreSslErrors(expectedSslErrors); |
|
3818 |
||
3819 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3820 |
QTestEventLoop::instance().enterLoop(10); |
|
3821 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3822 |
||
3823 |
QFETCH(QNetworkReply::NetworkError, expectedNetworkError); |
|
3824 |
QCOMPARE(reply->error(), expectedNetworkError); |
|
3825 |
} |
|
3826 |
||
3827 |
void tst_QNetworkReply::ignoreSslErrorsListWithSlot_data() |
|
3828 |
{ |
|
3829 |
ignoreSslErrorsList_data(); |
|
3830 |
} |
|
3831 |
||
3832 |
// this is not a test, just a slot called in the test below |
|
3833 |
void tst_QNetworkReply::ignoreSslErrorListSlot(QNetworkReply *reply, const QList<QSslError> &) |
|
3834 |
{ |
|
3835 |
reply->ignoreSslErrors(storedExpectedSslErrors); |
|
3836 |
} |
|
3837 |
||
3838 |
// do the same as in ignoreSslErrorsList, but ignore the errors in the slot |
|
3839 |
void tst_QNetworkReply::ignoreSslErrorsListWithSlot() |
|
3840 |
{ |
|
3841 |
QFETCH(QString, url); |
|
3842 |
QNetworkRequest request(url); |
|
3843 |
QNetworkReplyPtr reply = manager.get(request); |
|
3844 |
||
3845 |
QFETCH(QList<QSslError>, expectedSslErrors); |
|
3846 |
// store the errors to ignore them later in the slot connected below |
|
3847 |
storedExpectedSslErrors = expectedSslErrors; |
|
3848 |
connect(&manager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)), |
|
3849 |
this, SLOT(ignoreSslErrorListSlot(QNetworkReply *, const QList<QSslError> &))); |
|
3850 |
||
3851 |
||
3852 |
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
3853 |
QTestEventLoop::instance().enterLoop(10); |
|
3854 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
3855 |
||
3856 |
QFETCH(QNetworkReply::NetworkError, expectedNetworkError); |
|
3857 |
QCOMPARE(reply->error(), expectedNetworkError); |
|
3858 |
} |
|
3859 |
||
3860 |
#endif // QT_NO_OPENSSL |
|
3861 |
||
3862 |
QTEST_MAIN(tst_QNetworkReply) |
|
3863 |
#include "tst_qnetworkreply.moc" |