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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
|
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtHttpServer/qhttpserver.h>
#include <QtHttpServer/qhttpserverrequest.h>
#include <QtHttpServer/qhttpserverrouterrule.h>
#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
#include <QtConcurrent/qtconcurrentrun.h>
#include <QtCore/qurl.h>
#include <QtCore/qstring.h>
#include <QtCore/qlist.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qsemaphore.h>
#include <QtCore/qtimer.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtNetwork/qnetworkrequest.h>
#include <QtNetwork/qtcpserver.h>
#if QT_CONFIG(ssl)
#include <QtNetwork/qsslconfiguration.h>
#include <QtNetwork/qsslsocket.h>
#include <QtNetwork/qsslkey.h>
#include <QtNetwork/qsslserver.h>
#endif
#if QT_CONFIG(localserver)
#include <qrandom.h>
#include <QtNetwork/qlocalserver.h>
#include <QtNetwork/qlocalsocket.h>
#endif
#include <QtTest/private/qtesthelpers_p.h>
#include <array>
QT_BEGIN_NAMESPACE
#if QT_CONFIG(ssl)
static const char g_privateKey[] = R"(-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAvdrtZtVquwiG12+vd3OjRVibdK2Ob73DOOWgb5rIgQ+B2Uzc
OFa0xsiRyc/bam9CEEqgn5YHSn95LJHvN3dbsA8vrFqIXTkisFAuHJqsmsYZbAIi
CX8t1tlcUmQsJmjZ1IKhk37lgGMKkc28Oh/CHbTrhJZWdQyoBbNb8KeqSHkePYu0
/BMtO/lrkXJjV6BXgK8BgOqJfOqrGCsBvW+sqZz9D51ZBBVb3YCrBZP20NVA5xZU
qOFwS3jn+9hO1XlJcwiFA3VzU7uSVt2zjzhX0iHw6GOVbjR4IStqH/y0oa9R9mQa
0hmzQ7LcV9NighX5kM8PsgT9i6Xhv2nmsjpPreLYgXoXqpDRrL0PR0CSts2ucRdf
hMhY8ViNoarZ12Z2CTaNxiHPGzNYNJPaQG40o3LEbQ3GP7igZ8go/ffSV+kZJS5j
uAHCsUvNUA4gvFfVXLxzoG6qewLXSCXoqDyJ9T7g4L81W19hsBxVp8gDqVAiBnpg
+GTLaC69WOm9OMXEROTOlin7gxlQ0pZO+2/M3uFFo/hXlIH/Mb5NPKlpNBqgLpqI
wtGMugt7Dx9JoMwWvEBlzMgeycYmNXwSHsdQ5kxFS5uYuZEri62Xrk/WWlev/PDC
RdcSUhrjVSNotFQveGKSwC5z2FOAIZioA0mPxsBixSaQY8fhiaC5ydUw4F0CAwE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c8dlQzGDe/W
K/uVHlJZNF4Y0eXVlq9DUhpvKOjGc8A208wQlsTUgPxljgJ2+4F3D+t0luc3h65m
25iw8eRGuYDoCQLG7u7MI0g8A0H+0h9Xrt8PQql86vmQhmTUhKfedVGOo2t2Bcfn
ignL7f4e1m2jh0oWTLhuP1hnVFN4KAKpVIJXhbEkH59cLCN6ARXiEHCM9rmK5Rgk
NQZlAZc2w1Ha9lqisaWWpt42QVhQM64=
-----END CERTIFICATE-----)";
#endif // QT_CONFIG(ssl)
using namespace Qt::StringLiterals;
using namespace std::chrono_literals;
using RouterHandler = std::function<void(const QRegularExpressionMatch &,
const QHttpServerRequest &, QHttpServerResponder &)>;
class QueryRequireRouterRule : public QHttpServerRouterRule
{
public:
QueryRequireRouterRule(const QString &pathPattern,
const QObject *context,
RouterHandler &&routerHandler)
: QHttpServerRouterRule(pathPattern, context, std::move(routerHandler))
{
}
void setKey(const QString &key) { m_queryKey = key; }
bool matches(const QHttpServerRequest &request, QRegularExpressionMatch *match) const override
{
if (QHttpServerRouterRule::matches(request, match)) {
if (request.query().hasQueryItem(m_queryKey))
return true;
}
return false;
}
private:
QString m_queryKey;
};
class ReplyObject : public QObject
{
Q_OBJECT
public:
void replyRespReq(QHttpServerResponder &resp,
const QHttpServerRequest &req)
{
resp.write(req.body(), "text/plain"_ba);
}
void replyReqResp(const QHttpServerRequest &req,
QHttpServerResponder &resp)
{
resp.write(req.body(), "text/plain"_ba);
}
QString replyNoArgs()
{
return "ReplyAsHtml::replyNoArgs";
}
void replyRespReqConst(QHttpServerResponder &resp,
const QHttpServerRequest &req) const
{
resp.write(req.body(), "text/plain"_ba);
}
void replyReqRespConst(const QHttpServerRequest &req,
QHttpServerResponder &resp) const
{
resp.write(req.body(), "text/plain"_ba);
}
QString replyNoArgsConst() const
{
return "ReplyAsHtml::replyNoArgs";
}
};
class tst_QHttpServer final : public QObject
{
Q_OBJECT
private slots:
void initTestCase_data();
void initTestCase();
void init();
void routeGet_data();
void routeGet();
void routeKeepAlive();
void routePost_data();
void routePost();
void routeDelete_data();
void routeDelete();
void routeExtraHeaders();
void getLongChunks();
void invalidRouterArguments();
void checkRouteLambdaCapture();
void afterRequest();
void disconnectedInEventLoop();
void multipleRequests();
void pipelinedRequests();
void missingHandler();
void pipelinedFutureRequests();
void requestNotOverwritten();
void multipleResponses();
void contextObjectInOtherThreadWarning();
void keepAliveTimeout();
#if QT_CONFIG(localserver)
void localSocket();
#endif
private:
void checkReply(QNetworkReply *reply, const QString &response);
private:
QHttpServer httpserver;
QString clearUrlBase;
QString sslUrlBase;
QNetworkAccessManager networkAccessManager;
ReplyObject replyObject;
std::optional<QSemaphore> readySem, routeSem;
};
struct CustomArg {
int data = 10;
CustomArg() {} ;
CustomArg(const QString &urlArg) : data(urlArg.toInt()) {}
};
static void reqAndRespHandler(QHttpServerResponder &resp, const QHttpServerRequest &req)
{
resp.write(req.body(), "text/html"_ba);
}
static void testHandler(QHttpServerResponder &responder)
{
responder.write("test msg", "text/html"_ba);
}
class MessageHandler {
public:
explicit MessageHandler(const char *message) : message(message) {}
const char *operator()() const {
return message;
}
private:
const char *message;
};
void tst_QHttpServer::initTestCase_data()
{
QTest::addColumn<bool>("useSsl");
QTest::addColumn<bool>("useHttp2");
QTest::newRow("http/1.1") << false << false;
#if QT_CONFIG(ssl)
if (QSslSocket::supportsSsl()) {
QTest::newRow("https/1.1") << true << false;
if (QSslSocket::supportedFeatures().contains(QSsl::SupportedFeature::ServerSideAlpn))
QTest::newRow("http/2") << true << true;
}
#endif
}
void tst_QHttpServer::initTestCase()
{
httpserver.route("/resp-and-req-from-object", &replyObject, &ReplyObject::replyRespReq);
httpserver.route("/req-and-resp-from-object", &replyObject, &ReplyObject::replyReqResp);
httpserver.route("/qstring-from-object", &replyObject, &ReplyObject::replyNoArgs);
httpserver.route("/resp-and-req-from-const-object", &replyObject, &ReplyObject::replyRespReqConst);
httpserver.route("/req-and-resp-from-const-object", &replyObject, &ReplyObject::replyReqRespConst);
httpserver.route("/qstring-from-const-object", &replyObject, &ReplyObject::replyNoArgsConst);
ReplyObject *tempReplyObject = new ReplyObject();
httpserver.route("/deleted-route", tempReplyObject, &ReplyObject::replyRespReq);
delete tempReplyObject;
httpserver.route("/req-and-resp", this, reqAndRespHandler);
httpserver.route("/resp-and-req", this, [] (const QHttpServerRequest &req,
QHttpServerResponder &resp) {
resp.write(req.body(), "text/html"_ba);
});
auto testHandlerPtr = testHandler;
httpserver.route("/test", this, testHandlerPtr);
auto l = []() -> QString { return "Hello world get"; };
httpserver.route("/", QHttpServerRequest::Method::Get, this, l);
httpserver.route("/", QHttpServerRequest::Method::Post, this, MessageHandler("Hello world post"));
httpserver.route("/post-and-get",
QHttpServerRequest::Method::Get | QHttpServerRequest::Method::Post,
this,
[](const QHttpServerRequest &request) {
if (request.method() == QHttpServerRequest::Method::Get)
return "Hello world get";
else if (request.method() == QHttpServerRequest::Method::Post)
return "Hello world post";
return "This should not work";
});
httpserver.route(
"/any", QHttpServerRequest::Method::AnyKnown, this, [](const QHttpServerRequest &request) {
static const auto metaEnum = QMetaEnum::fromType<QHttpServerRequest::Method>();
return metaEnum.valueToKey(static_cast<int>(request.method()));
});
httpserver.route("/page/", this, [] (const qint32 number) {
return QString("page: %1").arg(number);
});
httpserver.route("/page/<arg>/detail", this, [] (const quint32 number) {
return QString("page: %1 detail").arg(number);
});
httpserver.route("/user/", this, [] (const QString &name) {
return QString("%1").arg(name);
});
httpserver.route("/user/<arg>/", this, [] (const QString &name, const QByteArray &ba) {
return QString("%1-%2").arg(name).arg(QString::fromLatin1(ba));
});
httpserver.route("/test/", this, [] (const QUrl &url) {
return QString("path: %1").arg(url.path());
});
httpserver.route("/api/v", this, [] (const float api) {
return QString("api %1v").arg(api);
});
httpserver.route("/api/v<arg>/user/", this, [] (const float api, const quint64 user) {
return QString("api %1v, user id - %2").arg(api).arg(user);
});
httpserver.route("/api/v<arg>/user/<arg>/settings", this, [] (const float api, const quint64 user,
const QHttpServerRequest &request) {
const auto &role = request.query().queryItemValue(QString::fromLatin1("role"));
const auto &fragment = request.url().fragment();
return QString("api %1v, user id - %2, set settings role=%3#'%4'")
.arg(api).arg(user).arg(role, fragment);
});
auto route = httpserver.route<QueryRequireRouterRule>(
"/custom/",
this,
[] (const quint64 num, const QHttpServerRequest &request) {
return QString("Custom router rule: %1, key=%2")
.arg(num)
.arg(request.query().queryItemValue("key"));
});
QVERIFY(route);
if (route)
route->setKey(QLatin1String("key"));
httpserver.router()->addConverter<CustomArg>("[+-]?\\d+"_L1);
httpserver.route("/check-custom-type/", this, [] (const CustomArg &customArg) {
return QString("data = %1").arg(customArg.data);
});
httpserver.route("/post-body", QHttpServerRequest::Method::Post, this,
[](const QHttpServerRequest &request) { return request.body(); });
httpserver.route("/file/", this, [] (const QString &file) {
return QHttpServerResponse::fromFile(QFINDTESTDATA("data/"_L1 + file));
});
httpserver.route("/json-object/", this, [] () {
return QJsonObject{
{"property", "test"},
{"value", 1}
};
});
httpserver.route("/json-array/", this, [] () {
return QJsonArray{
1, "2",
QJsonObject{
{"name", "test"}
}
};
});
httpserver.route("/data-and-custom-status-code/", this, []() {
return QHttpServerResponse(QJsonObject{ { "key", "value" } },
QHttpServerResponder::StatusCode::Accepted);
});
httpserver.route("/chunked/", this, [](QHttpServerResponder &responder) {
responder.writeBeginChunked("text/plain", QHttpServerResponder::StatusCode::Ok);
responder.writeChunk("part 1 of the message, ");
responder.writeEndChunked("part 2 of the message");
});
httpserver.route("/longChunks/", this, [](QHttpServerResponder &responder) {
responder.writeBeginChunked("text/plain", QHttpServerResponder::StatusCode::Ok);
constexpr qsizetype chunkLength = 8 * 1024 * 1024;
QByteArray a(chunkLength, 'a');
QByteArray b(chunkLength, 'b');
QByteArray c(chunkLength, 'c');
responder.writeChunk(a);
responder.writeChunk(b);
responder.writeEndChunked(c);
});
httpserver.route("/extra-headers", this, [] () {
QHttpServerResponse resp("");
auto h = resp.headers();
h.removeAll(QHttpHeaders::WellKnownHeader::ContentType);
h.append(QHttpHeaders::WellKnownHeader::ContentType, "application/x-empty");
h.removeAll(QHttpHeaders::WellKnownHeader::Server);
h.append(QHttpHeaders::WellKnownHeader::Server, "test server");
resp.setHeaders(std::move(h));
return resp;
});
httpserver.route("/processing", this, [](QHttpServerResponder &responder) {
responder.sendResponse(QHttpServerResponse(QHttpServerResponder::StatusCode::Processing));
responder.sendResponse(QHttpServerResponse("done"));
});
httpserver.addAfterRequestHandler(this, [] (const QHttpServerRequest &, QHttpServerResponse &) {
});
#if QT_CONFIG(concurrent)
httpserver.route("/future/", this, [] (int id) {
return QtConcurrent::run([id] () -> QHttpServerResponse {
if (id == 0)
return QHttpServerResponse::StatusCode::NotFound;
QTest::qSleep(500);
return QHttpServerResponse("future is coming");
});
});
httpserver.route("/wait/<arg>", this, [](qsizetype wait) {
return QtConcurrent::run([wait] () -> QHttpServerResponse {
QThread::msleep(wait);
return QString::number(wait);
});
});
httpserver.route("/user/<arg>/delayed/", this, [](const QString &user, unsigned long delayMs) {
return QtConcurrent::run([user, delayMs]() -> QHttpServerResponse {
QThread::msleep(delayMs);
return user;
});
});
httpserver.route("/wait-and-return-body/", [this](const QHttpServerRequest &request) {
auto body = request.body();
return QtConcurrent::run([body, this]() {
Q_ASSERT(readySem);
readySem->release();
Q_ASSERT(routeSem);
routeSem->acquire();
return QHttpServerResponse("text/plain", body);
});
});
httpserver.addAfterRequestHandler(
this, [](const QHttpServerRequest &request, QHttpServerResponse &response) {
if (request.url().path().startsWith("/wait-and-return-body/")) {
auto h = response.headers();
h.append("X-REQUEST-BODY-SIZE", QString::number(request.body().size()));
response.setHeaders(std::move(h));
}
});
#endif
#if QT_CONFIG(localserver)
const auto serverName =
QStringLiteral("testserver") + QString::number(qApp->applicationPid());
std::unique_ptr<QLocalServer> localServer = std::make_unique<QLocalServer>();
if (localServer->listen(serverName))
httpserver.bind(localServer.release());
else
qCritical("Local Socket server listen failed");
#endif
auto tcpserver = std::make_unique<QTcpServer>();
QVERIFY2(tcpserver->listen(), "HTTP server listen failed");
quint16 port = tcpserver->serverPort();
QVERIFY2(httpserver.bind(tcpserver.get()), "HTTP server bind failed");
tcpserver.release();
clearUrlBase = QStringLiteral("http://localhost:%1%2").arg(port);
#if QT_CONFIG(ssl)
if (QSslSocket::supportsSsl()) {
auto sslserver = std::make_unique<QSslServer>();
QSslConfiguration serverConfig = QSslConfiguration::defaultConfiguration();
serverConfig.setLocalCertificate(QSslCertificate(g_certificate));
serverConfig.setPrivateKey(QSslKey(g_privateKey, QSsl::Rsa));
serverConfig.setAllowedNextProtocols({QSslConfiguration::ALPNProtocolHTTP2});
sslserver->setSslConfiguration(serverConfig);
QVERIFY2(sslserver->listen(), "HTTPS server listen failed");
port = sslserver->serverPort();
QVERIFY2(httpserver.bind(sslserver.get()), "HTTPS server bind failed");
sslserver.release();
sslUrlBase = QStringLiteral("https://localhost:%1%2").arg(port);
const QList<QSslError> expectedSslErrors = {
QSslError(QSslError::SelfSignedCertificate, QSslCertificate(g_certificate)),
// Non-OpenSSL backends are not able to report a specific error code
// for self-signed certificates.
QSslError(QSslError::CertificateUntrusted, QSslCertificate(g_certificate)),
QSslError(QSslError::HostNameMismatch, QSslCertificate(g_certificate)),
};
connect(&networkAccessManager, &QNetworkAccessManager::sslErrors, this,
[expectedSslErrors](QNetworkReply *reply, const QList<QSslError> &errors) {
for (const auto &error : errors) {
if (!expectedSslErrors.contains(error)) {
qCritical()
<< "Got unexpected ssl error:" << error << error.certificate();
}
}
reply->ignoreSslErrors(expectedSslErrors);
});
}
#endif
}
void tst_QHttpServer::init()
{
#if QT_CONFIG(ssl)
QFETCH_GLOBAL(const bool, useSsl);
if (useSsl && QTestPrivate::isSecureTransportBlockingTest())
QSKIP("SslServer is blocking the test execution while trying to access the login keychain");
#endif // QT_CONFIG(ssl)
}
void tst_QHttpServer::routeGet_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<int>("code");
QTest::addColumn<QString>("type");
QTest::addColumn<QString>("body");
QTest::addRow("hello world")
<< "/"
<< 200
<< "text/plain"
<< "Hello world get";
QTest::addRow("test msg")
<< "/test"
<< 200
<< "text/html"
<< "test msg";
QTest::addRow("not found")
<< "/not-found"
<< 404
<< "application/x-empty"
<< "";
QTest::addRow("qstring-from-object")
<< "/qstring-from-object"
<< 200
<< "text/plain"
<< "ReplyAsHtml::replyNoArgs";
QTest::addRow("qstring-from-const-object")
<< "/qstring-from-const-object"
<< 200
<< "text/plain"
<< "ReplyAsHtml::replyNoArgs";
QTest::addRow("arg:int")
<< "/page/10"
<< 200
<< "text/plain"
<< "page: 10";
QTest::addRow("arg:-int")
<< "/page/-10"
<< 200
<< "text/plain"
<< "page: -10";
QTest::addRow("arg:uint")
<< "/page/10/detail"
<< 200
<< "text/plain"
<< "page: 10 detail";
QTest::addRow("arg:-uint")
<< "/page/-10/detail"
<< 404
<< "application/x-empty"
<< "";
QTest::addRow("arg:string")
<< "/user/test"
<< 200
<< "text/plain"
<< "test";
QTest::addRow("arg:string,!a+.")
<< "/user/test test ,!a+."
<< 200
<< "text/plain"
<< "test test ,!a+.";
QTest::addRow("arg:string,ba")
<< "/user/james/bond"
<< 200
<< "text/plain"
<< "james-bond";
QTest::addRow("arg:url")
<< "/test/api/v0/cmds?val=1"
<< 200
<< "text/plain"
<< "path: api/v0/cmds";
QTest::addRow("arg:float 5.1")
<< "/api/v5.1"
<< 200
<< "text/plain"
<< "api 5.1v";
QTest::addRow("arg:float 5.")
<< "/api/v5."
<< 200
<< "text/plain"
<< "api 5v";
QTest::addRow("arg:float 6.0")
<< "/api/v6.0"
<< 200
<< "text/plain"
<< "api 6v";
QTest::addRow("arg:float,uint")
<< "/api/v5.1/user/10"
<< 200
<< "text/plain"
<< "api 5.1v, user id - 10";
QTest::addRow("arg:float,uint,query")
<< "/api/v5.2/user/11/settings?role=admin"
<< 200
<< "text/plain"
<< "api 5.2v, user id - 11, set settings role=admin#''";
// The fragment isn't actually sent via HTTP (it's information for the user agent)
QTest::addRow("arg:float,uint, query+fragment")
<< "/api/v5.2/user/11/settings?role=admin#tag"
<< 200
<< "text/plain"
<< "api 5.2v, user id - 11, set settings role=admin#''";
QTest::addRow("custom route rule")
<< "/custom/15"
<< 404
<< "application/x-empty"
<< "";
QTest::addRow("custom route rule + query")
<< "/custom/10?key=11&g=1"
<< 200
<< "text/plain"
<< "Custom router rule: 10, key=11";
QTest::addRow("custom route rule + query key req")
<< "/custom/10?g=1&key=12"
<< 200
<< "text/plain"
<< "Custom router rule: 10, key=12";
QTest::addRow("post-and-get, get")
<< "/post-and-get"
<< 200
<< "text/plain"
<< "Hello world get";
QTest::addRow("invalid-rule-method, get")
<< "/invalid-rule-method"
<< 404
<< "application/x-empty"
<< "";
QTest::addRow("check custom type, data=1")
<< "/check-custom-type/1"
<< 200
<< "text/plain"
<< "data = 1";
QTest::addRow("any, get")
<< "/any"
<< 200
<< "text/plain"
<< "Get";
QTest::addRow("response from html file")
<< "/file/text.html"
<< 200
<< "text/html"
<< "<html></html>";
QTest::addRow("response from json file")
<< "/file/application.json"
<< 200
<< "application/json"
<< "{ \"key\": \"value\" }";
QTest::addRow("json-object")
<< "/json-object/"
<< 200
<< "application/json"
<< "{\"property\":\"test\",\"value\":1}";
QTest::addRow("json-array")
<< "/json-array/"
<< 200
<< "application/json"
<< "[1,\"2\",{\"name\":\"test\"}]";
QTest::addRow("data-and-custom-status-code")
<< "/data-and-custom-status-code/"
<< 202
<< "application/json"
<< "{\"key\":\"value\"}";
QTest::addRow("chunked")
<< "/chunked/"
<< 200
<< "text/plain"
<< "part 1 of the message, part 2 of the message";
#if QT_CONFIG(concurrent)
QTest::addRow("future")
<< "/future/1"
<< 200
<< "text/plain"
<< "future is coming";
QTest::addRow("future-not-found")
<< "/future/0"
<< 404
<< "application/x-empty"
<< "";
#endif
}
void tst_QHttpServer::routeGet()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QFETCH(QString, url);
QFETCH(int, code);
QFETCH(QString, type);
QFETCH(QString, body);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request = QNetworkRequest(urlBase.arg(url));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
QCOMPARE(reply->readAll().trimmed(), body);
}
void tst_QHttpServer::routeKeepAlive()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
if (useHttp2)
QSKIP("Keep-alive header is not allowed for HTTP 2");
httpserver.route("/keep-alive", this,
[] (const QHttpServerRequest &req) -> QHttpServerResponse {
return QString("header: %1, query: %2, body: %3, method: %4")
.arg(req.value("CustomHeader"),
req.url().query(),
req.body())
.arg(static_cast<int>(req.method()));
});
QNetworkRequest request(urlBase.arg("/keep-alive"));
request.setRawHeader(QByteArray("Connection"), QByteArray("keep-alive"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
checkReply(networkAccessManager.get(request),
QString("header: , query: , body: , method: %1")
.arg(static_cast<int>(QHttpServerRequest::Method::Get)));
if (QTest::currentTestFailed())
return;
request.setUrl(urlBase.arg("/keep-alive?po=98"));
request.setRawHeader("CustomHeader", "1");
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
checkReply(networkAccessManager.post(request, QByteArray("test")),
QString("header: 1, query: po=98, body: test, method: %1")
.arg(static_cast<int>(QHttpServerRequest::Method::Post)));
if (QTest::currentTestFailed())
return;
request = QNetworkRequest(urlBase.arg("/keep-alive"));
request.setRawHeader(QByteArray("Connection"), QByteArray("keep-alive"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
checkReply(networkAccessManager.post(request, QByteArray("")),
QString("header: , query: , body: , method: %1")
.arg(static_cast<int>(QHttpServerRequest::Method::Post)));
if (QTest::currentTestFailed())
return;
checkReply(networkAccessManager.get(request),
QString("header: , query: , body: , method: %1")
.arg(static_cast<int>(QHttpServerRequest::Method::Get)));
if (QTest::currentTestFailed())
return;
}
void tst_QHttpServer::routePost_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<int>("code");
QTest::addColumn<QString>("type");
QTest::addColumn<QString>("data");
QTest::addColumn<QString>("body");
QTest::addRow("hello world")
<< "/"
<< 200
<< "text/plain"
<< ""
<< "Hello world post";
QTest::addRow("post-and-get, post")
<< "/post-and-get"
<< 200
<< "text/plain"
<< ""
<< "Hello world post";
QTest::addRow("any, post")
<< "/any"
<< 200
<< "text/plain"
<< ""
<< "Post";
QTest::addRow("post-body")
<< "/post-body"
<< 200
<< "text/plain"
<< "some post data"
<< "some post data";
QString body;
for (int i = 0; i < 10000; i++)
body.append(QString::number(i));
QTest::addRow("post-body - huge body, chunk test")
<< "/post-body"
<< 200
<< "text/plain"
<< body
<< body;
QTest::addRow("req-and-resp")
<< "/req-and-resp"
<< 200
<< "text/html"
<< "test"
<< "test";
QTest::addRow("resp-and-req")
<< "/resp-and-req"
<< 200
<< "text/html"
<< "test"
<< "test";
QTest::addRow("resp-and-req-from-object")
<< "/resp-and-req-from-object"
<< 200
<< "text/plain"
<< "test"
<< "test";
QTest::addRow("req-and-resp-from-object")
<< "/req-and-resp-from-object"
<< 200
<< "text/plain"
<< "test"
<< "test";
QTest::addRow("resp-and-req-from-const-object")
<< "/resp-and-req-from-const-object"
<< 200
<< "text/plain"
<< "test"
<< "test";
QTest::addRow("req-and-resp-from-const-object")
<< "/req-and-resp-from-const-object"
<< 200
<< "text/plain"
<< "test"
<< "test";
QTest::addRow("deleted-route")
<< "/deleted-route"
<< 404
<< "application/x-empty"
<< ""
<< "";
}
void tst_QHttpServer::routePost()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QFETCH(QString, url);
QFETCH(int, code);
QFETCH(QString, type);
QFETCH(QString, data);
QFETCH(QString, body);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg(url));
if (data.size())
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.post(request, data.toUtf8()));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
QCOMPARE(reply->readAll(), body);
}
void tst_QHttpServer::routeDelete_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<int>("code");
QTest::addColumn<QString>("type");
QTest::addColumn<QString>("data");
QTest::addRow("post-and-get, delete")
<< "/post-and-get"
<< 404
<< "application/x-empty"
<< "";
QTest::addRow("any, delete")
<< "/any"
<< 200
<< "text/plain"
<< "Delete";
}
void tst_QHttpServer::routeDelete()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QFETCH(QString, url);
QFETCH(int, code);
QFETCH(QString, type);
QFETCH(QString, data);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg(url));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.deleteResource(request));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
}
void tst_QHttpServer::routeExtraHeaders()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg("/extra-headers"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), "application/x-empty");
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
QCOMPARE(reply->header(QNetworkRequest::ServerHeader), "test server");
}
void tst_QHttpServer::getLongChunks()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg("/longChunks/"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), "text/plain");
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
QByteArray body(reply->readAll());
qsizetype offset = 0;
char parts[] = { 'a', 'b', 'c' };
constexpr qsizetype chunkLength = 8 * 1024 * 1024;
for (auto part : parts) {
for (int i = 0; i < chunkLength; ++i)
QCOMPARE(body[i + offset], part);
offset += chunkLength;
}
}
struct CustomType {
CustomType() {}
CustomType(const QString &) {}
};
void tst_QHttpServer::invalidRouterArguments()
{
QTest::ignoreMessage(QtWarningMsg, "Can not find converter for type: QDateTime");
QCOMPARE(
httpserver.route("/datetime/", this, [] (const QDateTime &datetime) {
return QString("datetime: %1").arg(datetime.toString());
}),
nullptr);
QTest::ignoreMessage(QtWarningMsg,
"CustomType has not registered a converter to QString. "
"Use QHttpServerRouter::addConveter<Type>(converter).");
QCOMPARE(
httpserver.route("/implicit-conversion-to-qstring-has-no-registered/",
this,
[] (const CustomType &) {
return "";
}),
nullptr);
}
void tst_QHttpServer::checkRouteLambdaCapture()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request;
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
httpserver.route("/capture-this/", this, [&urlBase] () {
return urlBase;
});
QString msg = urlBase + "/pod";
httpserver.route("/capture-non-pod-data/", this, [&msg] () {
return msg;
});
request.setUrl(QUrl(urlBase.arg("/capture-this/")));
checkReply(networkAccessManager.get(request), urlBase);
if (QTest::currentTestFailed())
return;
request.setUrl(QUrl(urlBase.arg("/capture-non-pod-data/")));
checkReply(networkAccessManager.get(request), msg);
if (QTest::currentTestFailed())
return;
}
void tst_QHttpServer::afterRequest()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg("/test-after-request"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
httpserver.addAfterRequestHandler(this, [] (const QHttpServerRequest &request,
QHttpServerResponse &resp) {
if (request.url().path() == "/test-after-request") {
auto h = resp.headers();
h.removeAll("Arguments-Order-1");
h.append("Arguments-Order-1", "resp, request");
resp.setHeaders(std::move(h));
}
});
httpserver.addAfterRequestHandler(this, [] (const QHttpServerRequest &request,
QHttpServerResponse &resp) {
if (request.url().path() == "/test-after-request") {
auto h = resp.headers();
h.removeAll("Arguments-Order-2");
h.append("Arguments-Order-2", "request, resp");
resp.setHeaders(std::move(h));
}
});
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), "application/x-empty");
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
QCOMPARE(reply->rawHeader("Arguments-Order-1"), "resp, request");
QCOMPARE(reply->rawHeader("Arguments-Order-2"), "request, resp");
}
void tst_QHttpServer::checkReply(QNetworkReply *reply, const QString &response) {
QVERIFY(reply != nullptr);
std::unique_ptr<QNetworkReply> replyPtr(reply);
QTRY_VERIFY(replyPtr->isFinished());
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
const QVariant http2Used = replyPtr->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = replyPtr->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
QCOMPARE(replyPtr->header(QNetworkRequest::ContentTypeHeader).toByteArray(), "text/plain");
QCOMPARE(replyPtr->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
QCOMPARE(replyPtr->readAll(), response);
};
void tst_QHttpServer::disconnectedInEventLoop()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg("/event-loop/"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
httpserver.route("/event-loop/", this, [] () {
QEventLoop loop;
QTimer::singleShot(1000, &loop, &QEventLoop::quit);
loop.exec();
return QHttpServerResponse::StatusCode::Ok;
});
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTimer::singleShot(500, reply.get(), &QNetworkReply::abort); // cancel connection
QEventLoop loop;
connect(reply.get(), &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
}
void tst_QHttpServer::multipleRequests()
{
// Test to ensure that the passed lambda is not moved away after the
// first handled request
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QNetworkRequest request(urlBase.arg("/do-not-move"));
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
httpserver.route("/do-not-move", this, [v = std::vector<int>{1, 2, 3}] () {
return QString::number(v.size());
});
checkReply(networkAccessManager.get(request), "3");
if (QTest::currentTestFailed())
return;
checkReply(networkAccessManager.get(request), "3");
if (QTest::currentTestFailed())
return;
}
void tst_QHttpServer::pipelinedRequests()
{
QNetworkReply *replies[10];
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
for (std::size_t i = 0; i < std::size(replies); i++) {
QNetworkRequest req(QUrl(urlBase.arg("/user/") + QString::number(i)));
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
replies[i] = networkAccessManager.get(req);
}
for (std::size_t i = 0; i < std::size(replies); i++)
checkReply(replies[i], QString::number(i));
}
class OkResponder : public QObject
{
Q_OBJECT
public:
void operator()(const QHttpServerRequest &, QHttpServerResponder &responder)
{
responder.write(QHttpServerResponder::StatusCode::Ok);
}
};
void tst_QHttpServer::missingHandler()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
const QUrl requestUrl(urlBase.arg("/missing"));
QNetworkRequest request(requestUrl);
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
std::unique_ptr<QNetworkReply> reply(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
{
auto guard = QScopeGuard([this]() { httpserver.clearMissingHandler(); });
httpserver.setMissingHandler(
this, [](const QHttpServerRequest &, QHttpServerResponder &responder) {
responder.write(QHttpServerResponder::StatusCode::Ok);
});
reply.reset(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
}
reply.reset(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
{
OkResponder responder;
httpserver.setMissingHandler(&responder, &OkResponder::operator());
QNetworkRequest request2(requestUrl);
request2.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
reply.reset(networkAccessManager.get(QNetworkRequest(requestUrl)));
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
}
reply.reset(networkAccessManager.get(request));
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
const QVariant http2Used = reply->attribute(QNetworkRequest::Http2WasUsedAttribute);
QVERIFY(http2Used.isValid());
QCOMPARE(http2Used.toBool(), useHttp2);
const QVariant sslUsed = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QVERIFY(sslUsed.isValid());
QCOMPARE(sslUsed.toBool(), useSsl);
}
#if QT_CONFIG(concurrent)
// Test that responses to pipelined requests come in correct order, see also: QTBUG-105202
void tst_QHttpServer::pipelinedFutureRequests()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
std::array<QNetworkReply *, 10> replies;
QThreadPool::globalInstance()->setMaxThreadCount(static_cast<int>(replies.size()));
for (std::size_t i = 0; i < replies.size(); i++) {
auto delayMs = 1000 / replies.size() * (replies.size() - i);
QString path = u"/user/%1/delayed/%2"_s.arg(i).arg(delayMs);
QNetworkRequest req(QUrl(urlBase.arg(path)));
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
replies[i] = networkAccessManager.get(req);
}
for (std::size_t i = 0; i < replies.size(); i++)
checkReply(replies[i], QString::number(i));
}
void tst_QHttpServer::requestNotOverwritten()
{
readySem.emplace();
routeSem.emplace();
auto cleanup = qScopeGuard([this] {
readySem.reset();
routeSem.reset();
});
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
constexpr qsizetype NumberProcessed = 6;
std::array<QNetworkReply *, NumberProcessed> replies;
QThreadPool::globalInstance()->setMaxThreadCount(static_cast<int>(NumberProcessed));
for (std::size_t i = 0; i < NumberProcessed; i++) {
auto bodySize = i + 1; // All requests have different body sizes
QByteArray body(bodySize, 'a');
QString path = u"/wait-and-return-body/"_s;
QNetworkRequest req(QUrl(urlBase.arg(path)));
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
req.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
replies[i] = networkAccessManager.post(req, body);
}
auto waitUntilFuturesStarted = [this, &replies] {
if (QTest::qWaitFor([this]() { return readySem->available() == NumberProcessed; })) {
return true;
} else {
routeSem->release(NumberProcessed);
for (std::size_t i = 0; i < NumberProcessed; i++) {
if (replies[i] != nullptr) {
std::unique_ptr<QNetworkReply> replyPtr(replies[i]);
replyPtr->abort();
}
}
return false;
}
};
QVERIFY(waitUntilFuturesStarted());
readySem->acquire(NumberProcessed);
routeSem->release(NumberProcessed); // Let them complete and execute the afterrequest handlers
QSet<int> bodySizes;
for (std::size_t i = 0; i < NumberProcessed; i++) {
QVERIFY(replies[i] != nullptr);
std::unique_ptr<QNetworkReply> replyPtr(replies[i]);
QTRY_VERIFY(replyPtr->isFinished());
QVERIFY(replyPtr->hasRawHeader("X-REQUEST-BODY-SIZE"));
QByteArray header = replyPtr->rawHeader("X-REQUEST-BODY-SIZE");
bool ok;
int bodySize = header.toInt(&ok);
QVERIFY(ok);
bodySizes.insert(bodySize);
}
QCOMPARE(bodySizes.size(), NumberProcessed);
QCOMPARE(readySem->available(), 0);
QCOMPARE(routeSem->available(), 0);
}
#endif // QT_CONFIG(concurrent)
void tst_QHttpServer::multipleResponses()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
if (useHttp2)
QSKIP("StatusCode::Processing is not supported for HTTP 2");
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
const QUrl requestUrl(urlBase.arg("/processing"));
QNetworkRequest req(requestUrl);
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
auto reply = networkAccessManager.get(req);
QTRY_VERIFY(reply->isFinished());
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), "text/plain");
QCOMPARE(reply->readAll(), "done");
}
void tst_QHttpServer::contextObjectInOtherThreadWarning()
{
QThread newThread;
ReplyObject replyObject;
replyObject.moveToThread(&newThread);
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*context object.*same thread"));
QCOMPARE(httpserver.route("/otherThread", &replyObject, &ReplyObject::replyReqResp), nullptr);
}
#if QT_CONFIG(localserver)
void tst_QHttpServer::localSocket()
{
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
if (useSsl || useHttp2)
QSKIP("Use only HTTP 1.1 for localSocket");
QVERIFY(!httpserver.localServers().isEmpty());
for (const auto &localServer : httpserver.localServers()) {
QLocalSocket socket;
socket.connectToServer(localServer->fullServerName());
QVERIFY(socket.waitForConnected(1));
qApp->processEvents();
socket.write("GET /test HTTP/1.1\r\n"
"Host: local\r\n"
"User-Agent: curl/7.88.1\r\n"
"Accept: */*\r\n\r\n");
const QByteArray expectedResult =
"HTTP/1.1 200 OK\r\ncontent-type: text/html\r\ncontent-length: 8\r\n\r\ntest msg";
// We need to call process events a couple of times for the write/read to go through
QTRY_COMPARE_GE(socket.bytesAvailable(), expectedResult.size());
QCOMPARE(socket.readAll(), expectedResult);
}
}
#endif
void tst_QHttpServer::keepAliveTimeout()
{
#if QT_CONFIG(concurrent)
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
QHttpServerConfiguration config;
config.setKeepAliveTimeout(1s);
httpserver.setConfiguration(config);
const auto slowWaitTime = QString::number(6000);
QNetworkRequest reqSlow(QUrl(urlBase.arg(u"/wait/"_s + slowWaitTime)));
reqSlow.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
reqSlow.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
QNetworkReply *slowReply = networkAccessManager.get(reqSlow);
const auto fastWaitTime = QString::number(1000);
QNetworkRequest reqFast(QUrl(urlBase.arg(u"/wait/"_s + fastWaitTime)));
reqFast.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
reqFast.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
QNetworkReply *fastReply = networkAccessManager.get(reqFast);
QTRY_VERIFY_WITH_TIMEOUT(slowReply->isFinished() && fastReply->isFinished(), 7000);
checkReply(slowReply, slowWaitTime);
checkReply(fastReply, fastWaitTime);
#else
QSKIP("QtConcurrent is not available, skipping test");
#endif // QT_CONFIG(concurrent)
}
QT_END_NAMESPACE
Q_DECLARE_METATYPE(CustomArg);
Q_DECLARE_METATYPE(CustomType);
QTEST_MAIN(tst_QHttpServer)
#include "tst_qhttpserver.moc"
|