1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
44
#include <QtCore/QUrl>
45
#include <QtNetwork/QNetworkRequest>
46
#include <QtNetwork/QNetworkCookie>
48
Q_DECLARE_METATYPE(QNetworkRequest::KnownHeaders)
50
class tst_QNetworkRequest: public QObject
59
void setRawHeader_data();
61
void rawHeaderList_data();
63
void setHeader_data();
65
void rawHeaderParsing_data();
66
void rawHeaderParsing();
67
void originatingObject();
76
char *toString(const QNetworkCookie &cookie)
78
return qstrdup(cookie.toRawForm());
82
char *toString(const QList<QNetworkCookie> &list)
84
QString result = "QList(";
86
foreach (QNetworkCookie cookie, list) {
90
result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm()));
93
return qstrdup(result.append(')').toLocal8Bit());
99
void tst_QNetworkRequest::ctor_data()
101
QTest::addColumn<QUrl>("url");
103
QTest::newRow("nothing") << QUrl();
104
QTest::newRow("empty") << QUrl();
105
QTest::newRow("http") << QUrl("http://qt.nokia.com");
108
void tst_QNetworkRequest::ctor()
112
if (qstrcmp(QTest::currentDataTag(), "nothing") == 0) {
113
QNetworkRequest request;
114
QCOMPARE(request.url(), url);
116
QNetworkRequest request(url);
117
QCOMPARE(request.url(), url);
121
void tst_QNetworkRequest::setUrl_data()
126
void tst_QNetworkRequest::setUrl()
129
QNetworkRequest request;
131
if (qstrcmp(QTest::currentDataTag(), "nothing") != 0)
134
QCOMPARE(request.url(), url);
137
void tst_QNetworkRequest::setRawHeader_data()
139
QTest::addColumn<QByteArray>("header");
140
QTest::addColumn<QByteArray>("value");
141
QTest::addColumn<QByteArray>("headerToGet");
142
QTest::addColumn<QByteArray>("expectedValue");
143
QTest::addColumn<bool>("hasHeader");
145
QTest::newRow("null-header") << QByteArray() << QByteArray("abc")
146
<< QByteArray() << QByteArray() << false;
147
QTest::newRow("empty-header") << QByteArray("") << QByteArray("abc")
148
<< QByteArray("") << QByteArray() << false;
149
QTest::newRow("null-value") << QByteArray("foo") << QByteArray()
150
<< QByteArray("foo") << QByteArray() << false;
151
QTest::newRow("empty-value") << QByteArray("foo") << QByteArray("")
152
<< QByteArray("foo") << QByteArray("") << true;
153
QTest::newRow("empty-value-vs-null") << QByteArray("foo") << QByteArray("")
154
<< QByteArray("foo") << QByteArray() << true;
156
QTest::newRow("UPPER-UPPER") << QByteArray("FOO") << QByteArray("abc")
157
<< QByteArray("FOO") << QByteArray("abc") << true;
158
QTest::newRow("UPPER-Mixed") << QByteArray("FOO") << QByteArray("abc")
159
<< QByteArray("Foo") << QByteArray("abc") << true;
160
QTest::newRow("UPPER-lower") << QByteArray("FOO") << QByteArray("abc")
161
<< QByteArray("foo") << QByteArray("abc") << true;
162
QTest::newRow("Mixed-UPPER") << QByteArray("Foo") << QByteArray("abc")
163
<< QByteArray("FOO") << QByteArray("abc") << true;
164
QTest::newRow("Mixed-Mixed") << QByteArray("Foo") << QByteArray("abc")
165
<< QByteArray("Foo") << QByteArray("abc") << true;
166
QTest::newRow("Mixed-lower") << QByteArray("Foo") << QByteArray("abc")
167
<< QByteArray("foo") << QByteArray("abc") << true;
168
QTest::newRow("lower-UPPER") << QByteArray("foo") << QByteArray("abc")
169
<< QByteArray("FOO") << QByteArray("abc") << true;
170
QTest::newRow("lower-Mixed") << QByteArray("foo") << QByteArray("abc")
171
<< QByteArray("Foo") << QByteArray("abc") << true;
172
QTest::newRow("lower-lower") << QByteArray("foo") << QByteArray("abc")
173
<< QByteArray("foo") << QByteArray("abc") << true;
176
void tst_QNetworkRequest::setRawHeader()
178
QFETCH(QByteArray, header);
179
QFETCH(QByteArray, value);
180
QFETCH(QByteArray, headerToGet);
181
QFETCH(QByteArray, expectedValue);
182
QFETCH(bool, hasHeader);
184
QNetworkRequest request;
185
request.setRawHeader(header, value);
187
QCOMPARE(request.hasRawHeader(headerToGet), hasHeader);
188
QCOMPARE(request.rawHeader(headerToGet), expectedValue);
191
void tst_QNetworkRequest::rawHeaderList_data()
193
QTest::addColumn<QList<QByteArray> >("set");
194
QTest::addColumn<QList<QByteArray> >("expected");
196
QTest::newRow("empty") << QList<QByteArray>() << QList<QByteArray>();
198
QList<QByteArray> set;
199
QList<QByteArray> expected;
203
QTest::newRow("one") << set << expected;
207
QTest::newRow("two") << set << expected;
211
set << "foo" << "foo";
213
QTest::newRow("repeated") << set << expected;
217
set << "foo" << "bar" << "foo";
218
expected << "bar" << "foo";
219
QTest::newRow("repeated-interleaved") << set << expected;
222
void tst_QNetworkRequest::rawHeaderList()
224
QFETCH(QList<QByteArray>, set);
225
QFETCH(QList<QByteArray>, expected);
227
QNetworkRequest request;
228
foreach (QByteArray header, set)
229
request.setRawHeader(header, "a value");
231
QList<QByteArray> got = request.rawHeaderList();
232
QCOMPARE(got.size(), expected.size());
233
for (int i = 0; i < got.size(); ++i)
234
QCOMPARE(got.at(i), expected.at(i));
237
void tst_QNetworkRequest::setHeader_data()
239
QTest::addColumn<QNetworkRequest::KnownHeaders>("cookedHeader");
240
QTest::addColumn<QVariant>("cookedValue");
241
QTest::addColumn<bool>("success");
242
QTest::addColumn<QString>("rawHeader");
243
QTest::addColumn<QString>("rawValue");
245
QTest::newRow("Content-Type-Null") << QNetworkRequest::ContentTypeHeader << QVariant()
246
<< false << "Content-Type" << "";
247
QTest::newRow("Content-Type-String") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
249
<< "Content-Type" << "text/html";
250
QTest::newRow("Content-Type-ByteArray") << QNetworkRequest::ContentTypeHeader
251
<< QVariant("text/html") << true
252
<< "Content-Type" << "text/html";
254
QTest::newRow("Content-Length-Int") << QNetworkRequest::ContentLengthHeader << QVariant(1)
255
<< true << "Content-Length" << "1";
256
QTest::newRow("Content-Length-Int64") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
257
<< true << "Content-Length" << "1";
259
QTest::newRow("Location-String") << QNetworkRequest::LocationHeader << QVariant("http://foo/with space")
260
<< true << "Location" << "http://foo/with space";
261
QTest::newRow("Location-ByteArray") << QNetworkRequest::LocationHeader
262
<< QVariant("http://foo/with space")
263
<< true << "Location" << "http://foo/with space";
264
QTest::newRow("Location-Url") << QNetworkRequest::LocationHeader
265
<< QVariant(QUrl("http://foo/with space"))
266
<< true << "Location" << "http://foo/with%20space";
268
QTest::newRow("Last-Modified-Date") << QNetworkRequest::LastModifiedHeader
269
<< QVariant(QDate(2007, 11, 01))
270
<< true << "Last-Modified"
271
<< "Thu, 01 Nov 2007 00:00:00 GMT";
272
QTest::newRow("Last-Modified-DateTime") << QNetworkRequest::LastModifiedHeader
273
<< QVariant(QDateTime(QDate(2007, 11, 01),
276
<< true << "Last-Modified"
277
<< "Thu, 01 Nov 2007 18:08:30 GMT";
279
QNetworkCookie cookie;
281
cookie.setValue("b");
282
QTest::newRow("Cookie-1") << QNetworkRequest::CookieHeader
283
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
286
QTest::newRow("SetCookie-1") << QNetworkRequest::SetCookieHeader
287
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
288
<< true << "Set-Cookie"
292
QTest::newRow("Cookie-2") << QNetworkRequest::CookieHeader
293
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
296
QTest::newRow("SetCookie-2") << QNetworkRequest::SetCookieHeader
297
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
298
<< true << "Set-Cookie"
301
QNetworkCookie cookie2;
302
cookie2.setName("c");
303
cookie2.setValue("d");
304
QTest::newRow("Cookie-3") << QNetworkRequest::CookieHeader
305
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie << cookie2)
308
QTest::newRow("SetCookie-3") << QNetworkRequest::SetCookieHeader
309
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie << cookie2)
310
<< true << "Set-Cookie"
311
<< "a=b; path=/, c=d";
314
void tst_QNetworkRequest::setHeader()
316
QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
317
QFETCH(QVariant, cookedValue);
318
QFETCH(bool, success);
319
QFETCH(QString, rawHeader);
320
QFETCH(QString, rawValue);
322
QNetworkRequest request;
323
request.setHeader(cookedHeader, cookedValue);
325
QCOMPARE(request.header(cookedHeader).isNull(), !success);
326
QCOMPARE(request.hasRawHeader(rawHeader.toLatin1()), success);
327
QCOMPARE(request.rawHeader(rawHeader.toLatin1()).isEmpty(), !success);
330
QCOMPARE(request.header(cookedHeader), cookedValue);
331
QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
335
void tst_QNetworkRequest::rawHeaderParsing_data()
337
QTest::addColumn<QNetworkRequest::KnownHeaders>("cookedHeader");
338
QTest::addColumn<QVariant>("cookedValue");
339
QTest::addColumn<bool>("success");
340
QTest::addColumn<QString>("rawHeader");
341
QTest::addColumn<QString>("rawValue");
343
QTest::newRow("Content-Type") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
345
<< "Content-Type" << "text/html";
346
QTest::newRow("Content-Length") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
347
<< true << "Content-Length" << " 1 ";
348
QTest::newRow("Location") << QNetworkRequest::LocationHeader
349
<< QVariant(QUrl("http://foo/with space"))
350
<< true << "Location" << "http://foo/with%20space";
351
QTest::newRow("Last-Modified-RFC1123") << QNetworkRequest::LastModifiedHeader
352
<< QVariant(QDateTime(QDate(1994, 11, 06),
355
<< true << "Last-Modified"
356
<< "Sun, 06 Nov 1994 08:49:37 GMT";
357
QTest::newRow("Last-Modified-RFC850") << QNetworkRequest::LastModifiedHeader
358
<< QVariant(QDateTime(QDate(1994, 11, 06),
361
<< true << "Last-Modified"
362
<< "Sunday, 06-Nov-94 08:49:37 GMT";
363
QTest::newRow("Last-Modified-asctime") << QNetworkRequest::LastModifiedHeader
364
<< QVariant(QDateTime(QDate(1994, 11, 06),
367
<< true << "Last-Modified"
368
<< "Sun Nov 6 08:49:37 1994";
370
QTest::newRow("Content-Length-invalid1") << QNetworkRequest::ContentLengthHeader << QVariant()
371
<< false << "Content-Length" << "1a";
372
QTest::newRow("Content-Length-invalid2") << QNetworkRequest::ContentLengthHeader << QVariant()
373
<< false << "Content-Length" << "a";
376
QTest::newRow("Location-invalid1") << QNetworkRequest::LocationHeader << QVariant() << false
377
<< "Location" << "abc";
378
QTest::newRow("Location-invalid2") << QNetworkRequest::LocationHeader << QVariant() << false
379
<< "Location" << "1http://foo";
380
QTest::newRow("Location-invalid3") << QNetworkRequest::LocationHeader << QVariant() << false
381
<< "Location" << "http://foo/%gg";
383
// don't test for invalid dates because we may want to support broken servers in the future
385
QNetworkCookie cookie;
387
cookie.setValue("b");
388
QTest::newRow("Cookie-1") << QNetworkRequest::CookieHeader
389
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
392
QTest::newRow("SetCookie-1") << QNetworkRequest::SetCookieHeader
393
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
394
<< true << "Set-Cookie"
398
QTest::newRow("SetCookie-2") << QNetworkRequest::SetCookieHeader
399
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie)
400
<< true << "Set-Cookie"
403
QNetworkCookie cookie2;
405
cookie2.setName("c");
406
cookie2.setValue("d");
407
QTest::newRow("Cookie-3") << QNetworkRequest::CookieHeader
408
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie << cookie2)
412
QTest::newRow("SetCookie-3") << QNetworkRequest::SetCookieHeader
413
<< QVariant::fromValue(QList<QNetworkCookie>() << cookie << cookie2)
414
<< true << "Set-Cookie"
415
<< "a=b; path=/\nc=d";
418
void tst_QNetworkRequest::rawHeaderParsing()
420
QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
421
QFETCH(QVariant, cookedValue);
422
QFETCH(bool, success);
423
QFETCH(QString, rawHeader);
424
QFETCH(QString, rawValue);
426
QNetworkRequest request;
427
request.setRawHeader(rawHeader.toLatin1(), rawValue.toLatin1());
429
// even if it doesn't parse, it's as a raw header
430
QVERIFY(request.hasRawHeader(rawHeader.toLatin1()));
431
QVERIFY(request.hasRawHeader(rawHeader.toLower().toLatin1()));
432
QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
434
QCOMPARE(request.header(cookedHeader).isNull(), !success);
435
if (cookedValue.type() != QVariant::UserType)
436
QCOMPARE(request.header(cookedHeader), cookedValue);
437
else if (cookedValue.userType() == qMetaTypeId<QList<QNetworkCookie> >())
438
QCOMPARE(qvariant_cast<QList<QNetworkCookie> >(request.header(cookedHeader)),
439
qvariant_cast<QList<QNetworkCookie> >(cookedValue));
442
void tst_QNetworkRequest::removeHeader()
444
QNetworkRequest request;
446
request.setRawHeader("Foo", "1");
447
QVERIFY(request.hasRawHeader("Foo"));
448
QVERIFY(request.hasRawHeader("foo"));
449
request.setRawHeader("Foo", QByteArray());
450
QVERIFY(!request.hasRawHeader("Foo"));
452
// same, but remove with different capitalisation
453
request.setRawHeader("Foo", "1");
454
QVERIFY(request.hasRawHeader("Foo"));
455
QVERIFY(request.hasRawHeader("foo"));
456
request.setRawHeader("foo", QByteArray());
457
QVERIFY(!request.hasRawHeader("Foo"));
459
// same, but not the first
460
request.setRawHeader("Bar", "2");
461
request.setRawHeader("Foo", "1");
462
QVERIFY(request.hasRawHeader("Foo"));
463
QVERIFY(request.hasRawHeader("foo"));
464
request.setRawHeader("foo", QByteArray());
465
QVERIFY(!request.hasRawHeader("Foo"));
466
QVERIFY(request.hasRawHeader("bar"));
468
// same, but not the first nor last
469
request.setRawHeader("Foo", "1");
470
request.setRawHeader("Bar", "3");
471
QVERIFY(request.hasRawHeader("Foo"));
472
QVERIFY(request.hasRawHeader("foo"));
473
request.setRawHeader("foo", QByteArray());
474
QVERIFY(!request.hasRawHeader("Foo"));
475
QVERIFY(request.hasRawHeader("bar"));
478
void tst_QNetworkRequest::originatingObject()
480
QNetworkRequest request;
482
QVERIFY(!request.originatingObject());
486
request.setOriginatingObject(&dummy);
487
QCOMPARE(request.originatingObject(), &dummy);
490
QVERIFY(!request.originatingObject());
493
QTEST_MAIN(tst_QNetworkRequest)
494
#include "tst_qnetworkrequest.moc"