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