1/****************************************************************************
2**
3** Copyright (C) 2022 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QTest>
30#include <QtCore/QUrl>
31#if QT_CONFIG(timezone)
32# include <QtCore/QTimeZone>
33#endif
34#include <QtNetwork/QNetworkRequest>
35#include <QtNetwork/QNetworkCookie>
36
37Q_DECLARE_METATYPE(QNetworkRequest::KnownHeaders)
38
39class tst_QNetworkRequest: public QObject
40{
41 Q_OBJECT
42
43private slots:
44 void ctor_data();
45 void ctor();
46 void implicitDefaultCtor();
47 void setUrl_data();
48 void setUrl();
49 void setRawHeader_data();
50 void setRawHeader();
51 void rawHeaderList_data();
52 void rawHeaderList();
53 void setHeader_data();
54 void setHeader();
55 void rawHeaderParsing_data();
56 void rawHeaderParsing();
57 void originatingObject();
58
59 void removeHeader();
60};
61
62void tst_QNetworkRequest::ctor_data()
63{
64 QTest::addColumn<QUrl>(name: "url");
65
66 QTest::newRow(dataTag: "nothing") << QUrl();
67 QTest::newRow(dataTag: "empty") << QUrl();
68 QTest::newRow(dataTag: "http") << QUrl("http://qt-project.org");
69}
70
71void tst_QNetworkRequest::ctor()
72{
73 QFETCH(QUrl, url);
74
75 if (qstrcmp(str1: QTest::currentDataTag(), str2: "nothing") == 0) {
76 QNetworkRequest request;
77 QCOMPARE(request.url(), url);
78 } else {
79 QNetworkRequest request(url);
80 QCOMPARE(request.url(), url);
81 }
82}
83
84void tst_QNetworkRequest::implicitDefaultCtor()
85{
86 QNetworkRequest r = {};
87 Q_UNUSED(r);
88}
89
90void tst_QNetworkRequest::setUrl_data()
91{
92 ctor_data();
93}
94
95void tst_QNetworkRequest::setUrl()
96{
97 QFETCH(QUrl, url);
98 QNetworkRequest request;
99
100 if (qstrcmp(str1: QTest::currentDataTag(), str2: "nothing") != 0)
101 request.setUrl(url);
102
103 QCOMPARE(request.url(), url);
104}
105
106void tst_QNetworkRequest::setRawHeader_data()
107{
108 QTest::addColumn<QByteArray>(name: "header");
109 QTest::addColumn<QByteArray>(name: "value");
110 QTest::addColumn<QByteArray>(name: "headerToGet");
111 QTest::addColumn<QByteArray>(name: "expectedValue");
112 QTest::addColumn<bool>(name: "hasHeader");
113
114 QTest::newRow(dataTag: "null-header") << QByteArray() << QByteArray("abc")
115 << QByteArray() << QByteArray() << false;
116 QTest::newRow(dataTag: "empty-header") << QByteArray("") << QByteArray("abc")
117 << QByteArray("") << QByteArray() << false;
118 QTest::newRow(dataTag: "null-value") << QByteArray("foo") << QByteArray()
119 << QByteArray("foo") << QByteArray() << false;
120 QTest::newRow(dataTag: "empty-value") << QByteArray("foo") << QByteArray("")
121 << QByteArray("foo") << QByteArray("") << true;
122 QTest::newRow(dataTag: "empty-value-vs-null") << QByteArray("foo") << QByteArray("")
123 << QByteArray("foo") << QByteArray() << true;
124
125 QTest::newRow(dataTag: "UPPER-UPPER") << QByteArray("FOO") << QByteArray("abc")
126 << QByteArray("FOO") << QByteArray("abc") << true;
127 QTest::newRow(dataTag: "UPPER-Mixed") << QByteArray("FOO") << QByteArray("abc")
128 << QByteArray("Foo") << QByteArray("abc") << true;
129 QTest::newRow(dataTag: "UPPER-lower") << QByteArray("FOO") << QByteArray("abc")
130 << QByteArray("foo") << QByteArray("abc") << true;
131 QTest::newRow(dataTag: "Mixed-UPPER") << QByteArray("Foo") << QByteArray("abc")
132 << QByteArray("FOO") << QByteArray("abc") << true;
133 QTest::newRow(dataTag: "Mixed-Mixed") << QByteArray("Foo") << QByteArray("abc")
134 << QByteArray("Foo") << QByteArray("abc") << true;
135 QTest::newRow(dataTag: "Mixed-lower") << QByteArray("Foo") << QByteArray("abc")
136 << QByteArray("foo") << QByteArray("abc") << true;
137 QTest::newRow(dataTag: "lower-UPPER") << QByteArray("foo") << QByteArray("abc")
138 << QByteArray("FOO") << QByteArray("abc") << true;
139 QTest::newRow(dataTag: "lower-Mixed") << QByteArray("foo") << QByteArray("abc")
140 << QByteArray("Foo") << QByteArray("abc") << true;
141 QTest::newRow(dataTag: "lower-lower") << QByteArray("foo") << QByteArray("abc")
142 << QByteArray("foo") << QByteArray("abc") << true;
143}
144
145void tst_QNetworkRequest::setRawHeader()
146{
147 QFETCH(QByteArray, header);
148 QFETCH(QByteArray, value);
149 QFETCH(QByteArray, headerToGet);
150 QFETCH(QByteArray, expectedValue);
151 QFETCH(bool, hasHeader);
152
153 QNetworkRequest request;
154 request.setRawHeader(headerName: header, value);
155
156 QCOMPARE(request.hasRawHeader(headerToGet), hasHeader);
157 QCOMPARE(request.rawHeader(headerToGet), expectedValue);
158}
159
160void tst_QNetworkRequest::rawHeaderList_data()
161{
162 QTest::addColumn<QList<QByteArray> >(name: "set");
163 QTest::addColumn<QList<QByteArray> >(name: "expected");
164
165 QTest::newRow(dataTag: "empty") << QList<QByteArray>() << QList<QByteArray>();
166
167 QList<QByteArray> set;
168 QList<QByteArray> expected;
169
170 set << "foo";
171 expected = set;
172 QTest::newRow(dataTag: "one") << set << expected;
173
174 set << "bar";
175 expected = set;
176 QTest::newRow(dataTag: "two") << set << expected;
177
178 set.clear();
179 expected.clear();
180 set << "foo" << "foo";
181 expected << "foo";
182 QTest::newRow(dataTag: "repeated") << set << expected;
183
184 set.clear();
185 expected.clear();
186 set << "foo" << "bar" << "foo";
187 expected << "bar" << "foo";
188 QTest::newRow(dataTag: "repeated-interleaved") << set << expected;
189}
190
191void tst_QNetworkRequest::rawHeaderList()
192{
193 QFETCH(QList<QByteArray>, set);
194 QFETCH(QList<QByteArray>, expected);
195
196 QNetworkRequest request;
197 foreach (QByteArray header, set)
198 request.setRawHeader(headerName: header, value: "a value");
199
200 QList<QByteArray> got = request.rawHeaderList();
201 QCOMPARE(got.size(), expected.size());
202 for (int i = 0; i < got.size(); ++i)
203 QCOMPARE(got.at(i), expected.at(i));
204}
205
206void tst_QNetworkRequest::setHeader_data()
207{
208 QTest::addColumn<QNetworkRequest::KnownHeaders>(name: "cookedHeader");
209 QTest::addColumn<QVariant>(name: "cookedValue");
210 QTest::addColumn<bool>(name: "success");
211 QTest::addColumn<QString>(name: "rawHeader");
212 QTest::addColumn<QString>(name: "rawValue");
213
214 QTest::newRow(dataTag: "Content-Type-Null") << QNetworkRequest::ContentTypeHeader << QVariant()
215 << false << "Content-Type" << "";
216 QTest::newRow(dataTag: "Content-Type-String") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
217 << true
218 << "Content-Type" << "text/html";
219 QTest::newRow(dataTag: "Content-Type-ByteArray") << QNetworkRequest::ContentTypeHeader
220 << QVariant("text/html") << true
221 << "Content-Type" << "text/html";
222
223 QTest::newRow(dataTag: "Content-Length-Int") << QNetworkRequest::ContentLengthHeader << QVariant(1)
224 << true << "Content-Length" << "1";
225 QTest::newRow(dataTag: "Content-Length-Int64") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
226 << true << "Content-Length" << "1";
227
228 QTest::newRow(dataTag: "Location-String") << QNetworkRequest::LocationHeader << QVariant("http://foo/with space")
229 << true << "Location" << "http://foo/with space";
230 QTest::newRow(dataTag: "Location-ByteArray") << QNetworkRequest::LocationHeader
231 << QVariant("http://foo/with space")
232 << true << "Location" << "http://foo/with space";
233 QTest::newRow(dataTag: "Location-Url") << QNetworkRequest::LocationHeader
234 << QVariant(QUrl("http://foo/with space"))
235 << true << "Location" << "http://foo/with%20space";
236
237 QTest::newRow(dataTag: "Last-Modified-Date") << QNetworkRequest::LastModifiedHeader
238 << QVariant(QDate(2007, 11, 01))
239 << true << "Last-Modified"
240 << "Thu, 01 Nov 2007 00:00:00 GMT";
241 QTest::newRow(dataTag: "Last-Modified-DateTime-UTC")
242 << QNetworkRequest::LastModifiedHeader
243 << QVariant(QDateTime(QDate(2007, 11, 1), QTime(18, 8, 30), Qt::UTC))
244 << true << "Last-Modified" << "Thu, 01 Nov 2007 18:08:30 GMT";
245 // QTBUG-80666: format dates correctly (as GMT) even if the date passed in isn't in UTC:
246 QTest::newRow(dataTag: "Last-Modified-DateTime-Local")
247 << QNetworkRequest::LastModifiedHeader
248 << QVariant(QDateTime(QDate(2007, 11, 1), QTime(18, 8, 30), Qt::UTC).toLocalTime())
249 << true << "Last-Modified" << "Thu, 01 Nov 2007 18:08:30 GMT";
250 QTest::newRow(dataTag: "Last-Modified-DateTime-Offset")
251 << QNetworkRequest::LastModifiedHeader
252 << QVariant(QDateTime(QDate(2007, 11, 1), QTime(18, 8, 30), Qt::UTC).toOffsetFromUtc(offsetSeconds: 3600))
253 << true << "Last-Modified" << "Thu, 01 Nov 2007 18:08:30 GMT";
254#if QT_CONFIG(timezone)
255 QTimeZone cet("Europe/Oslo");
256 if (cet.isValid()) {
257 QTest::newRow(dataTag: "Last-Modified-DateTime-CET")
258 << QNetworkRequest::LastModifiedHeader
259 << QVariant(QDateTime(QDate(2007, 11, 1), QTime(18, 8, 30), Qt::UTC).toTimeZone(toZone: cet))
260 << true << "Last-Modified" << "Thu, 01 Nov 2007 18:08:30 GMT";
261 }
262#endif
263
264 QTest::newRow(dataTag: "If-Modified-Since-Date") << QNetworkRequest::IfModifiedSinceHeader
265 << QVariant(QDate(2017, 7, 01))
266 << true << "If-Modified-Since"
267 << "Sat, 01 Jul 2017 00:00:00 GMT";
268 QTest::newRow(dataTag: "If-Modified-Since-DateTime") << QNetworkRequest::IfModifiedSinceHeader
269 << QVariant(QDateTime(QDate(2017, 7, 01),
270 QTime(3, 14, 15),
271 Qt::UTC))
272 << true << "If-Modified-Since"
273 << "Sat, 01 Jul 2017 03:14:15 GMT";
274
275 QTest::newRow(dataTag: "Etag-strong") << QNetworkRequest::ETagHeader << QVariant(R"("xyzzy")")
276 << true << "ETag" << R"("xyzzy")";
277 QTest::newRow(dataTag: "Etag-weak") << QNetworkRequest::ETagHeader << QVariant(R"(W/"xyzzy")")
278 << true << "ETag" << R"(W/"xyzzy")";
279 QTest::newRow(dataTag: "Etag-empty") << QNetworkRequest::ETagHeader << QVariant(R"("")")
280 << true << "ETag" << R"("")";
281
282 QTest::newRow(dataTag: "If-Match-empty") << QNetworkRequest::IfMatchHeader << QVariant(R"("")")
283 << true << "If-Match" << R"("")";
284 QTest::newRow(dataTag: "If-Match-any") << QNetworkRequest::IfMatchHeader << QVariant(R"("*")")
285 << true << "If-Match" << R"("*")";
286 QTest::newRow(dataTag: "If-Match-single") << QNetworkRequest::IfMatchHeader << QVariant(R"("xyzzy")")
287 << true << "If-Match" << R"("xyzzy")";
288 QTest::newRow(dataTag: "If-Match-multiple") << QNetworkRequest::IfMatchHeader
289 << QVariant(R"("xyzzy", "r2d2xxxx", "c3piozzzz")")
290 << true << "If-Match"
291 << R"("xyzzy", "r2d2xxxx", "c3piozzzz")";
292
293 QTest::newRow(dataTag: "If-None-Match-empty") << QNetworkRequest::IfNoneMatchHeader << QVariant(R"("")")
294 << true << "If-None-Match" << R"("")";
295 QTest::newRow(dataTag: "If-None-Match-any") << QNetworkRequest::IfNoneMatchHeader << QVariant(R"("*")")
296 << true << "If-None-Match" << R"("*")";
297 QTest::newRow(dataTag: "If-None-Match-single") << QNetworkRequest::IfNoneMatchHeader << QVariant(R"("xyzzy")")
298 << true << "If-None-Match" << R"("xyzzy")";
299 QTest::newRow(dataTag: "If-None-Match-multiple") << QNetworkRequest::IfNoneMatchHeader
300 << QVariant(R"("xyzzy", W/"r2d2xxxx", "c3piozzzz")")
301 << true << "If-None-Match"
302 << R"("xyzzy", W/"r2d2xxxx", "c3piozzzz")";
303
304 QNetworkCookie cookie;
305 cookie.setName("a");
306 cookie.setValue("b");
307 QTest::newRow(dataTag: "Cookie-1") << QNetworkRequest::CookieHeader
308 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
309 << true << "Cookie"
310 << "a=b";
311 QTest::newRow(dataTag: "SetCookie-1") << QNetworkRequest::SetCookieHeader
312 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
313 << true << "Set-Cookie"
314 << "a=b";
315
316 cookie.setPath("/");
317 QTest::newRow(dataTag: "Cookie-2") << QNetworkRequest::CookieHeader
318 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
319 << true << "Cookie"
320 << "a=b";
321 QTest::newRow(dataTag: "SetCookie-2") << QNetworkRequest::SetCookieHeader
322 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
323 << true << "Set-Cookie"
324 << "a=b; path=/";
325
326 QNetworkCookie cookie2;
327 cookie2.setName("c");
328 cookie2.setValue("d");
329 QTest::newRow(dataTag: "Cookie-3") << QNetworkRequest::CookieHeader
330 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie << cookie2)
331 << true << "Cookie"
332 << "a=b; c=d";
333 QTest::newRow(dataTag: "SetCookie-3") << QNetworkRequest::SetCookieHeader
334 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie << cookie2)
335 << true << "Set-Cookie"
336 << "a=b; path=/, c=d";
337}
338
339void tst_QNetworkRequest::setHeader()
340{
341 QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
342 QFETCH(QVariant, cookedValue);
343 QFETCH(bool, success);
344 QFETCH(QString, rawHeader);
345 QFETCH(QString, rawValue);
346
347 QNetworkRequest request;
348 request.setHeader(header: cookedHeader, value: cookedValue);
349
350 QCOMPARE(request.header(cookedHeader).isNull(), !success);
351 QCOMPARE(request.hasRawHeader(rawHeader.toLatin1()), success);
352 QCOMPARE(request.rawHeader(rawHeader.toLatin1()).isEmpty(), !success);
353
354 if (success) {
355 QCOMPARE(request.header(cookedHeader), cookedValue);
356 QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
357 }
358}
359
360void tst_QNetworkRequest::rawHeaderParsing_data()
361{
362 QTest::addColumn<QNetworkRequest::KnownHeaders>(name: "cookedHeader");
363 QTest::addColumn<QVariant>(name: "cookedValue");
364 QTest::addColumn<bool>(name: "success");
365 QTest::addColumn<QString>(name: "rawHeader");
366 QTest::addColumn<QString>(name: "rawValue");
367
368 QTest::newRow(dataTag: "Content-Type") << QNetworkRequest::ContentTypeHeader << QVariant("text/html")
369 << true
370 << "Content-Type" << "text/html";
371 QTest::newRow(dataTag: "Content-Length") << QNetworkRequest::ContentLengthHeader << QVariant(qint64(1))
372 << true << "Content-Length" << " 1 ";
373 QTest::newRow(dataTag: "Location") << QNetworkRequest::LocationHeader
374 << QVariant(QUrl("http://foo/with space"))
375 << true << "Location" << "http://foo/with%20space";
376 QTest::newRow(dataTag: "Last-Modified-RFC1123") << QNetworkRequest::LastModifiedHeader
377 << QVariant(QDateTime(QDate(1994, 11, 06),
378 QTime(8, 49, 37),
379 Qt::UTC))
380 << true << "Last-Modified"
381 << "Sun, 06 Nov 1994 08:49:37 GMT";
382 QTest::newRow(dataTag: "Last-Modified-RFC850") << QNetworkRequest::LastModifiedHeader
383 << QVariant(QDateTime(QDate(1994, 11, 06),
384 QTime(8, 49, 37),
385 Qt::UTC))
386 << true << "Last-Modified"
387 << "Sunday, 06-Nov-94 08:49:37 GMT";
388 QTest::newRow(dataTag: "Last-Modified-asctime") << QNetworkRequest::LastModifiedHeader
389 << QVariant(QDateTime(QDate(1994, 11, 06),
390 QTime(8, 49, 37),
391 Qt::UTC))
392 << true << "Last-Modified"
393 << "Sun Nov 6 08:49:37 1994";
394
395 QTest::newRow(dataTag: "If-Modified-Since-RFC1123") << QNetworkRequest::IfModifiedSinceHeader
396 << QVariant(QDateTime(QDate(1994, 8, 06),
397 QTime(8, 49, 37),
398 Qt::UTC))
399 << true << "If-Modified-Since"
400 << "Sun, 06 Aug 1994 08:49:37 GMT";
401 QTest::newRow(dataTag: "If-Modified-Since-RFC850") << QNetworkRequest::IfModifiedSinceHeader
402 << QVariant(QDateTime(QDate(1994, 8, 06),
403 QTime(8, 49, 37),
404 Qt::UTC))
405 << true << "If-Modified-Since"
406 << "Sunday, 06-Aug-94 08:49:37 GMT";
407 QTest::newRow(dataTag: "If-Modified-Since-asctime") << QNetworkRequest::IfModifiedSinceHeader
408 << QVariant(QDateTime(QDate(1994, 8, 06),
409 QTime(8, 49, 37),
410 Qt::UTC))
411 << true << "If-Modified-Since"
412 << "Sun Aug 6 08:49:37 1994";
413
414 QTest::newRow(dataTag: "Etag-strong") << QNetworkRequest::ETagHeader << QVariant(R"("xyzzy")")
415 << true << "ETag" << R"("xyzzy")";
416 QTest::newRow(dataTag: "Etag-weak") << QNetworkRequest::ETagHeader << QVariant(R"(W/"xyzzy")")
417 << true << "ETag" << R"(W/"xyzzy")";
418 QTest::newRow(dataTag: "Etag-empty") << QNetworkRequest::ETagHeader << QVariant(R"("")")
419 << true << "ETag" << R"("")";
420
421 QTest::newRow(dataTag: "If-Match-empty") << QNetworkRequest::IfMatchHeader << QVariant(QStringList(R"("")"))
422 << true << "If-Match" << R"("")";
423 QTest::newRow(dataTag: "If-Match-any") << QNetworkRequest::IfMatchHeader << QVariant(QStringList(R"("*")"))
424 << true << "If-Match" << R"("*")";
425 QTest::newRow(dataTag: "If-Match-single") << QNetworkRequest::IfMatchHeader
426 << QVariant(QStringList(R"("xyzzy")"))
427 << true << "If-Match" << R"("xyzzy")";
428 QTest::newRow(dataTag: "If-Match-multiple") << QNetworkRequest::IfMatchHeader
429 << QVariant(QStringList({R"("xyzzy")",
430 R"("r2d2xxxx")",
431 R"("c3piozzzz")"}))
432 << true << "If-Match"
433 << R"("xyzzy", "r2d2xxxx", "c3piozzzz")";
434
435 QTest::newRow(dataTag: "If-None-Match-empty") << QNetworkRequest::IfNoneMatchHeader
436 << QVariant(QStringList(R"("")"))
437 << true << "If-None-Match" << R"("")";
438 QTest::newRow(dataTag: "If-None-Match-any") << QNetworkRequest::IfNoneMatchHeader
439 << QVariant(QStringList(R"("*")"))
440 << true << "If-None-Match" << R"("*")";
441 QTest::newRow(dataTag: "If-None-Match-single") << QNetworkRequest::IfNoneMatchHeader
442 << QVariant(QStringList(R"("xyzzy")"))
443 << true << "If-None-Match" << R"("xyzzy")";
444 QTest::newRow(dataTag: "If-None-Match-multiple") << QNetworkRequest::IfNoneMatchHeader
445 << QVariant(QStringList({R"("xyzzy")",
446 R"(W/"r2d2xxxx")",
447 R"("c3piozzzz")"}))
448 << true << "If-None-Match"
449 << R"("xyzzy", W/"r2d2xxxx", "c3piozzzz")";
450
451 QTest::newRow(dataTag: "Content-Length-invalid1") << QNetworkRequest::ContentLengthHeader << QVariant()
452 << false << "Content-Length" << "1a";
453 QTest::newRow(dataTag: "Content-Length-invalid2") << QNetworkRequest::ContentLengthHeader << QVariant()
454 << false << "Content-Length" << "a";
455
456
457 QTest::newRow(dataTag: "Location-invalid1") << QNetworkRequest::LocationHeader << QVariant() << false
458 << "Location" << "abc";
459 QTest::newRow(dataTag: "Location-invalid2") << QNetworkRequest::LocationHeader << QVariant() << false
460 << "Location" << "1http://foo";
461 QTest::newRow(dataTag: "Location-invalid3") << QNetworkRequest::LocationHeader << QVariant() << false
462 << "Location" << "http://foo/%gg";
463
464 // don't test for invalid dates because we may want to support broken servers in the future
465
466 QNetworkCookie cookie;
467 cookie.setName("a");
468 cookie.setValue("b");
469 QTest::newRow(dataTag: "Cookie-1") << QNetworkRequest::CookieHeader
470 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
471 << true << "Cookie"
472 << "a=b";
473 QTest::newRow(dataTag: "SetCookie-1") << QNetworkRequest::SetCookieHeader
474 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
475 << true << "Set-Cookie"
476 << "a=b";
477
478 cookie.setPath("/");
479 QTest::newRow(dataTag: "SetCookie-2") << QNetworkRequest::SetCookieHeader
480 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie)
481 << true << "Set-Cookie"
482 << "a=b; path=/";
483
484 QNetworkCookie cookie2;
485 cookie.setPath("");
486 cookie2.setName("c");
487 cookie2.setValue("d");
488 QTest::newRow(dataTag: "Cookie-3") << QNetworkRequest::CookieHeader
489 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie << cookie2)
490 << true << "Cookie"
491 << "a=b; c=d";
492 cookie.setPath("/");
493 QTest::newRow(dataTag: "SetCookie-3") << QNetworkRequest::SetCookieHeader
494 << QVariant::fromValue(value: QList<QNetworkCookie>() << cookie << cookie2)
495 << true << "Set-Cookie"
496 << "a=b; path=/\nc=d";
497 QTest::newRow(dataTag: "Content-Disposition") << QNetworkRequest::ContentDispositionHeader
498 << QVariant("attachment; filename=\"test.txt\"") << true
499 << "Content-Disposition" << "attachment; filename=\"test.txt\"";
500}
501
502void tst_QNetworkRequest::rawHeaderParsing()
503{
504 QFETCH(QNetworkRequest::KnownHeaders, cookedHeader);
505 QFETCH(QVariant, cookedValue);
506 QFETCH(bool, success);
507 QFETCH(QString, rawHeader);
508 QFETCH(QString, rawValue);
509
510 QNetworkRequest request;
511 request.setRawHeader(headerName: rawHeader.toLatin1(), value: rawValue.toLatin1());
512
513 // even if it doesn't parse, it's as a raw header
514 QVERIFY(request.hasRawHeader(rawHeader.toLatin1()));
515 QVERIFY(request.hasRawHeader(rawHeader.toLower().toLatin1()));
516 QCOMPARE(QString(request.rawHeader(rawHeader.toLatin1())), rawValue);
517
518 QCOMPARE(request.header(cookedHeader).isNull(), !success);
519 if (cookedValue.type() != QVariant::UserType)
520 QCOMPARE(request.header(cookedHeader), cookedValue);
521 else if (cookedValue.userType() == qMetaTypeId<QList<QNetworkCookie> >())
522 QCOMPARE(qvariant_cast<QList<QNetworkCookie> >(request.header(cookedHeader)),
523 qvariant_cast<QList<QNetworkCookie> >(cookedValue));
524}
525
526void tst_QNetworkRequest::removeHeader()
527{
528 QNetworkRequest request;
529
530 request.setRawHeader(headerName: "Foo", value: "1");
531 QVERIFY(request.hasRawHeader("Foo"));
532 QVERIFY(request.hasRawHeader("foo"));
533 request.setRawHeader(headerName: "Foo", value: QByteArray());
534 QVERIFY(!request.hasRawHeader("Foo"));
535
536 // same, but remove with different capitalisation
537 request.setRawHeader(headerName: "Foo", value: "1");
538 QVERIFY(request.hasRawHeader("Foo"));
539 QVERIFY(request.hasRawHeader("foo"));
540 request.setRawHeader(headerName: "foo", value: QByteArray());
541 QVERIFY(!request.hasRawHeader("Foo"));
542
543 // same, but not the first
544 request.setRawHeader(headerName: "Bar", value: "2");
545 request.setRawHeader(headerName: "Foo", value: "1");
546 QVERIFY(request.hasRawHeader("Foo"));
547 QVERIFY(request.hasRawHeader("foo"));
548 request.setRawHeader(headerName: "foo", value: QByteArray());
549 QVERIFY(!request.hasRawHeader("Foo"));
550 QVERIFY(request.hasRawHeader("bar"));
551
552 // same, but not the first nor last
553 request.setRawHeader(headerName: "Foo", value: "1");
554 request.setRawHeader(headerName: "Bar", value: "3");
555 QVERIFY(request.hasRawHeader("Foo"));
556 QVERIFY(request.hasRawHeader("foo"));
557 request.setRawHeader(headerName: "foo", value: QByteArray());
558 QVERIFY(!request.hasRawHeader("Foo"));
559 QVERIFY(request.hasRawHeader("bar"));
560}
561
562void tst_QNetworkRequest::originatingObject()
563{
564 QNetworkRequest request;
565
566 QVERIFY(!request.originatingObject());
567
568 {
569 QObject dummy;
570 request.setOriginatingObject(&dummy);
571 QCOMPARE(request.originatingObject(), &dummy);
572 }
573
574 QVERIFY(!request.originatingObject());
575}
576
577QTEST_MAIN(tst_QNetworkRequest)
578#include "tst_qnetworkrequest.moc"
579

source code of qtbase/tests/auto/network/access/qnetworkrequest/tst_qnetworkrequest.cpp