1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtCore/QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkCookie>
Q_DECLARE_METATYPE(QList<QByteArray>)
Q_DECLARE_METATYPE(QNetworkRequest::KnownHeaders)
Q_DECLARE_METATYPE(QVariant)
class tst_QNetworkRequest: public QObject
{
Q_OBJECT
private slots:
void ctor_data();
void ctor();
void setUrl_data();
void setUrl();
void setRawHeader_data();
void setRawHeader();
void rawHeaderList_data();
void rawHeaderList();
void setHeader_data();
void setHeader();
void rawHeaderParsing_data();
void rawHeaderParsing();
void removeHeader();
};
QT_BEGIN_NAMESPACE
namespace QTest {
template<>
char *toString(const QNetworkCookie &cookie)
{
return qstrdup(cookie.toRawForm());
}
template<>
char *toString(const QList<QNetworkCookie> &list)
{
QString result = "QList(";
bool first = true;
foreach (QNetworkCookie cookie, list) {
if (!first)
result += ", ";
first = false;
result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm()));
}
return qstrdup(result.append(')').toLocal8Bit());
}
}
QT_END_NAMESPACE
void tst_QNetworkRequest::ctor_data()
{
QTest::addColumn<QUrl>("url");
QTest::newRow("nothing") << QUrl();
QTest::newRow("empty") << QUrl();
QTest::newRow("http") << QUrl("http://www.trolltech.com");
}
void tst_QNetworkRequest::ctor()
{
QFETCH(QUrl, url);
if (qstrcmp(QTest::currentDataTag(), "nothing") == 0) {
QNetworkRequest request;
QCOMPARE(request.url(), url);
} else {
QNetworkRequest request(url);
QCOMPARE(request.url(), url);
}
}
void tst_QNetworkRequest::setUrl_data()
{
ctor_data();
}
void tst_QNetworkRequest::setUrl()
{
QFETCH(QUrl, url);
QNetworkRequest request;
if (qstrcmp(QTest::currentDataTag(), "nothing") != 0)
request.setUrl(url);
QCOMPARE(request.url(), url);
}
void tst_QNetworkRequest::setRawHeader_data()
{
QTest::addColumn<QByteArray>("header");
QTest::addColumn<QByteArray>("value");
QTest::addColumn<QByteArray>("headerToGet");
QTest::addColumn<QByteArray>("expectedValue");
QTest::addColumn<bool>("hasHeader");
QTest::newRow("null-header") << QByteArray() << QByteArray("abc")
<< QByteArray() << QByteArray() << false;
QTest::newRow("empty-header") << QByteArray("") << QByteArray("abc")
<< QByteArray("") << QByteArray() << false;
QTest::newRow("null-value") << QByteArray("foo") << QByteArray()
<< QByteArray("foo") << QByteArray() << false;
QTest::newRow("empty-value") << QByteArray("foo") << QByteArray("")
<< QByteArray("foo") << QByteArray("") << true;
QTest::newRow("empty-value-vs-null") << QByteArray("foo") << QByteArray("")
<< QByteArray("foo") << QByteArray() << true;
QTest::newRow("UPPER-UPPER") << QByteArray("FOO") << QByteArray("abc")
<< QByteArray("FOO") << QByteArray("abc") << true;
QTest::newRow("UPPER-Mixed") << QByteArray("FOO") << QByteArray("abc")
<< QByteArray("Foo") << QByteArray("abc") << true;
QTest::newRow("UPPER-lower") << QByteArray("FOO") << QByteArray("abc")
<< QByteArray("foo") << QByteArray("abc") << true;
QTest::newRow("Mixed-UPPER") << QByteArray("Foo") << QByteArray("abc")
<< QByteArray("FOO") << QByteArray("abc") << true;
QTest::newRow("Mixed-Mixed") << QByteArray("Foo") << QByteArray("abc")
<< QByteArray("Foo") << QByteArray("abc") << true;
QTest::newRow("Mixed-lower") << QByteArray("Foo") << QByteArray("abc")
<< QByteArray("foo") << QByteArray("abc") << true;
QTest::newRow("lower-UPPER") << QByteArray("foo") << QByteArray("abc")
<< QByteArray("FOO") << QByteArray("abc") << true;
QTest::newRow("lower-Mixed") << QByteArray("foo") << QByteArray("abc")
<< QByteArray("Foo") << QByteArray("abc") << true;
QTest::newRow("lower-lower") << QByteArray("foo") << QByteArray("abc")
<< QByteArray("foo") << QByteArray("abc") << true;
}
void tst_QNetworkRequest::setRawHeader()
{
QFETCH(QByteArray, header);
QFETCH(QByteArray, value);
QFETCH(QByteArray, headerToGet);
QFETCH(QByteArray, expectedValue);
QFETCH(bool, hasHeader);
QNetworkRequest request;
request.setRawHeader(header, value);
QCOMPARE(request.hasRawHeader(headerToGet), hasHeader);
QCOMPARE(request.rawHeader(headerToGet), expectedValue);
}
void tst_QNetworkRequest::rawHeaderList_data()
{
QTest::addColumn<QList<QByteArray> >("set");
QTest::addColumn<QList<QByteArray> >("expected");
QTest::newRow("empty") << QList<QByteArray>() << QList<QByteArray>();
QList<QByteArray> set;
QList<QByteArray> expected;
set << "foo";
expected = set;
QTest::newRow("one") << set << expected;
set << "bar";
expected = set;
QTest::newRow("two") << set << expected;
set.clear();
expected.clear();
set << "foo" << "foo";
expected << "foo";
QTest::newRow("repeated") << set << expected;
set.clear();
expected.clear();
set << "foo" << "bar" << "foo";
expected << "bar" << "foo";
QTest::newRow("repeated-interleaved") << set << expected;
}
void tst_QNetworkRequest::rawHeaderList()
{
QFETCH(QList<QByteArray>, set);
QFETCH(QList<QByteArray>, expected);
QNetworkRequest request;
foreach (QByteArray header, set)
request.setRawHeader(header, "a value");
QList<QByteArray> got = request.rawHeaderList();
QCOMPARE(got.size(), expected.size());
for (int i = 0; i < got.size(); ++i)
QCOMPARE(got.at(i), expected.at(i));
}
void tst_QNetworkRequest::setHeader_data()
{
QTest::addColumn<QNetworkRequest::KnownHeaders>("cookedHeader");
QTest::addColumn<QVariant>("cookedValue");
QTest::addColumn<bool>("success");
QTest::addColumn<QString>("rawHeader");
QTest::addColumn<QString>("rawValue");
QTest::newRow("Content-Type-Null") << QNetworkRequest::ContentTypeHeader << QVariant()
<< false << "Content-Type" << "";
QTest::newRow("Content-Type-String") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
<< true
<< "Content-Type" << "text/html";
QTest::newRow("Content-Type-ByteArray") << QNetworkRequest::ContentTypeHeader
<< QVariant("text/html") << true
<< "Content-Type" << "text/html";
QTest::newRow("Content-Length-Int") << QNetworkRequest::ContentLengthHeader << QVariant(1)
<< true << "Content-Length" << "1";
QTest::newRow("Content-Length-Int64") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
<< true << "Content-Length" << "1";
QTest::newRow("Location-String") << QNetworkRequest::LocationHeader << QVariant("http://foo/with space")
<< true << "Location" << "http://foo/with space";
QTest::newRow("Location-ByteArray") << QNetworkRequest::LocationHeader
<< QVariant("http://foo/with space")
<< true << "Location" << "http://foo/with space";
QTest::newRow("Location-Url") << QNetworkRequest::LocationHeader
<< QVariant(QUrl("http://foo/with space"))
<< true << "Location" << "http://foo/with%20space";
QTest::newRow("Last-Modified-Date") << QNetworkRequest::LastModifiedHeader
<< QVariant(QDate(2007, 11, 01))
<< true << "Last-Modified"
<< "Thu, 01 Nov 2007 00:00:00 GMT";
QTest::newRow("Last-Modified-DateTime") << QNetworkRequest::LastModifiedHeader
<< QVariant(QDateTime(QDate(2007, 11, 01),
QTime(18, 8, 30),
Qt::UTC))
<< true << "Last-Modified"
<< "Thu, 01 Nov 2007 18:08:30 GMT";
QNetworkCookie cookie;
cookie.setName("a");
cookie.setValue("b");
QTest::newRow("Cookie-1") << QNetworkRequest::CookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Cookie"
<< "a=b";
QTest::newRow("SetCookie-1") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Set-Cookie"
<< "a=b";
cookie.setPath("/");
QTest::newRow("Cookie-2") << QNetworkRequest::CookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Cookie"
<< "a=b";
QTest::newRow("SetCookie-2") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Set-Cookie"
<< "a=b; path=/";
QNetworkCookie cookie2;
cookie2.setName("c");
cookie2.setValue("d");
QTest::newRow("Cookie-3") << QNetworkRequest::CookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie << cookie2)
<< true << "Cookie"
<< "a=b; c=d";
QTest::newRow("SetCookie-3") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie << cookie2)
<< true << "Set-Cookie"
<< "a=b; path=/, c=d";
}
void tst_QNetworkRequest::setHeader()
{
QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
QFETCH(QVariant, cookedValue);
QFETCH(bool, success);
QFETCH(QString, rawHeader);
QFETCH(QString, rawValue);
QNetworkRequest request;
request.setHeader(cookedHeader, cookedValue);
QCOMPARE(request.header(cookedHeader).isNull(), !success);
QCOMPARE(request.hasRawHeader(rawHeader.toLatin1()), success);
QCOMPARE(request.rawHeader(rawHeader.toLatin1()).isEmpty(), !success);
if (success) {
QCOMPARE(request.header(cookedHeader), cookedValue);
QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
}
}
void tst_QNetworkRequest::rawHeaderParsing_data()
{
QTest::addColumn<QNetworkRequest::KnownHeaders>("cookedHeader");
QTest::addColumn<QVariant>("cookedValue");
QTest::addColumn<bool>("success");
QTest::addColumn<QString>("rawHeader");
QTest::addColumn<QString>("rawValue");
QTest::newRow("Content-Type") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
<< true
<< "Content-Type" << "text/html";
QTest::newRow("Content-Length") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
<< true << "Content-Length" << " 1 ";
QTest::newRow("Location") << QNetworkRequest::LocationHeader
<< QVariant(QUrl("http://foo/with space"))
<< true << "Location" << "http://foo/with%20space";
QTest::newRow("Last-Modified-RFC1123") << QNetworkRequest::LastModifiedHeader
<< QVariant(QDateTime(QDate(1994, 11, 06),
QTime(8, 49, 37),
Qt::UTC))
<< true << "Last-Modified"
<< "Sun, 06 Nov 1994 08:49:37 GMT";
QTest::newRow("Last-Modified-RFC850") << QNetworkRequest::LastModifiedHeader
<< QVariant(QDateTime(QDate(1994, 11, 06),
QTime(8, 49, 37),
Qt::UTC))
<< true << "Last-Modified"
<< "Sunday, 06-Nov-94 08:49:37 GMT";
QTest::newRow("Last-Modified-asctime") << QNetworkRequest::LastModifiedHeader
<< QVariant(QDateTime(QDate(1994, 11, 06),
QTime(8, 49, 37),
Qt::UTC))
<< true << "Last-Modified"
<< "Sun Nov 6 08:49:37 1994";
QTest::newRow("Content-Length-invalid1") << QNetworkRequest::ContentLengthHeader << QVariant()
<< false << "Content-Length" << "1a";
QTest::newRow("Content-Length-invalid2") << QNetworkRequest::ContentLengthHeader << QVariant()
<< false << "Content-Length" << "a";
QTest::newRow("Location-invalid1") << QNetworkRequest::LocationHeader << QVariant() << false
<< "Location" << "abc";
QTest::newRow("Location-invalid2") << QNetworkRequest::LocationHeader << QVariant() << false
<< "Location" << "1http://foo";
QTest::newRow("Location-invalid3") << QNetworkRequest::LocationHeader << QVariant() << false
<< "Location" << "http://foo/%gg";
// don't test for invalid dates because we may want to support broken servers in the future
QNetworkCookie cookie;
cookie.setName("a");
cookie.setValue("b");
QTest::newRow("Cookie-1") << QNetworkRequest::CookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Cookie"
<< "a=b";
QTest::newRow("SetCookie-1") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Set-Cookie"
<< "a=b";
cookie.setPath("/");
QTest::newRow("SetCookie-2") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie)
<< true << "Set-Cookie"
<< "a=b; path=/";
QNetworkCookie cookie2;
cookie.setPath("");
cookie2.setName("c");
cookie2.setValue("d");
QTest::newRow("Cookie-3") << QNetworkRequest::CookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie << cookie2)
<< true << "Cookie"
<< "a=b; c=d";
cookie.setPath("/");
QTest::newRow("SetCookie-3") << QNetworkRequest::SetCookieHeader
<< qVariantFromValue(QList<QNetworkCookie>() << cookie << cookie2)
<< true << "Set-Cookie"
<< "a=b; path=/, c=d";
}
void tst_QNetworkRequest::rawHeaderParsing()
{
QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
QFETCH(QVariant, cookedValue);
QFETCH(bool, success);
QFETCH(QString, rawHeader);
QFETCH(QString, rawValue);
QNetworkRequest request;
request.setRawHeader(rawHeader.toLatin1(), rawValue.toLatin1());
// even if it doesn't parse, it's as a raw header
QVERIFY(request.hasRawHeader(rawHeader.toLatin1()));
QVERIFY(request.hasRawHeader(rawHeader.toLower().toLatin1()));
QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
QCOMPARE(request.header(cookedHeader).isNull(), !success);
if (cookedValue.type() != QVariant::UserType)
QCOMPARE(request.header(cookedHeader), cookedValue);
else if (cookedValue.userType() == qMetaTypeId<QList<QNetworkCookie> >())
QCOMPARE(qvariant_cast<QList<QNetworkCookie> >(request.header(cookedHeader)),
qvariant_cast<QList<QNetworkCookie> >(cookedValue));
}
void tst_QNetworkRequest::removeHeader()
{
QNetworkRequest request;
request.setRawHeader("Foo", "1");
QVERIFY(request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("foo"));
request.setRawHeader("Foo", QByteArray());
QVERIFY(!request.hasRawHeader("Foo"));
// same, but remove with different capitalisation
request.setRawHeader("Foo", "1");
QVERIFY(request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("foo"));
request.setRawHeader("foo", QByteArray());
QVERIFY(!request.hasRawHeader("Foo"));
// same, but not the first
request.setRawHeader("Bar", "2");
request.setRawHeader("Foo", "1");
QVERIFY(request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("foo"));
request.setRawHeader("foo", QByteArray());
QVERIFY(!request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("bar"));
// same, but not the first nor last
request.setRawHeader("Foo", "1");
request.setRawHeader("Bar", "3");
QVERIFY(request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("foo"));
request.setRawHeader("foo", QByteArray());
QVERIFY(!request.hasRawHeader("Foo"));
QVERIFY(request.hasRawHeader("bar"));
}
QTEST_MAIN(tst_QNetworkRequest)
#include "tst_qnetworkrequest.moc"
|