author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
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 |
||
45 |
#include <qbuffer.h> |
|
46 |
#include <qcoreapplication.h> |
|
47 |
#include <qfile.h> |
|
48 |
#include <qhostinfo.h> |
|
49 |
#include <qhttp.h> |
|
50 |
#include <qlist.h> |
|
51 |
#include <qpointer.h> |
|
52 |
#include <qtcpsocket.h> |
|
53 |
#include <qtcpserver.h> |
|
54 |
#include <qauthenticator.h> |
|
55 |
#include <QNetworkProxy> |
|
56 |
#ifndef QT_NO_OPENSSL |
|
57 |
# include <qsslsocket.h> |
|
58 |
#endif |
|
59 |
||
60 |
#include "../network-settings.h" |
|
61 |
||
62 |
//TESTED_CLASS= |
|
63 |
//TESTED_FILES= |
|
64 |
||
65 |
#ifdef Q_OS_SYMBIAN |
|
66 |
// In Symbian OS test data is located in applications private dir |
|
67 |
// And underlying Open C have application private dir in default search path |
|
68 |
#define SRCDIR "" |
|
69 |
#endif |
|
70 |
||
71 |
Q_DECLARE_METATYPE(QHttpResponseHeader) |
|
72 |
||
73 |
class tst_QHttp : public QObject |
|
74 |
{ |
|
75 |
Q_OBJECT |
|
76 |
||
77 |
public: |
|
78 |
tst_QHttp(); |
|
79 |
virtual ~tst_QHttp(); |
|
80 |
||
81 |
||
82 |
public slots: |
|
83 |
void initTestCase_data(); |
|
84 |
void initTestCase(); |
|
85 |
void cleanupTestCase(); |
|
86 |
void init(); |
|
87 |
void cleanup(); |
|
88 |
private slots: |
|
89 |
void constructing(); |
|
90 |
void invalidRequests(); |
|
91 |
void get_data(); |
|
92 |
void get(); |
|
93 |
void head_data(); |
|
94 |
void head(); |
|
95 |
void post_data(); |
|
96 |
void post(); |
|
97 |
void request_data(); |
|
98 |
void request(); |
|
99 |
void authorization_data(); |
|
100 |
void authorization(); |
|
101 |
void proxy_data(); |
|
102 |
void proxy(); |
|
103 |
void proxy2(); |
|
104 |
void proxy3(); |
|
105 |
void postAuthNtlm(); |
|
106 |
void proxyAndSsl(); |
|
107 |
void cachingProxyAndSsl(); |
|
108 |
void reconnect(); |
|
109 |
void setSocket(); |
|
110 |
void unexpectedRemoteClose(); |
|
111 |
void pctEncodedPath(); |
|
112 |
void caseInsensitiveKeys(); |
|
113 |
void emptyBodyInReply(); |
|
114 |
void abortInReadyRead(); |
|
115 |
void abortInResponseHeaderReceived(); |
|
116 |
void nestedEventLoop(); |
|
117 |
||
118 |
||
119 |
// manual tests |
|
120 |
void connectionClose(); |
|
121 |
||
122 |
protected slots: |
|
123 |
void stateChanged( int ); |
|
124 |
void responseHeaderReceived( const QHttpResponseHeader & ); |
|
125 |
void readyRead( const QHttpResponseHeader& ); |
|
126 |
void dataSendProgress( int, int ); |
|
127 |
void dataReadProgress( int , int ); |
|
128 |
||
129 |
void requestStarted( int ); |
|
130 |
void requestFinished( int, bool ); |
|
131 |
void done( bool ); |
|
132 |
||
133 |
void reconnect_state(int state); |
|
134 |
void proxy2_slot(); |
|
135 |
void nestedEventLoop_slot(int id); |
|
136 |
||
137 |
void abortSender(); |
|
138 |
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth); |
|
139 |
||
140 |
private: |
|
141 |
QHttp *newHttp(bool withAuth = false); |
|
142 |
void addRequest( QHttpRequestHeader, int ); |
|
143 |
bool headerAreEqual( const QHttpHeader&, const QHttpHeader& ); |
|
144 |
||
145 |
QHttp *http; |
|
146 |
||
147 |
struct RequestResult |
|
148 |
{ |
|
149 |
QHttpRequestHeader req; |
|
150 |
QHttpResponseHeader resp; |
|
151 |
int success; |
|
152 |
}; |
|
153 |
QMap< int, RequestResult > resultMap; |
|
154 |
typedef QMap<int,RequestResult>::Iterator ResMapIt; |
|
155 |
QList<int> ids; // helper to make sure that all expected signals are emitted |
|
156 |
||
157 |
int current_id; |
|
158 |
int cur_state; |
|
159 |
int done_success; |
|
160 |
||
161 |
QByteArray readyRead_ba; |
|
162 |
||
163 |
int bytesTotalSend; |
|
164 |
int bytesDoneSend; |
|
165 |
int bytesTotalRead; |
|
166 |
int bytesDoneRead; |
|
167 |
||
168 |
int getId; |
|
169 |
int headId; |
|
170 |
int postId; |
|
171 |
||
172 |
int reconnect_state_connect_count; |
|
173 |
||
174 |
bool connectionWithAuth; |
|
175 |
bool proxyAuthCalled; |
|
176 |
}; |
|
177 |
||
178 |
//#define DUMP_SIGNALS |
|
179 |
||
180 |
const int bytesTotal_init = -10; |
|
181 |
const int bytesDone_init = -10; |
|
182 |
||
183 |
tst_QHttp::tst_QHttp() |
|
184 |
{ |
|
185 |
Q_SET_DEFAULT_IAP |
|
186 |
} |
|
187 |
||
188 |
tst_QHttp::~tst_QHttp() |
|
189 |
{ |
|
190 |
} |
|
191 |
||
192 |
void tst_QHttp::initTestCase_data() |
|
193 |
{ |
|
194 |
QTest::addColumn<bool>("setProxy"); |
|
195 |
QTest::addColumn<int>("proxyType"); |
|
196 |
||
197 |
QTest::newRow("WithoutProxy") << false << 0; |
|
198 |
QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy); |
|
199 |
} |
|
200 |
||
201 |
void tst_QHttp::initTestCase() |
|
202 |
{ |
|
203 |
} |
|
204 |
||
205 |
void tst_QHttp::cleanupTestCase() |
|
206 |
{ |
|
207 |
} |
|
208 |
||
209 |
void tst_QHttp::init() |
|
210 |
{ |
|
211 |
QFETCH_GLOBAL(bool, setProxy); |
|
212 |
if (setProxy) { |
|
213 |
QFETCH_GLOBAL(int, proxyType); |
|
214 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
215 |
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080)); |
|
216 |
} |
|
217 |
} |
|
218 |
||
219 |
http = 0; |
|
220 |
||
221 |
resultMap.clear(); |
|
222 |
ids.clear(); |
|
223 |
||
224 |
current_id = 0; |
|
225 |
cur_state = QHttp::Unconnected; |
|
226 |
done_success = -1; |
|
227 |
||
228 |
readyRead_ba = QByteArray(); |
|
229 |
||
230 |
getId = -1; |
|
231 |
headId = -1; |
|
232 |
postId = -1; |
|
233 |
} |
|
234 |
||
235 |
void tst_QHttp::cleanup() |
|
236 |
{ |
|
237 |
delete http; |
|
238 |
http = 0; |
|
239 |
||
240 |
QCoreApplication::processEvents(); |
|
241 |
||
242 |
QFETCH_GLOBAL(bool, setProxy); |
|
243 |
if (setProxy) { |
|
244 |
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
void tst_QHttp::constructing() |
|
249 |
{ |
|
250 |
//QHeader |
|
251 |
{ |
|
252 |
QHttpRequestHeader header; |
|
253 |
header.addValue("key1", "val1"); |
|
254 |
header.addValue("key2", "val2"); |
|
255 |
header.addValue("key1", "val3"); |
|
256 |
QCOMPARE(header.values().size(), 3); |
|
257 |
QCOMPARE(header.allValues("key1").size(), 2); |
|
258 |
QVERIFY(header.hasKey("key2")); |
|
259 |
QCOMPARE(header.keys().count(), 2); |
|
260 |
||
261 |
} |
|
262 |
||
263 |
{ |
|
264 |
QHttpResponseHeader header(200); |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
void tst_QHttp::invalidRequests() |
|
269 |
{ |
|
270 |
QHttp http; |
|
271 |
http.setHost("localhost"); // we will not actually connect |
|
272 |
||
273 |
QTest::ignoreMessage(QtWarningMsg, "QHttp: empty path requested is invalid -- using '/'"); |
|
274 |
http.get(QString()); |
|
275 |
||
276 |
QTest::ignoreMessage(QtWarningMsg, "QHttp: empty path requested is invalid -- using '/'"); |
|
277 |
http.head(QString()); |
|
278 |
||
279 |
QTest::ignoreMessage(QtWarningMsg, "QHttp: empty path requested is invalid -- using '/'"); |
|
280 |
http.post(QString(), QByteArray()); |
|
281 |
||
282 |
QTest::ignoreMessage(QtWarningMsg, "QHttp: empty path requested is invalid -- using '/'"); |
|
283 |
http.request(QHttpRequestHeader("PROPFIND", QString())); |
|
284 |
} |
|
285 |
||
286 |
void tst_QHttp::get_data() |
|
287 |
{ |
|
288 |
QTest::addColumn<QString>("host"); |
|
289 |
QTest::addColumn<uint>("port"); |
|
290 |
QTest::addColumn<QString>("path"); |
|
291 |
QTest::addColumn<int>("success"); |
|
292 |
QTest::addColumn<int>("statusCode"); |
|
293 |
QTest::addColumn<QByteArray>("res"); |
|
294 |
QTest::addColumn<bool>("useIODevice"); |
|
295 |
||
296 |
// ### move this into external testdata |
|
297 |
QFile file( SRCDIR "rfc3252.txt" ); |
|
298 |
QVERIFY( file.open( QIODevice::ReadOnly ) ); |
|
299 |
QByteArray rfc3252 = file.readAll(); |
|
300 |
file.close(); |
|
301 |
||
302 |
file.setFileName( SRCDIR "trolltech" ); |
|
303 |
QVERIFY( file.open( QIODevice::ReadOnly ) ); |
|
304 |
QByteArray trolltech = file.readAll(); |
|
305 |
file.close(); |
|
306 |
||
307 |
// test the two get() modes in one routine |
|
308 |
for ( int i=0; i<2; i++ ) { |
|
309 |
QTest::newRow(QString("path_01_%1").arg(i).toLatin1()) << QtNetworkSettings::serverName() << 80u |
|
310 |
<< QString("/qtest/rfc3252.txt") << 1 << 200 << rfc3252 << (bool)(i==1); |
|
311 |
QTest::newRow( QString("path_02_%1").arg(i).toLatin1() ) << QString("www.ietf.org") << 80u |
|
312 |
<< QString("/rfc/rfc3252.txt") << 1 << 200 << rfc3252 << (bool)(i==1); |
|
313 |
||
314 |
QTest::newRow( QString("uri_01_%1").arg(i).toLatin1() ) << QtNetworkSettings::serverName() << 80u |
|
315 |
<< QString("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt") << 1 << 200 << rfc3252 << (bool)(i==1); |
|
316 |
QTest::newRow( QString("uri_02_%1").arg(i).toLatin1() ) << "www.ietf.org" << 80u |
|
317 |
<< QString("http://www.ietf.org/rfc/rfc3252.txt") << 1 << 200 << rfc3252 << (bool)(i==1); |
|
318 |
||
319 |
QTest::newRow( QString("fail_01_%1").arg(i).toLatin1() ) << QString("this-host-will-not-exist.") << 80u |
|
320 |
<< QString("/qtest/rfc3252.txt") << 0 << 0 << QByteArray() << (bool)(i==1); |
|
321 |
||
322 |
QTest::newRow( QString("failprot_01_%1").arg(i).toLatin1() ) << QtNetworkSettings::serverName() << 80u |
|
323 |
<< QString("/t") << 1 << 404 << QByteArray() << (bool)(i==1); |
|
324 |
QTest::newRow( QString("failprot_02_%1").arg(i).toLatin1() ) << QtNetworkSettings::serverName() << 80u |
|
325 |
<< QString("qtest/rfc3252.txt") << 1 << 400 << QByteArray() << (bool)(i==1); |
|
326 |
||
327 |
// qt.nokia.com/doc uses transfer-encoding=chunked |
|
328 |
/* qt.nokia.com/doc no longer seams to be using chuncked encodig. |
|
329 |
QTest::newRow( QString("chunked_01_%1").arg(i).toLatin1() ) << QString("test.troll.no") << 80u |
|
330 |
<< QString("/") << 1 << 200 << trolltech << (bool)(i==1); |
|
331 |
*/ |
|
332 |
QTest::newRow( QString("chunked_02_%1").arg(i).toLatin1() ) << QtNetworkSettings::serverName() << 80u |
|
333 |
<< QString("/qtest/cgi-bin/rfc.cgi") << 1 << 200 << rfc3252 << (bool)(i==1); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
void tst_QHttp::get() |
|
338 |
{ |
|
339 |
// for the overload that takes a QIODevice |
|
340 |
QByteArray buf_ba; |
|
341 |
QBuffer buf( &buf_ba ); |
|
342 |
||
343 |
QFETCH( QString, host ); |
|
344 |
QFETCH( uint, port ); |
|
345 |
QFETCH( QString, path ); |
|
346 |
QFETCH( bool, useIODevice ); |
|
347 |
||
348 |
http = newHttp(); |
|
349 |
QCOMPARE( http->currentId(), 0 ); |
|
350 |
QCOMPARE( (int)http->state(), (int)QHttp::Unconnected ); |
|
351 |
||
352 |
addRequest( QHttpRequestHeader(), http->setHost( host, port ) ); |
|
353 |
if ( useIODevice ) { |
|
354 |
buf.open( QIODevice::WriteOnly ); |
|
355 |
getId = http->get( path, &buf ); |
|
356 |
} else { |
|
357 |
getId = http->get( path ); |
|
358 |
} |
|
359 |
addRequest( QHttpRequestHeader(), getId ); |
|
360 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
361 |
QTestEventLoop::instance().enterLoop( 50 ); |
0 | 362 |
|
363 |
if ( QTestEventLoop::instance().timeout() ) |
|
364 |
QFAIL( "Network operation timed out" ); |
|
365 |
||
366 |
ResMapIt res = resultMap.find( getId ); |
|
367 |
QVERIFY( res != resultMap.end() ); |
|
368 |
if ( res.value().success!=1 && host=="www.ietf.org" ) { |
|
369 |
// The nightly tests fail from time to time. In order to make them more |
|
370 |
// stable, add some debug output that might help locate the problem (I |
|
371 |
// can't reproduce the problem in the non-nightly builds). |
|
372 |
qDebug( "Error %d: %s", http->error(), http->errorString().toLatin1().constData() ); |
|
373 |
} |
|
374 |
QTEST( res.value().success, "success" ); |
|
375 |
if ( res.value().success ) { |
|
376 |
QTEST( res.value().resp.statusCode(), "statusCode" ); |
|
377 |
||
378 |
QFETCH( QByteArray, res ); |
|
379 |
if ( res.count() > 0 ) { |
|
380 |
if ( useIODevice ) { |
|
381 |
QCOMPARE(buf_ba, res); |
|
382 |
if ( bytesDoneRead != bytesDone_init ) |
|
383 |
QVERIFY( (int)buf_ba.size() == bytesDoneRead ); |
|
384 |
} else { |
|
385 |
QCOMPARE(readyRead_ba, res); |
|
386 |
if ( bytesDoneRead != bytesDone_init ) |
|
387 |
QVERIFY( (int)readyRead_ba.size() == bytesDoneRead ); |
|
388 |
} |
|
389 |
} |
|
390 |
QVERIFY( bytesTotalRead != bytesTotal_init ); |
|
391 |
if ( bytesTotalRead > 0 ) |
|
392 |
QVERIFY( bytesDoneRead == bytesTotalRead ); |
|
393 |
} else { |
|
394 |
QVERIFY( !res.value().resp.isValid() ); |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
void tst_QHttp::head_data() |
|
399 |
{ |
|
400 |
QTest::addColumn<QString>("host"); |
|
401 |
QTest::addColumn<uint>("port"); |
|
402 |
QTest::addColumn<QString>("path"); |
|
403 |
QTest::addColumn<int>("success"); |
|
404 |
QTest::addColumn<int>("statusCode"); |
|
405 |
QTest::addColumn<uint>("contentLength"); |
|
406 |
||
407 |
QTest::newRow( "path_01" ) << QtNetworkSettings::serverName() << 80u |
|
408 |
<< QString("/qtest/rfc3252.txt") << 1 << 200 << 25962u; |
|
409 |
||
410 |
QTest::newRow( "path_02" ) << QString("www.ietf.org") << 80u |
|
411 |
<< QString("/rfc/rfc3252.txt") << 1 << 200 << 25962u; |
|
412 |
||
413 |
QTest::newRow( "uri_01" ) << QtNetworkSettings::serverName() << 80u |
|
414 |
<< QString("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt") << 1 << 200 << 25962u; |
|
415 |
||
416 |
QTest::newRow( "uri_02" ) << QString("www.ietf.org") << 80u |
|
417 |
<< QString("http://www.ietf.org/rfc/rfc3252.txt") << 1 << 200 << 25962u; |
|
418 |
||
419 |
QTest::newRow( "fail_01" ) << QString("this-host-will-not-exist.") << 80u |
|
420 |
<< QString("/qtest/rfc3252.txt") << 0 << 0 << 0u; |
|
421 |
||
422 |
QTest::newRow( "failprot_01" ) << QtNetworkSettings::serverName() << 80u |
|
423 |
<< QString("/t") << 1 << 404 << 0u; |
|
424 |
||
425 |
QTest::newRow( "failprot_02" ) << QtNetworkSettings::serverName() << 80u |
|
426 |
<< QString("qtest/rfc3252.txt") << 1 << 400 << 0u; |
|
427 |
||
428 |
/* qt.nokia.com/doc no longer seams to be using chuncked encodig. |
|
429 |
QTest::newRow( "chunked_01" ) << QString("qt.nokia.com/doc") << 80u |
|
430 |
<< QString("/index.html") << 1 << 200 << 0u; |
|
431 |
*/ |
|
432 |
QTest::newRow( "chunked_02" ) << QtNetworkSettings::serverName() << 80u |
|
433 |
<< QString("/qtest/cgi-bin/rfc.cgi") << 1 << 200 << 0u; |
|
434 |
} |
|
435 |
||
436 |
void tst_QHttp::head() |
|
437 |
{ |
|
438 |
QFETCH( QString, host ); |
|
439 |
QFETCH( uint, port ); |
|
440 |
QFETCH( QString, path ); |
|
441 |
||
442 |
http = newHttp(); |
|
443 |
QCOMPARE( http->currentId(), 0 ); |
|
444 |
QCOMPARE( (int)http->state(), (int)QHttp::Unconnected ); |
|
445 |
||
446 |
addRequest( QHttpRequestHeader(), http->setHost( host, port ) ); |
|
447 |
headId = http->head( path ); |
|
448 |
addRequest( QHttpRequestHeader(), headId ); |
|
449 |
||
450 |
QTestEventLoop::instance().enterLoop( 30 ); |
|
451 |
if ( QTestEventLoop::instance().timeout() ) |
|
452 |
QFAIL( "Network operation timed out" ); |
|
453 |
||
454 |
ResMapIt res = resultMap.find( headId ); |
|
455 |
QVERIFY( res != resultMap.end() ); |
|
456 |
if ( res.value().success!=1 && host=="www.ietf.org" ) { |
|
457 |
// The nightly tests fail from time to time. In order to make them more |
|
458 |
// stable, add some debug output that might help locate the problem (I |
|
459 |
// can't reproduce the problem in the non-nightly builds). |
|
460 |
qDebug( "Error %d: %s", http->error(), http->errorString().toLatin1().constData() ); |
|
461 |
} |
|
462 |
QTEST( res.value().success, "success" ); |
|
463 |
if ( res.value().success ) { |
|
464 |
QTEST( res.value().resp.statusCode(), "statusCode" ); |
|
465 |
QTEST( res.value().resp.contentLength(), "contentLength" ); |
|
466 |
||
467 |
QCOMPARE( (uint)readyRead_ba.size(), 0u ); |
|
468 |
QVERIFY( bytesTotalRead == bytesTotal_init ); |
|
469 |
QVERIFY( bytesDoneRead == bytesDone_init ); |
|
470 |
} else { |
|
471 |
QVERIFY( !res.value().resp.isValid() ); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
void tst_QHttp::post_data() |
|
476 |
{ |
|
477 |
QTest::addColumn<QString>("source"); |
|
478 |
QTest::addColumn<bool>("useIODevice"); |
|
479 |
QTest::addColumn<bool>("useProxy"); |
|
480 |
QTest::addColumn<QString>("host"); |
|
481 |
QTest::addColumn<int>("port"); |
|
482 |
QTest::addColumn<bool>("ssl"); |
|
483 |
QTest::addColumn<QString>("path"); |
|
484 |
QTest::addColumn<QByteArray>("result"); |
|
485 |
||
486 |
QByteArray md5sum; |
|
487 |
md5sum = "d41d8cd98f00b204e9800998ecf8427e"; |
|
488 |
QTest::newRow("empty-data") |
|
489 |
<< QString() << false << false |
|
490 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
491 |
QTest::newRow("empty-device") |
|
492 |
<< QString() << true << false |
|
493 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
494 |
QTest::newRow("proxy-empty-data") |
|
495 |
<< QString() << false << true |
|
496 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
497 |
||
498 |
md5sum = "b3e32ac459b99d3f59318f3ac31e4bee"; |
|
499 |
QTest::newRow("data") << "rfc3252.txt" << false << false |
|
500 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" |
|
501 |
<< md5sum; |
|
502 |
QTest::newRow("device") << "rfc3252.txt" << true << false |
|
503 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" |
|
504 |
<< md5sum; |
|
505 |
QTest::newRow("proxy-data") << "rfc3252.txt" << false << true |
|
506 |
<< QtNetworkSettings::serverName() << 80 << false << "/qtest/cgi-bin/md5sum.cgi" |
|
507 |
<< md5sum; |
|
508 |
||
509 |
#ifndef QT_NO_OPENSSL |
|
510 |
md5sum = "d41d8cd98f00b204e9800998ecf8427e"; |
|
511 |
QTest::newRow("empty-data-ssl") |
|
512 |
<< QString() << false << false |
|
513 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
514 |
QTest::newRow("empty-device-ssl") |
|
515 |
<< QString() << true << false |
|
516 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
517 |
QTest::newRow("proxy-empty-data-ssl") |
|
518 |
<< QString() << false << true |
|
519 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" << md5sum; |
|
520 |
md5sum = "b3e32ac459b99d3f59318f3ac31e4bee"; |
|
521 |
QTest::newRow("data-ssl") << "rfc3252.txt" << false << false |
|
522 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" |
|
523 |
<< md5sum; |
|
524 |
QTest::newRow("device-ssl") << "rfc3252.txt" << true << false |
|
525 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" |
|
526 |
<< md5sum; |
|
527 |
QTest::newRow("proxy-data-ssl") << "rfc3252.txt" << false << true |
|
528 |
<< QtNetworkSettings::serverName() << 443 << true << "/qtest/cgi-bin/md5sum.cgi" |
|
529 |
<< md5sum; |
|
530 |
#endif |
|
531 |
||
532 |
// the following test won't work. See task 185996 |
|
533 |
/* |
|
534 |
QTest::newRow("proxy-device") << "rfc3252.txt" << true << true |
|
535 |
<< QtNetworkSettings::serverName() << 80 << "/qtest/cgi-bin/md5sum.cgi" |
|
536 |
<< md5sum; |
|
537 |
*/ |
|
538 |
} |
|
539 |
||
540 |
void tst_QHttp::post() |
|
541 |
{ |
|
542 |
QFETCH(QString, source); |
|
543 |
QFETCH(bool, useIODevice); |
|
544 |
QFETCH(bool, useProxy); |
|
545 |
QFETCH(QString, host); |
|
546 |
QFETCH(int, port); |
|
547 |
QFETCH(bool, ssl); |
|
548 |
QFETCH(QString, path); |
|
549 |
||
550 |
http = newHttp(useProxy); |
|
551 |
#ifndef QT_NO_OPENSSL |
|
552 |
QObject::connect(http, SIGNAL(sslErrors(const QList<QSslError> &)), |
|
553 |
http, SLOT(ignoreSslErrors())); |
|
554 |
#endif |
|
555 |
QCOMPARE(http->currentId(), 0); |
|
556 |
QCOMPARE((int)http->state(), (int)QHttp::Unconnected); |
|
557 |
if (useProxy) |
|
558 |
addRequest(QHttpRequestHeader(), http->setProxy(QtNetworkSettings::serverName(), 3129)); |
|
559 |
addRequest(QHttpRequestHeader(), http->setHost(host, (ssl ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp), port)); |
|
560 |
||
561 |
// add the POST request |
|
562 |
QFile file(SRCDIR + source); |
|
563 |
QBuffer emptyBuffer; |
|
564 |
QIODevice *dev; |
|
565 |
if (!source.isEmpty()) { |
|
566 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
567 |
dev = &file; |
|
568 |
} else { |
|
569 |
emptyBuffer.open(QIODevice::ReadOnly); |
|
570 |
dev = &emptyBuffer; |
|
571 |
} |
|
572 |
||
573 |
if (useIODevice) |
|
574 |
postId = http->post(path, dev); |
|
575 |
else |
|
576 |
postId = http->post(path, dev->readAll()); |
|
577 |
addRequest(QHttpRequestHeader(), postId); |
|
578 |
||
579 |
// run request |
|
580 |
connect(http, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
581 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
582 |
QTestEventLoop::instance().enterLoop( 30 ); |
|
583 |
||
584 |
if ( QTestEventLoop::instance().timeout() ) |
|
585 |
QFAIL( "Network operation timed out" ); |
|
586 |
||
587 |
ResMapIt res = resultMap.find(postId); |
|
588 |
QVERIFY(res != resultMap.end()); |
|
589 |
QVERIFY(res.value().success); |
|
590 |
QCOMPARE(res.value().resp.statusCode(), 200); |
|
591 |
QTEST(readyRead_ba.trimmed(), "result"); |
|
592 |
} |
|
593 |
||
594 |
void tst_QHttp::request_data() |
|
595 |
{ |
|
596 |
QTest::addColumn<QString>("source"); |
|
597 |
QTest::addColumn<bool>("useIODevice"); |
|
598 |
QTest::addColumn<bool>("useProxy"); |
|
599 |
QTest::addColumn<QString>("host"); |
|
600 |
QTest::addColumn<int>("port"); |
|
601 |
QTest::addColumn<QString>("method"); |
|
602 |
QTest::addColumn<QString>("path"); |
|
603 |
QTest::addColumn<QByteArray>("result"); |
|
604 |
||
605 |
QFile source(SRCDIR "rfc3252.txt"); |
|
606 |
if (!source.open(QIODevice::ReadOnly)) |
|
607 |
return; |
|
608 |
||
609 |
QByteArray contents = source.readAll(); |
|
610 |
QByteArray md5sum = QCryptographicHash::hash(contents, QCryptographicHash::Md5).toHex() + '\n'; |
|
611 |
QByteArray emptyMd5sum = "d41d8cd98f00b204e9800998ecf8427e\n"; |
|
612 |
||
613 |
QTest::newRow("head") << QString() << false << false << QtNetworkSettings::serverName() << 80 |
|
614 |
<< "HEAD" << "/qtest/rfc3252.txt" |
|
615 |
<< QByteArray(); |
|
616 |
QTest::newRow("get") << QString() << false << false << QtNetworkSettings::serverName() << 80 |
|
617 |
<< "GET" << "/qtest/rfc3252.txt" |
|
618 |
<< contents; |
|
619 |
QTest::newRow("post-empty-data") << QString() << false << false |
|
620 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
621 |
<< emptyMd5sum; |
|
622 |
QTest::newRow("post-empty-device") << QString() << true << false |
|
623 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
624 |
<< emptyMd5sum; |
|
625 |
QTest::newRow("post-data") << "rfc3252.txt" << false << false |
|
626 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
627 |
<< md5sum; |
|
628 |
QTest::newRow("post-device") << "rfc3252.txt" << true << false |
|
629 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
630 |
<< md5sum; |
|
631 |
||
632 |
QTest::newRow("proxy-head") << QString() << false << true << QtNetworkSettings::serverName() << 80 |
|
633 |
<< "HEAD" << "/qtest/rfc3252.txt" |
|
634 |
<< QByteArray(); |
|
635 |
QTest::newRow("proxy-get") << QString() << false << true << QtNetworkSettings::serverName() << 80 |
|
636 |
<< "GET" << "/qtest/rfc3252.txt" |
|
637 |
<< contents; |
|
638 |
QTest::newRow("proxy-post-empty-data") << QString() << false << true |
|
639 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
640 |
<< emptyMd5sum; |
|
641 |
QTest::newRow("proxy-post-data") << "rfc3252.txt" << false << true |
|
642 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
643 |
<< md5sum; |
|
644 |
// the following test won't work. See task 185996 |
|
645 |
/* |
|
646 |
QTest::newRow("proxy-post-device") << "rfc3252.txt" << true << true |
|
647 |
<< QtNetworkSettings::serverName() << 80 << "POST" << "/qtest/cgi-bin/md5sum.cgi" |
|
648 |
<< md5sum; |
|
649 |
*/ |
|
650 |
} |
|
651 |
||
652 |
void tst_QHttp::request() |
|
653 |
{ |
|
654 |
QFETCH(QString, source); |
|
655 |
QFETCH(bool, useIODevice); |
|
656 |
QFETCH(bool, useProxy); |
|
657 |
QFETCH(QString, host); |
|
658 |
QFETCH(int, port); |
|
659 |
QFETCH(QString, method); |
|
660 |
QFETCH(QString, path); |
|
661 |
||
662 |
http = newHttp(useProxy); |
|
663 |
QCOMPARE(http->currentId(), 0); |
|
664 |
QCOMPARE((int)http->state(), (int)QHttp::Unconnected); |
|
665 |
if (useProxy) |
|
666 |
addRequest(QHttpRequestHeader(), http->setProxy(QtNetworkSettings::serverName(), 3129)); |
|
667 |
addRequest(QHttpRequestHeader(), http->setHost(host, port)); |
|
668 |
||
669 |
QFile file(SRCDIR + source); |
|
670 |
QBuffer emptyBuffer; |
|
671 |
QIODevice *dev; |
|
672 |
if (!source.isEmpty()) { |
|
673 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
674 |
dev = &file; |
|
675 |
} else { |
|
676 |
emptyBuffer.open(QIODevice::ReadOnly); |
|
677 |
dev = &emptyBuffer; |
|
678 |
} |
|
679 |
||
680 |
// prepare the request |
|
681 |
QHttpRequestHeader request; |
|
682 |
request.setRequest(method, path, 1,1); |
|
683 |
request.addValue("Host", host); |
|
684 |
int *theId; |
|
685 |
||
686 |
if (method == "POST") |
|
687 |
theId = &postId; |
|
688 |
else if (method == "GET") |
|
689 |
theId = &getId; |
|
690 |
else if (method == "HEAD") |
|
691 |
theId = &headId; |
|
692 |
else |
|
693 |
QFAIL("You're lazy! Please implement your test!"); |
|
694 |
||
695 |
// now send the request |
|
696 |
if (useIODevice) |
|
697 |
*theId = http->request(request, dev); |
|
698 |
else |
|
699 |
*theId = http->request(request, dev->readAll()); |
|
700 |
addRequest(QHttpRequestHeader(), *theId); |
|
701 |
||
702 |
// run request |
|
703 |
connect(http, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
704 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
705 |
QTestEventLoop::instance().enterLoop( 30 ); |
|
706 |
||
707 |
if ( QTestEventLoop::instance().timeout() ) |
|
708 |
QFAIL( "Network operation timed out" ); |
|
709 |
||
710 |
ResMapIt res = resultMap.find(*theId); |
|
711 |
QVERIFY(res != resultMap.end()); |
|
712 |
QVERIFY(res.value().success); |
|
713 |
QCOMPARE(res.value().resp.statusCode(), 200); |
|
714 |
QTEST(readyRead_ba, "result"); |
|
715 |
} |
|
716 |
||
717 |
void tst_QHttp::authorization_data() |
|
718 |
{ |
|
719 |
QTest::addColumn<QString>("host"); |
|
720 |
QTest::addColumn<QString>("path"); |
|
721 |
QTest::addColumn<QString>("user"); |
|
722 |
QTest::addColumn<QString>("pass"); |
|
723 |
QTest::addColumn<int>("result"); |
|
724 |
||
725 |
QTest::newRow("correct password") << QtNetworkSettings::serverName() |
|
726 |
<< QString::fromLatin1("/qtest/rfcs-auth/index.html") |
|
727 |
<< QString::fromLatin1("httptest") |
|
728 |
<< QString::fromLatin1("httptest") |
|
729 |
<< 200; |
|
730 |
||
731 |
QTest::newRow("no password") << QtNetworkSettings::serverName() |
|
732 |
<< QString::fromLatin1("/qtest/rfcs-auth/index.html") |
|
733 |
<< QString::fromLatin1("") |
|
734 |
<< QString::fromLatin1("") |
|
735 |
<< 401; |
|
736 |
||
737 |
QTest::newRow("wrong password") << QtNetworkSettings::serverName() |
|
738 |
<< QString::fromLatin1("/qtest/rfcs-auth/index.html") |
|
739 |
<< QString::fromLatin1("maliciu0s") |
|
740 |
<< QString::fromLatin1("h4X0r") |
|
741 |
<< 401; |
|
742 |
} |
|
743 |
||
744 |
void tst_QHttp::authorization() |
|
745 |
{ |
|
746 |
QFETCH(QString, host); |
|
747 |
QFETCH(QString, path); |
|
748 |
QFETCH(QString, user); |
|
749 |
QFETCH(QString, pass); |
|
750 |
QFETCH(int, result); |
|
751 |
||
752 |
QEventLoop loop; |
|
753 |
||
754 |
QHttp http; |
|
755 |
connect(&http, SIGNAL(done(bool)), &loop, SLOT(quit())); |
|
756 |
||
757 |
if (!user.isEmpty()) |
|
758 |
http.setUser(user, pass); |
|
759 |
http.setHost(host); |
|
760 |
int id = http.get(path); |
|
761 |
||
762 |
QTimer::singleShot(5000, &loop, SLOT(quit())); |
|
763 |
loop.exec(); |
|
764 |
||
765 |
QCOMPARE(http.lastResponse().statusCode(), result); |
|
766 |
} |
|
767 |
||
768 |
void tst_QHttp::proxy_data() |
|
769 |
{ |
|
770 |
QTest::addColumn<QString>("proxyhost"); |
|
771 |
QTest::addColumn<int>("port"); |
|
772 |
QTest::addColumn<QString>("host"); |
|
773 |
QTest::addColumn<QString>("path"); |
|
774 |
QTest::addColumn<QString>("proxyuser"); |
|
775 |
QTest::addColumn<QString>("proxypass"); |
|
776 |
||
777 |
QTest::newRow("qt-test-server") << QtNetworkSettings::serverName() << 3128 |
|
778 |
<< QString::fromLatin1("qt.nokia.com") << QString::fromLatin1("/") |
|
779 |
<< QString::fromLatin1("") << QString::fromLatin1(""); |
|
780 |
QTest::newRow("qt-test-server pct") << QtNetworkSettings::serverName() << 3128 |
|
781 |
<< QString::fromLatin1("qt.nokia.com") << QString::fromLatin1("/%64eveloper") |
|
782 |
<< QString::fromLatin1("") << QString::fromLatin1(""); |
|
783 |
QTest::newRow("qt-test-server-basic") << QtNetworkSettings::serverName() << 3129 |
|
784 |
<< QString::fromLatin1("qt.nokia.com") << QString::fromLatin1("/") |
|
785 |
<< QString::fromLatin1("qsockstest") << QString::fromLatin1("password"); |
|
786 |
||
787 |
#if 0 |
|
788 |
// NTLM requires sending the same request three times for it to work |
|
789 |
// the tst_QHttp class is too strict to handle the byte counts sent by dataSendProgress |
|
790 |
// So don't run this test: |
|
791 |
QTest::newRow("qt-test-server-ntlm") << QtNetworkSettings::serverName() << 3130 |
|
792 |
<< QString::fromLatin1("qt.nokia.com") << QString::fromLatin1("/") |
|
793 |
<< QString::fromLatin1("qsockstest") << QString::fromLatin1("password"); |
|
794 |
#endif |
|
795 |
} |
|
796 |
||
797 |
void tst_QHttp::proxy() |
|
798 |
{ |
|
799 |
QFETCH(QString, proxyhost); |
|
800 |
QFETCH(int, port); |
|
801 |
QFETCH(QString, host); |
|
802 |
QFETCH(QString, path); |
|
803 |
QFETCH(QString, proxyuser); |
|
804 |
QFETCH(QString, proxypass); |
|
805 |
||
806 |
http = newHttp(!proxyuser.isEmpty()); |
|
807 |
||
808 |
QCOMPARE(http->currentId(), 0); |
|
809 |
QCOMPARE((int)http->state(), (int)QHttp::Unconnected); |
|
810 |
||
811 |
addRequest(QHttpRequestHeader(), http->setProxy(proxyhost, port, proxyuser, proxypass)); |
|
812 |
addRequest(QHttpRequestHeader(), http->setHost(host)); |
|
813 |
getId = http->get(path); |
|
814 |
addRequest(QHttpRequestHeader(), getId); |
|
815 |
||
816 |
QTestEventLoop::instance().enterLoop(30); |
|
817 |
if (QTestEventLoop::instance().timeout()) |
|
818 |
QFAIL("Network operation timed out"); |
|
819 |
||
820 |
ResMapIt res = resultMap.find(getId); |
|
821 |
QVERIFY(res != resultMap.end()); |
|
822 |
QVERIFY(res.value().success); |
|
823 |
QCOMPARE(res.value().resp.statusCode(), 200); |
|
824 |
} |
|
825 |
||
826 |
void tst_QHttp::proxy2() |
|
827 |
{ |
|
828 |
QFETCH_GLOBAL(bool, setProxy); |
|
829 |
if (setProxy) |
|
830 |
return; |
|
831 |
||
832 |
readyRead_ba.clear(); |
|
833 |
||
834 |
QHttp http; |
|
835 |
http.setProxy(QtNetworkSettings::serverName(), 3128); |
|
836 |
http.setHost(QtNetworkSettings::serverName()); |
|
837 |
http.get("/index.html"); |
|
838 |
http.get("/index.html"); |
|
839 |
||
840 |
connect(&http, SIGNAL(requestFinished(int, bool)), |
|
841 |
this, SLOT(proxy2_slot())); |
|
842 |
QTestEventLoop::instance().enterLoop(30); |
|
843 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
844 |
||
845 |
QCOMPARE(readyRead_ba.count("Welcome to fluke.troll.no"), 2); |
|
846 |
||
847 |
readyRead_ba.clear(); |
|
848 |
} |
|
849 |
||
850 |
void tst_QHttp::proxy2_slot() |
|
851 |
{ |
|
852 |
QHttp *http = static_cast<QHttp *>(sender()); |
|
853 |
readyRead_ba.append(http->readAll()); |
|
854 |
if (!http->hasPendingRequests()) |
|
855 |
QTestEventLoop::instance().exitLoop(); |
|
856 |
} |
|
857 |
||
858 |
void tst_QHttp::proxy3() |
|
859 |
{ |
|
860 |
QFETCH_GLOBAL(bool, setProxy); |
|
861 |
if (setProxy) |
|
862 |
return; |
|
863 |
||
864 |
readyRead_ba.clear(); |
|
865 |
||
866 |
QTcpSocket socket; |
|
867 |
socket.setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3128)); |
|
868 |
||
869 |
QHttp http; |
|
870 |
http.setSocket(&socket); |
|
871 |
http.setHost(QtNetworkSettings::serverName()); |
|
872 |
http.get("/index.html"); |
|
873 |
http.get("/index.html"); |
|
874 |
||
875 |
connect(&http, SIGNAL(requestFinished(int, bool)), |
|
876 |
this, SLOT(proxy2_slot())); |
|
877 |
QTestEventLoop::instance().enterLoop(30); |
|
878 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
879 |
||
880 |
QCOMPARE(readyRead_ba.count("Welcome to fluke.troll.no"), 2); |
|
881 |
||
882 |
readyRead_ba.clear(); |
|
883 |
} |
|
884 |
||
885 |
// test QHttp::currentId() and QHttp::currentRequest() |
|
886 |
#define CURRENTREQUEST_TEST \ |
|
887 |
{ \ |
|
888 |
ResMapIt res = resultMap.find( http->currentId() ); \ |
|
889 |
QVERIFY( res != resultMap.end() ); \ |
|
890 |
if ( http->currentId() == getId ) { \ |
|
891 |
QCOMPARE( http->currentRequest().method(), QString("GET") ); \ |
|
892 |
} else if ( http->currentId() == headId ) { \ |
|
893 |
QCOMPARE( http->currentRequest().method(), QString("HEAD") ); \ |
|
894 |
} else if ( http->currentId() == postId ) { \ |
|
895 |
QCOMPARE( http->currentRequest().method(), QString("POST") ); \ |
|
896 |
} else { \ |
|
897 |
QVERIFY( headerAreEqual( http->currentRequest(), res.value().req ) ); \ |
|
898 |
} \ |
|
899 |
} |
|
900 |
||
901 |
void tst_QHttp::requestStarted( int id ) |
|
902 |
{ |
|
903 |
#if defined( DUMP_SIGNALS ) |
|
904 |
qDebug( "%d:requestStarted( %d )", http->currentId(), id ); |
|
905 |
#endif |
|
906 |
// make sure that the requestStarted and requestFinished are nested correctly |
|
907 |
QVERIFY( current_id == 0 ); |
|
908 |
current_id = id; |
|
909 |
||
910 |
QVERIFY( !ids.isEmpty() ); |
|
911 |
QVERIFY( ids.first() == id ); |
|
912 |
if ( ids.count() > 1 ) { |
|
913 |
QVERIFY( http->hasPendingRequests() ); |
|
914 |
} else { |
|
915 |
QVERIFY( !http->hasPendingRequests() ); |
|
916 |
} |
|
917 |
||
918 |
QVERIFY( http->currentId() == id ); |
|
919 |
QVERIFY( cur_state == http->state() ); |
|
920 |
||
921 |
||
922 |
||
923 |
||
924 |
CURRENTREQUEST_TEST; |
|
925 |
||
926 |
QVERIFY( http->error() == QHttp::NoError ); |
|
927 |
} |
|
928 |
||
929 |
void tst_QHttp::requestFinished( int id, bool error ) |
|
930 |
{ |
|
931 |
#if defined( DUMP_SIGNALS ) |
|
932 |
qDebug( "%d:requestFinished( %d, %d ) -- errorString: '%s'", |
|
933 |
http->currentId(), id, (int)error, http->errorString().toAscii().data() ); |
|
934 |
#endif |
|
935 |
// make sure that the requestStarted and requestFinished are nested correctly |
|
936 |
QVERIFY( current_id == id ); |
|
937 |
current_id = 0; |
|
938 |
||
939 |
QVERIFY( !ids.isEmpty() ); |
|
940 |
QVERIFY( ids.first() == id ); |
|
941 |
if ( ids.count() > 1 ) { |
|
942 |
QVERIFY( http->hasPendingRequests() ); |
|
943 |
} else { |
|
944 |
QVERIFY( !http->hasPendingRequests() ); |
|
945 |
} |
|
946 |
||
947 |
if ( error ) { |
|
948 |
QVERIFY( http->error() != QHttp::NoError ); |
|
949 |
ids.clear(); |
|
950 |
} else { |
|
951 |
QVERIFY( http->error() == QHttp::NoError ); |
|
952 |
ids.pop_front(); |
|
953 |
} |
|
954 |
||
955 |
QVERIFY( http->currentId() == id ); |
|
956 |
QVERIFY( cur_state == http->state() ); |
|
957 |
CURRENTREQUEST_TEST; |
|
958 |
||
959 |
ResMapIt res = resultMap.find( http->currentId() ); |
|
960 |
QVERIFY( res != resultMap.end() ); |
|
961 |
QVERIFY( res.value().success == -1 ); |
|
962 |
if ( error ) |
|
963 |
res.value().success = 0; |
|
964 |
else |
|
965 |
res.value().success = 1; |
|
966 |
} |
|
967 |
||
968 |
void tst_QHttp::done( bool error ) |
|
969 |
{ |
|
970 |
#if defined( DUMP_SIGNALS ) |
|
971 |
qDebug( "%d:done( %d )", http->currentId(), (int)error ); |
|
972 |
#endif |
|
973 |
QVERIFY( http->currentId() == 0 ); |
|
974 |
QVERIFY( current_id == 0 ); |
|
975 |
QVERIFY( ids.isEmpty() ); |
|
976 |
QVERIFY( cur_state == http->state() ); |
|
977 |
QVERIFY( !http->hasPendingRequests() ); |
|
978 |
||
979 |
QVERIFY( done_success == -1 ); |
|
980 |
if ( error ) { |
|
981 |
QVERIFY( http->error() != QHttp::NoError ); |
|
982 |
done_success = 0; |
|
983 |
} else { |
|
984 |
QVERIFY( http->error() == QHttp::NoError ); |
|
985 |
done_success = 1; |
|
986 |
} |
|
987 |
QTestEventLoop::instance().exitLoop(); |
|
988 |
} |
|
989 |
||
990 |
void tst_QHttp::stateChanged( int state ) |
|
991 |
{ |
|
992 |
#if defined( DUMP_SIGNALS ) |
|
993 |
qDebug( "%d: stateChanged( %d )", http->currentId(), state ); |
|
994 |
#endif |
|
995 |
QCOMPARE( http->currentId(), current_id ); |
|
996 |
if ( ids.count() > 0 ) |
|
997 |
CURRENTREQUEST_TEST; |
|
998 |
||
999 |
QVERIFY( state != cur_state ); |
|
1000 |
QVERIFY( state == http->state() ); |
|
1001 |
if ( state != QHttp::Unconnected && !connectionWithAuth ) { |
|
1002 |
// make sure that the states are always emitted in the right order (for |
|
1003 |
// this, we assume an ordering on the enum values, which they have at |
|
1004 |
// the moment) |
|
1005 |
// connections with authentication will possibly reconnect, so ignore them |
|
1006 |
QVERIFY( cur_state < state ); |
|
1007 |
} |
|
1008 |
cur_state = state; |
|
1009 |
||
1010 |
if (state == QHttp::Connecting) { |
|
1011 |
bytesTotalSend = bytesTotal_init; |
|
1012 |
bytesDoneSend = bytesDone_init; |
|
1013 |
bytesTotalRead = bytesTotal_init; |
|
1014 |
bytesDoneRead = bytesDone_init; |
|
1015 |
} |
|
1016 |
} |
|
1017 |
||
1018 |
void tst_QHttp::responseHeaderReceived( const QHttpResponseHeader &header ) |
|
1019 |
{ |
|
1020 |
#if defined( DUMP_SIGNALS ) |
|
1021 |
qDebug( "%d: responseHeaderReceived(\n---{\n%s}---)", http->currentId(), header.toString().toAscii().data() ); |
|
1022 |
#endif |
|
1023 |
QCOMPARE( http->currentId(), current_id ); |
|
1024 |
if ( ids.count() > 1 ) { |
|
1025 |
QVERIFY( http->hasPendingRequests() ); |
|
1026 |
} else { |
|
1027 |
QVERIFY( !http->hasPendingRequests() ); |
|
1028 |
} |
|
1029 |
CURRENTREQUEST_TEST; |
|
1030 |
||
1031 |
resultMap[ http->currentId() ].resp = header; |
|
1032 |
} |
|
1033 |
||
1034 |
void tst_QHttp::readyRead( const QHttpResponseHeader & ) |
|
1035 |
{ |
|
1036 |
#if defined( DUMP_SIGNALS ) |
|
1037 |
qDebug( "%d: readyRead()", http->currentId() ); |
|
1038 |
#endif |
|
1039 |
QCOMPARE( http->currentId(), current_id ); |
|
1040 |
if ( ids.count() > 1 ) { |
|
1041 |
QVERIFY( http->hasPendingRequests() ); |
|
1042 |
} else { |
|
1043 |
QVERIFY( !http->hasPendingRequests() ); |
|
1044 |
} |
|
1045 |
QVERIFY( cur_state == http->state() ); |
|
1046 |
CURRENTREQUEST_TEST; |
|
1047 |
||
1048 |
if ( QTest::currentTestFunction() != QLatin1String("bytesAvailable") ) { |
|
1049 |
int oldSize = readyRead_ba.size(); |
|
1050 |
quint64 bytesAvail = http->bytesAvailable(); |
|
1051 |
QByteArray ba = http->readAll(); |
|
1052 |
QVERIFY( (quint64) ba.size() == bytesAvail ); |
|
1053 |
readyRead_ba.resize( oldSize + ba.size() ); |
|
1054 |
memcpy( readyRead_ba.data()+oldSize, ba.data(), ba.size() ); |
|
1055 |
||
1056 |
if ( bytesTotalRead > 0 ) { |
|
1057 |
QVERIFY( (int)readyRead_ba.size() <= bytesTotalRead ); |
|
1058 |
} |
|
1059 |
QVERIFY( (int)readyRead_ba.size() == bytesDoneRead ); |
|
1060 |
} |
|
1061 |
} |
|
1062 |
||
1063 |
void tst_QHttp::dataSendProgress( int done, int total ) |
|
1064 |
{ |
|
1065 |
#if defined( DUMP_SIGNALS ) |
|
1066 |
qDebug( "%d: dataSendProgress( %d, %d )", http->currentId(), done, total ); |
|
1067 |
#endif |
|
1068 |
QCOMPARE( http->currentId(), current_id ); |
|
1069 |
if ( ids.count() > 1 ) { |
|
1070 |
QVERIFY( http->hasPendingRequests() ); |
|
1071 |
} else { |
|
1072 |
QVERIFY( !http->hasPendingRequests() ); |
|
1073 |
} |
|
1074 |
QVERIFY( cur_state == http->state() ); |
|
1075 |
CURRENTREQUEST_TEST; |
|
1076 |
||
1077 |
if ( bytesTotalSend == bytesTotal_init ) { |
|
1078 |
bytesTotalSend = total; |
|
1079 |
} else { |
|
1080 |
QCOMPARE( bytesTotalSend, total ); |
|
1081 |
} |
|
1082 |
||
1083 |
QVERIFY( bytesTotalSend != bytesTotal_init ); |
|
1084 |
QVERIFY( bytesDoneSend <= done ); |
|
1085 |
bytesDoneSend = done; |
|
1086 |
if ( bytesTotalSend > 0 ) { |
|
1087 |
QVERIFY( bytesDoneSend <= bytesTotalSend ); |
|
1088 |
} |
|
1089 |
||
1090 |
if ( QTest::currentTestFunction() == QLatin1String("abort") ) { |
|
1091 |
// ### it would be nice if we could specify in our testdata when to do |
|
1092 |
// the abort |
|
1093 |
if ( done >= total/100000 ) { |
|
1094 |
if ( ids.count() != 1 ) { |
|
1095 |
// do abort only once |
|
1096 |
int tmpId = ids.first(); |
|
1097 |
ids.clear(); |
|
1098 |
ids << tmpId; |
|
1099 |
http->abort(); |
|
1100 |
} |
|
1101 |
} |
|
1102 |
} |
|
1103 |
} |
|
1104 |
||
1105 |
void tst_QHttp::dataReadProgress( int done, int total ) |
|
1106 |
{ |
|
1107 |
#if defined( DUMP_SIGNALS ) |
|
1108 |
qDebug( "%d: dataReadProgress( %d, %d )", http->currentId(), done, total ); |
|
1109 |
#endif |
|
1110 |
QCOMPARE( http->currentId(), current_id ); |
|
1111 |
if ( ids.count() > 1 ) { |
|
1112 |
QVERIFY( http->hasPendingRequests() ); |
|
1113 |
} else { |
|
1114 |
QVERIFY( !http->hasPendingRequests() ); |
|
1115 |
} |
|
1116 |
QVERIFY( cur_state == http->state() ); |
|
1117 |
CURRENTREQUEST_TEST; |
|
1118 |
||
1119 |
if ( bytesTotalRead == bytesTotal_init ) |
|
1120 |
bytesTotalRead = total; |
|
1121 |
else { |
|
1122 |
QVERIFY( bytesTotalRead == total ); |
|
1123 |
} |
|
1124 |
||
1125 |
QVERIFY( bytesTotalRead != bytesTotal_init ); |
|
1126 |
QVERIFY( bytesDoneRead <= done ); |
|
1127 |
bytesDoneRead = done; |
|
1128 |
if ( bytesTotalRead > 0 ) { |
|
1129 |
QVERIFY( bytesDoneRead <= bytesTotalRead ); |
|
1130 |
} |
|
1131 |
||
1132 |
if ( QTest::currentTestFunction() == QLatin1String("abort") ) { |
|
1133 |
// ### it would be nice if we could specify in our testdata when to do |
|
1134 |
// the abort |
|
1135 |
if ( done >= total/100000 ) { |
|
1136 |
if ( ids.count() != 1 ) { |
|
1137 |
// do abort only once |
|
1138 |
int tmpId = ids.first(); |
|
1139 |
ids.clear(); |
|
1140 |
ids << tmpId; |
|
1141 |
http->abort(); |
|
1142 |
} |
|
1143 |
} |
|
1144 |
} |
|
1145 |
} |
|
1146 |
||
1147 |
||
1148 |
QHttp *tst_QHttp::newHttp(bool withAuth) |
|
1149 |
{ |
|
1150 |
QHttp *nHttp = new QHttp( 0 ); |
|
1151 |
connect( nHttp, SIGNAL(requestStarted(int)), |
|
1152 |
SLOT(requestStarted(int)) ); |
|
1153 |
connect( nHttp, SIGNAL(requestFinished(int,bool)), |
|
1154 |
SLOT(requestFinished(int,bool)) ); |
|
1155 |
connect( nHttp, SIGNAL(done(bool)), |
|
1156 |
SLOT(done(bool)) ); |
|
1157 |
connect( nHttp, SIGNAL(stateChanged(int)), |
|
1158 |
SLOT(stateChanged(int)) ); |
|
1159 |
connect( nHttp, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), |
|
1160 |
SLOT(responseHeaderReceived(const QHttpResponseHeader&)) ); |
|
1161 |
connect( nHttp, SIGNAL(readyRead(const QHttpResponseHeader&)), |
|
1162 |
SLOT(readyRead(const QHttpResponseHeader&)) ); |
|
1163 |
connect( nHttp, SIGNAL(dataSendProgress(int,int)), |
|
1164 |
SLOT(dataSendProgress(int,int)) ); |
|
1165 |
connect( nHttp, SIGNAL(dataReadProgress(int,int)), |
|
1166 |
SLOT(dataReadProgress(int,int)) ); |
|
1167 |
||
1168 |
connectionWithAuth = withAuth; |
|
1169 |
return nHttp; |
|
1170 |
} |
|
1171 |
||
1172 |
void tst_QHttp::addRequest( QHttpRequestHeader header, int id ) |
|
1173 |
{ |
|
1174 |
ids << id; |
|
1175 |
RequestResult res; |
|
1176 |
res.req = header; |
|
1177 |
res.success = -1; |
|
1178 |
resultMap[ id ] = res; |
|
1179 |
} |
|
1180 |
||
1181 |
bool tst_QHttp::headerAreEqual( const QHttpHeader &h1, const QHttpHeader &h2 ) |
|
1182 |
{ |
|
1183 |
if ( !h1.isValid() ) |
|
1184 |
return !h2.isValid(); |
|
1185 |
if ( !h2.isValid() ) |
|
1186 |
return !h1.isValid(); |
|
1187 |
||
1188 |
return h1.toString() == h2.toString(); |
|
1189 |
} |
|
1190 |
||
1191 |
||
1192 |
void tst_QHttp::reconnect() |
|
1193 |
{ |
|
1194 |
reconnect_state_connect_count = 0; |
|
1195 |
||
1196 |
QHttp http; |
|
1197 |
||
1198 |
QObject::connect(&http, SIGNAL(stateChanged(int)), this, SLOT(reconnect_state(int))); |
|
1199 |
http.setHost("trolltech.com", 80); |
|
1200 |
http.get("/company/index.html"); |
|
1201 |
http.setHost("trolltech.com", 8080); |
|
1202 |
http.get("/company/index.html"); |
|
1203 |
||
1204 |
QTestEventLoop::instance().enterLoop(60); |
|
1205 |
if (QTestEventLoop::instance().timeout()) |
|
1206 |
QFAIL("Network operation timed out"); |
|
1207 |
||
1208 |
QCOMPARE(reconnect_state_connect_count, 1); |
|
1209 |
||
1210 |
QTestEventLoop::instance().enterLoop(60); |
|
1211 |
if (QTestEventLoop::instance().timeout()) |
|
1212 |
QFAIL("Network operation timed out"); |
|
1213 |
||
1214 |
QCOMPARE(reconnect_state_connect_count, 2); |
|
1215 |
} |
|
1216 |
||
1217 |
void tst_QHttp::reconnect_state(int state) |
|
1218 |
{ |
|
1219 |
if (state == QHttp::Connecting) { |
|
1220 |
++reconnect_state_connect_count; |
|
1221 |
QTestEventLoop::instance().exitLoop(); |
|
1222 |
} |
|
1223 |
} |
|
1224 |
||
1225 |
void tst_QHttp::setSocket() |
|
1226 |
{ |
|
1227 |
QHttp *http = new QHttp; |
|
1228 |
QPointer<QTcpSocket> replacementSocket = new QTcpSocket; |
|
1229 |
http->setSocket(replacementSocket); |
|
1230 |
QCoreApplication::processEvents(); |
|
1231 |
delete http; |
|
1232 |
QVERIFY(replacementSocket); |
|
1233 |
delete replacementSocket; |
|
1234 |
} |
|
1235 |
||
1236 |
class Server : public QTcpServer |
|
1237 |
{ |
|
1238 |
Q_OBJECT |
|
1239 |
public: |
|
1240 |
Server() |
|
1241 |
{ |
|
1242 |
connect(this, SIGNAL(newConnection()), |
|
1243 |
this, SLOT(serveConnection())); |
|
1244 |
} |
|
1245 |
||
1246 |
private slots: |
|
1247 |
void serveConnection() |
|
1248 |
{ |
|
1249 |
QTcpSocket *socket = nextPendingConnection(); |
|
1250 |
socket->write("HTTP/1.1 404 Not found\r\n" |
|
1251 |
"content-length: 4\r\n\r\nabcd"); |
|
1252 |
socket->disconnectFromHost(); |
|
1253 |
}; |
|
1254 |
}; |
|
1255 |
||
1256 |
void tst_QHttp::unexpectedRemoteClose() |
|
1257 |
{ |
|
1258 |
QFETCH_GLOBAL(int, proxyType); |
|
1259 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
1260 |
// This test doesn't make sense for SOCKS5 |
|
1261 |
return; |
|
1262 |
} |
|
1263 |
||
1264 |
Server server; |
|
1265 |
server.listen(); |
|
1266 |
QCoreApplication::instance()->processEvents(); |
|
1267 |
||
1268 |
QEventLoop loop; |
|
1269 |
QTimer::singleShot(3000, &loop, SLOT(quit())); |
|
1270 |
||
1271 |
QHttp http; |
|
1272 |
QObject::connect(&http, SIGNAL(done(bool)), &loop, SLOT(quit())); |
|
1273 |
QSignalSpy finishedSpy(&http, SIGNAL(requestFinished(int, bool))); |
|
1274 |
QSignalSpy doneSpy(&http, SIGNAL(done(bool))); |
|
1275 |
||
1276 |
http.setHost("localhost", server.serverPort()); |
|
1277 |
http.get("/"); |
|
1278 |
http.get("/"); |
|
1279 |
http.get("/"); |
|
1280 |
||
1281 |
loop.exec(); |
|
1282 |
||
1283 |
QCOMPARE(finishedSpy.count(), 4); |
|
1284 |
QVERIFY(!finishedSpy.at(1).at(1).toBool()); |
|
1285 |
QVERIFY(!finishedSpy.at(2).at(1).toBool()); |
|
1286 |
QVERIFY(!finishedSpy.at(3).at(1).toBool()); |
|
1287 |
QCOMPARE(doneSpy.count(), 1); |
|
1288 |
QVERIFY(!doneSpy.at(0).at(0).toBool()); |
|
1289 |
} |
|
1290 |
||
1291 |
void tst_QHttp::pctEncodedPath() |
|
1292 |
{ |
|
1293 |
QHttpRequestHeader header; |
|
1294 |
header.setRequest("GET", "/index.asp/a=%20&b=%20&c=%20"); |
|
1295 |
QCOMPARE(header.toString(), QString("GET /index.asp/a=%20&b=%20&c=%20 HTTP/1.1\r\n\r\n")); |
|
1296 |
} |
|
1297 |
||
1298 |
void tst_QHttp::caseInsensitiveKeys() |
|
1299 |
{ |
|
1300 |
QHttpResponseHeader header("HTTP/1.1 200 OK\r\nContent-Length: 213\r\nX-Been-There: True\r\nLocation: http://www.TrollTech.com/\r\n\r\n"); |
|
1301 |
QVERIFY(header.hasKey("Content-Length")); |
|
1302 |
QVERIFY(header.hasKey("X-Been-There")); |
|
1303 |
QVERIFY(header.hasKey("Location")); |
|
1304 |
QVERIFY(header.hasKey("content-length")); |
|
1305 |
QVERIFY(header.hasKey("x-been-there")); |
|
1306 |
QVERIFY(header.hasKey("location")); |
|
1307 |
QCOMPARE(header.value("Content-Length"), QString("213")); |
|
1308 |
QCOMPARE(header.value("X-Been-There"), QString("True")); |
|
1309 |
QCOMPARE(header.value("Location"), QString("http://www.TrollTech.com/")); |
|
1310 |
QCOMPARE(header.value("content-length"), QString("213")); |
|
1311 |
QCOMPARE(header.value("x-been-there"), QString("True")); |
|
1312 |
QCOMPARE(header.value("location"), QString("http://www.TrollTech.com/")); |
|
1313 |
QCOMPARE(header.allValues("location"), QStringList("http://www.TrollTech.com/")); |
|
1314 |
||
1315 |
header.addValue("Content-Length", "213"); |
|
1316 |
header.addValue("Content-Length", "214"); |
|
1317 |
header.addValue("Content-Length", "215"); |
|
1318 |
qDebug() << header.toString(); |
|
1319 |
} |
|
1320 |
||
1321 |
void tst_QHttp::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) |
|
1322 |
{ |
|
1323 |
proxyAuthCalled = true; |
|
1324 |
auth->setUser("qsockstest"); |
|
1325 |
auth->setPassword("password"); |
|
1326 |
} |
|
1327 |
||
1328 |
void tst_QHttp::postAuthNtlm() |
|
1329 |
{ |
|
1330 |
QSKIP("NTLM not working", SkipAll); |
|
1331 |
||
1332 |
QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName()); |
|
1333 |
QByteArray postData("Hello World"); |
|
1334 |
QHttp http; |
|
1335 |
||
1336 |
http.setHost(QtNetworkSettings::serverName()); |
|
1337 |
http.setProxy(QtNetworkSettings::serverName(), 3130); |
|
1338 |
http.post("/", postData); |
|
1339 |
||
1340 |
proxyAuthCalled = false; |
|
1341 |
connect(&http, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1342 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1343 |
||
1344 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1345 |
QTestEventLoop::instance().enterLoop(3); |
|
1346 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1347 |
||
1348 |
QVERIFY(proxyAuthCalled); |
|
1349 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1350 |
}; |
|
1351 |
||
1352 |
void tst_QHttp::proxyAndSsl() |
|
1353 |
{ |
|
1354 |
#ifdef QT_NO_OPENSSL |
|
1355 |
QSKIP("No OpenSSL support in this platform", SkipAll); |
|
1356 |
#else |
|
1357 |
QFETCH_GLOBAL(bool, setProxy); |
|
1358 |
if (setProxy) |
|
1359 |
return; |
|
1360 |
||
1361 |
QHttp http; |
|
1362 |
||
1363 |
http.setHost(QtNetworkSettings::serverName(), QHttp::ConnectionModeHttps); |
|
1364 |
http.setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129)); |
|
1365 |
http.get("/"); |
|
1366 |
||
1367 |
proxyAuthCalled = false; |
|
1368 |
connect(&http, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1369 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1370 |
connect(&http, SIGNAL(sslErrors(QList<QSslError>)), |
|
1371 |
&http, SLOT(ignoreSslErrors())); |
|
1372 |
||
1373 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1374 |
QTestEventLoop::instance().enterLoop(3); |
|
1375 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1376 |
||
1377 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1378 |
QVERIFY(proxyAuthCalled); |
|
1379 |
||
1380 |
QHttpResponseHeader header = http.lastResponse(); |
|
1381 |
QVERIFY(header.isValid()); |
|
1382 |
QVERIFY(header.statusCode() < 400); // Should be 200, but as long as it's not an error, we're happy |
|
1383 |
#endif |
|
1384 |
} |
|
1385 |
||
1386 |
void tst_QHttp::cachingProxyAndSsl() |
|
1387 |
{ |
|
1388 |
#ifdef QT_NO_OPENSSL |
|
1389 |
QSKIP("No OpenSSL support in this platform", SkipAll); |
|
1390 |
#else |
|
1391 |
QFETCH_GLOBAL(bool, setProxy); |
|
1392 |
if (setProxy) |
|
1393 |
return; |
|
1394 |
||
1395 |
QHttp http; |
|
1396 |
||
1397 |
http.setHost(QtNetworkSettings::serverName(), QHttp::ConnectionModeHttps); |
|
1398 |
http.setProxy(QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)); |
|
1399 |
http.get("/"); |
|
1400 |
||
1401 |
proxyAuthCalled = false; |
|
1402 |
connect(&http, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1403 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1404 |
||
1405 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1406 |
QTestEventLoop::instance().enterLoop(3); |
|
1407 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1408 |
||
1409 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1410 |
QVERIFY(!proxyAuthCalled); // NOT called! QHttp should get a socket error |
|
1411 |
QVERIFY(http.state() != QHttp::Connected); |
|
1412 |
||
1413 |
QHttpResponseHeader header = http.lastResponse(); |
|
1414 |
QVERIFY(!header.isValid()); |
|
1415 |
#endif |
|
1416 |
} |
|
1417 |
||
1418 |
void tst_QHttp::emptyBodyInReply() |
|
1419 |
{ |
|
1420 |
// Note: if this test starts failing, please verify the date on the file |
|
1421 |
// returned by Apache on http://netiks.troll.no/ |
|
1422 |
// It is right now hard-coded to the date below |
|
1423 |
QHttp http; |
|
1424 |
http.setHost(QtNetworkSettings::serverName()); |
|
1425 |
||
1426 |
QHttpRequestHeader headers("GET", "/"); |
|
1427 |
headers.addValue("If-Modified-Since", "Sun, 16 Nov 2008 12:29:51 GMT"); |
|
1428 |
headers.addValue("Host", QtNetworkSettings::serverName()); |
|
1429 |
http.request(headers); |
|
1430 |
||
1431 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1432 |
QTestEventLoop::instance().enterLoop(10); |
|
1433 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1434 |
||
1435 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1436 |
||
1437 |
// check the reply |
|
1438 |
if (http.lastResponse().statusCode() != 304) { |
|
1439 |
qWarning() << http.lastResponse().statusCode() << qPrintable(http.lastResponse().reasonPhrase()); |
|
1440 |
qWarning() << "Last-Modified:" << qPrintable(http.lastResponse().value("last-modified")); |
|
1441 |
QFAIL("Server replied with the wrong status code; see warning output"); |
|
1442 |
} |
|
1443 |
} |
|
1444 |
||
1445 |
void tst_QHttp::abortSender() |
|
1446 |
{ |
|
1447 |
QHttp *http = qobject_cast<QHttp *>(sender()); |
|
1448 |
if (http) |
|
1449 |
http->abort(); |
|
1450 |
} |
|
1451 |
||
1452 |
void tst_QHttp::abortInReadyRead() |
|
1453 |
{ |
|
1454 |
QHttp http; |
|
1455 |
http.setHost(QtNetworkSettings::serverName()); |
|
1456 |
http.get("/qtest/bigfile"); |
|
1457 |
||
1458 |
qRegisterMetaType<QHttpResponseHeader>(); |
|
1459 |
QSignalSpy spy(&http, SIGNAL(readyRead(QHttpResponseHeader))); |
|
1460 |
||
1461 |
QObject::connect(&http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(abortSender())); |
|
1462 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1463 |
QTestEventLoop::instance().enterLoop(10); |
|
1464 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1465 |
||
1466 |
QVERIFY2(!QTestEventLoop::instance().timeout(), "Network timeout"); |
|
1467 |
QVERIFY(http.state() != QHttp::Connected); |
|
1468 |
||
1469 |
QCOMPARE(spy.count(), 1); |
|
1470 |
} |
|
1471 |
||
1472 |
void tst_QHttp::abortInResponseHeaderReceived() |
|
1473 |
{ |
|
1474 |
QHttp http; |
|
1475 |
http.setHost(QtNetworkSettings::serverName()); |
|
1476 |
http.get("/qtest/bigfile"); |
|
1477 |
||
1478 |
qRegisterMetaType<QHttpResponseHeader>(); |
|
1479 |
QSignalSpy spy(&http, SIGNAL(readyRead(QHttpResponseHeader))); |
|
1480 |
||
1481 |
QObject::connect(&http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), this, SLOT(abortSender())); |
|
1482 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1483 |
QTestEventLoop::instance().enterLoop(10); |
|
1484 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1485 |
||
1486 |
QVERIFY2(!QTestEventLoop::instance().timeout(), "Network timeout"); |
|
1487 |
QVERIFY(http.state() != QHttp::Connected); |
|
1488 |
||
1489 |
QCOMPARE(spy.count(), 0); |
|
1490 |
} |
|
1491 |
||
1492 |
void tst_QHttp::connectionClose() |
|
1493 |
{ |
|
1494 |
// This test tries to connect to a client's server, so it is disabled. |
|
1495 |
// Every now and then, someone please run it to make sure it's not broken. |
|
1496 |
// Note: the servers might change too... |
|
1497 |
// |
|
1498 |
// This was added in response to bug 176822 |
|
1499 |
#ifndef Q_OS_SYMBIAN |
|
1500 |
QSKIP("This test is manual - read comments in the source code", SkipAll); |
|
1501 |
#endif |
|
1502 |
QFETCH_GLOBAL(bool, setProxy); |
|
1503 |
if (setProxy) |
|
1504 |
return; |
|
1505 |
||
1506 |
QHttp http; |
|
1507 |
http.setHost("www.fon.com", QHttp::ConnectionModeHttps); |
|
1508 |
http.get("/login/gateway/processLogin"); |
|
1509 |
||
1510 |
// another possibility: |
|
1511 |
//http.setHost("nexus.passport.com", QHttp::ConnectionModeHttps, 443); |
|
1512 |
//http.get("/rdr/pprdr.asp"); |
|
1513 |
||
1514 |
QObject::connect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1515 |
QTestEventLoop::instance().enterLoop(900); |
|
1516 |
QObject::disconnect(&http, SIGNAL(done(bool)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1517 |
||
1518 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
1519 |
} |
|
1520 |
||
1521 |
void tst_QHttp::nestedEventLoop_slot(int id) |
|
1522 |
{ |
|
1523 |
if (!ids.contains(id)) |
|
1524 |
return; |
|
1525 |
QEventLoop subloop; |
|
1526 |
||
1527 |
// 16 seconds: fluke times out in 15 seconds, which triggers a QTcpSocket error |
|
1528 |
QTimer::singleShot(16000, &subloop, SLOT(quit())); |
|
1529 |
subloop.exec(); |
|
1530 |
||
1531 |
QTestEventLoop::instance().exitLoop(); |
|
1532 |
} |
|
1533 |
||
1534 |
void tst_QHttp::nestedEventLoop() |
|
1535 |
{ |
|
1536 |
QFETCH_GLOBAL(bool, setProxy); |
|
1537 |
if (setProxy) |
|
1538 |
return; |
|
1539 |
||
1540 |
http = new QHttp; |
|
1541 |
http->setHost(QtNetworkSettings::serverName()); |
|
1542 |
int getId = http->get("/"); |
|
1543 |
||
1544 |
ids.clear(); |
|
1545 |
ids << getId; |
|
1546 |
||
1547 |
QSignalSpy spy(http, SIGNAL(requestStarted(int))); |
|
1548 |
QSignalSpy spy2(http, SIGNAL(done(bool))); |
|
1549 |
||
1550 |
connect(http, SIGNAL(requestFinished(int,bool)), SLOT(nestedEventLoop_slot(int))); |
|
1551 |
QTestEventLoop::instance().enterLoop(20); |
|
1552 |
||
1553 |
QVERIFY2(!QTestEventLoop::instance().timeout(), "Network timeout"); |
|
1554 |
||
1555 |
// Find out how many signals with the first argument equalling our id were found |
|
1556 |
int spyCount = 0; |
|
1557 |
for (int i = 0; i < spy.count(); ++i) |
|
1558 |
if (spy.at(i).at(0).toInt() == getId) |
|
1559 |
++spyCount; |
|
1560 |
||
1561 |
// each signal spied should have been emitted only once |
|
1562 |
QCOMPARE(spyCount, 1); |
|
1563 |
QCOMPARE(spy2.count(), 1); |
|
1564 |
} |
|
1565 |
||
1566 |
QTEST_MAIN(tst_QHttp) |
|
1567 |
#include "tst_qhttp.moc" |