2022-06-22 12:54:22 +00:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
#include <QtHttpServer/qhttpserver.h>
|
|
|
|
#include <QtHttpServer/qhttpserverrequest.h>
|
|
|
|
#include <QtHttpServer/qhttpserverrouterrule.h>
|
|
|
|
|
|
|
|
#include <QtTest/qtest.h>
|
|
|
|
#include <QtTest/qsignalspy.h>
|
|
|
|
|
2020-12-14 15:55:30 +00:00
|
|
|
#include <QtConcurrent/qtconcurrentrun.h>
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
#include <QtCore/qurl.h>
|
|
|
|
#include <QtCore/qstring.h>
|
|
|
|
#include <QtCore/qlist.h>
|
|
|
|
#include <QtCore/qbytearray.h>
|
2019-01-23 12:34:57 +00:00
|
|
|
#include <QtCore/qdatetime.h>
|
2019-03-12 14:26:33 +00:00
|
|
|
#include <QtCore/qmetaobject.h>
|
2019-07-06 13:00:17 +00:00
|
|
|
#include <QtCore/qjsonobject.h>
|
|
|
|
#include <QtCore/qjsonvalue.h>
|
2019-07-06 12:44:56 +00:00
|
|
|
#include <QtCore/qjsonarray.h>
|
2020-06-04 21:53:12 +00:00
|
|
|
#include <QtCore/qtimer.h>
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
#include <QtNetwork/qnetworkaccessmanager.h>
|
|
|
|
#include <QtNetwork/qnetworkreply.h>
|
|
|
|
#include <QtNetwork/qnetworkrequest.h>
|
|
|
|
|
2022-06-29 12:01:16 +00:00
|
|
|
#include <QtNetwork/qsslconfiguration.h>
|
|
|
|
#include <QtNetwork/qsslkey.h>
|
|
|
|
|
2022-08-18 13:57:33 +00:00
|
|
|
#include <array>
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
static const char g_privateKey[] = R"(-----BEGIN RSA PRIVATE KEY-----
|
2022-02-07 14:45:40 +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J8GO0NAoIBAE5nwz0iU4+ZFbuknqI76MVkL6xC
|
|
|
|
llcZHCOpZZIpXTZmCqWySQycqFO3U8BxD2DTxsNAKH0YsnaihHyNgp1g5fzFnPb8
|
|
|
|
HlVuHhCfJN5Ywo1gfCNHaRJIMYgjPAD+ewTDSowbzH2HlpUt5NOQJWuiZfxPDJll
|
|
|
|
qmRAqZ3fyf8AP7pXxj5p0y8AUPtkmjk7h8hxstbvcmQvtTDzgkqeBYwZhEtGemdY
|
|
|
|
OCi7UuXYjRwDfnka2nAdB9lv4ExvU5lkrJVZXONYUwToArAxRtdKMqCfl36JILMA
|
|
|
|
C4+9sOeTo6HtZRvPVNLMX/rkWIv+onFgblfb8guA2wz1JUT00fNxQPt1k8s=
|
2019-09-26 12:27:07 +00:00
|
|
|
-----END RSA PRIVATE KEY-----)";
|
|
|
|
|
|
|
|
static const char g_certificate[] = R"(-----BEGIN CERTIFICATE-----
|
2022-02-07 14:45:40 +00:00
|
|
|
MIIFszCCA5ugAwIBAgIUfpP54qSLfus/pFUIBDizbnrDjE4wDQYJKoZIhvcNAQEL
|
2019-09-26 12:27:07 +00:00
|
|
|
BQAwaDELMAkGA1UEBhMCRlIxDzANBgNVBAgMBkZyYW5jZTERMA8GA1UEBwwIR3Jl
|
|
|
|
bm9ibGUxFjAUBgNVBAoMDVF0Q29udHJpYnV0b3IxHTAbBgNVBAMMFHFodHRwc3Nl
|
2022-02-07 14:45:40 +00:00
|
|
|
cnZlcnRlc3QuY29tMCAXDTIyMDIwNzE0MzE0NVoYDzIyNjgwNzA3MTQzMTQ1WjBo
|
2019-09-26 12:27:07 +00:00
|
|
|
MQswCQYDVQQGEwJGUjEPMA0GA1UECAwGRnJhbmNlMREwDwYDVQQHDAhHcmVub2Js
|
|
|
|
ZTEWMBQGA1UECgwNUXRDb250cmlidXRvcjEdMBsGA1UEAwwUcWh0dHBzc2VydmVy
|
2022-02-07 14:45:40 +00:00
|
|
|
dGVzdC5jb20wggIiMA0GCSqGSIb3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|
|
LhbUAxim/iIgWZR33uzvqXMXBORZ0zffjy2SjGCW8ZJYyTmg9c0tc0jEjv7owtlU
|
|
|
|
m6tUXMOs9U0CzvEKLt0K0dMALaLkXtscuzEWA4PHVvnvTu0Wyjj/8n+DgYzY09kC
|
|
|
|
YF0lJfcG6bddDgspmYyFpULeGGP7+qwgGh4cVBtY5I4Htr3p7hDo6UGDF6AsMQZF
|
|
|
|
1CAEgBVRbJgI2GTnptpm9k3EFKwQ81z5O+NnP3ZsuuZ3CEVaPHyQf/POLAIhmZLt
|
|
|
|
0vS9qoRiS4uMUJDXz2kJFBOFHki073eMvHiKtlpYOlJXMQ4MkHCydjeeuhHcgUCq
|
|
|
|
ZDWuQMmq/8tMwf4YtvxYtXzAMVW9dM8BgWu2G8/JwPMGUGhLfKkHmc8dlQzGDe/W
|
|
|
|
K/uVHlJZNF4Y0eXVlq9DUhpvKOjGc8A208wQlsTUgPxljgJ2+4F3D+t0luc3h65m
|
|
|
|
25iw8eRGuYDoCQLG7u7MI0g8A0H+0h9Xrt8PQql86vmQhmTUhKfedVGOo2t2Bcfn
|
|
|
|
ignL7f4e1m2jh0oWTLhuP1hnVFN4KAKpVIJXhbEkH59cLCN6ARXiEHCM9rmK5Rgk
|
|
|
|
NQZlAZc2w1Ha9lqisaWWpt42QVhQM64=
|
2019-09-26 12:27:07 +00:00
|
|
|
-----END CERTIFICATE-----)";
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2022-06-21 20:37:39 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
class QueryRequireRouterRule : public QHttpServerRouterRule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QueryRequireRouterRule(const QString &pathPattern,
|
|
|
|
const char *queryKey,
|
2022-06-21 20:58:10 +00:00
|
|
|
RouterHandler routerHandler)
|
|
|
|
: QHttpServerRouterRule(pathPattern, std::move(routerHandler)),
|
2018-10-30 15:23:23 +00:00
|
|
|
m_queryKey(queryKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
const char * m_queryKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
class tst_QHttpServer final : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void initTestCase();
|
|
|
|
void routeGet_data();
|
|
|
|
void routeGet();
|
2019-03-25 15:09:50 +00:00
|
|
|
void routeKeepAlive();
|
2018-10-30 15:23:23 +00:00
|
|
|
void routePost_data();
|
|
|
|
void routePost();
|
2019-03-12 14:26:33 +00:00
|
|
|
void routeDelete_data();
|
|
|
|
void routeDelete();
|
2019-07-21 18:52:31 +00:00
|
|
|
void routeExtraHeaders();
|
2019-01-23 12:34:57 +00:00
|
|
|
void invalidRouterArguments();
|
2019-05-03 13:19:00 +00:00
|
|
|
void checkRouteLambdaCapture();
|
2020-04-29 20:06:00 +00:00
|
|
|
void afterRequest();
|
2020-06-04 21:53:12 +00:00
|
|
|
void disconnectedInEventLoop();
|
2022-07-14 09:04:02 +00:00
|
|
|
void multipleRequests();
|
2022-08-01 14:15:29 +00:00
|
|
|
void pipelinedRequests();
|
2022-08-19 14:28:46 +00:00
|
|
|
void missingHandler();
|
2022-08-18 13:57:33 +00:00
|
|
|
void pipelinedFutureRequests();
|
2019-05-03 13:19:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void checkReply(QNetworkReply *reply, const QString &response);
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QHttpServer httpserver;
|
|
|
|
QString urlBase;
|
2019-09-26 12:27:07 +00:00
|
|
|
QString sslUrlBase;
|
|
|
|
QNetworkAccessManager networkAccessManager;
|
2018-10-30 15:23:23 +00:00
|
|
|
};
|
|
|
|
|
2019-01-23 12:34:57 +00:00
|
|
|
struct CustomArg {
|
|
|
|
int data = 10;
|
|
|
|
|
|
|
|
CustomArg() {} ;
|
|
|
|
CustomArg(const QString &urlArg) : data(urlArg.toInt()) {}
|
|
|
|
};
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
void tst_QHttpServer::initTestCase()
|
|
|
|
{
|
2019-09-01 19:33:22 +00:00
|
|
|
|
|
|
|
httpserver.route("/req-and-resp", [] (QHttpServerResponder &&resp,
|
|
|
|
const QHttpServerRequest &req) {
|
2022-08-08 14:26:33 +00:00
|
|
|
resp.write(req.body(), "text/html"_ba);
|
2019-09-01 19:33:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/resp-and-req", [] (const QHttpServerRequest &req,
|
|
|
|
QHttpServerResponder &&resp) {
|
2022-08-08 14:26:33 +00:00
|
|
|
resp.write(req.body(), "text/html"_ba);
|
2019-09-01 19:33:22 +00:00
|
|
|
});
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
httpserver.route("/test", [] (QHttpServerResponder &&responder) {
|
2022-08-08 14:26:33 +00:00
|
|
|
responder.write("test msg", "text/html"_ba);
|
2018-10-30 15:23:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/", QHttpServerRequest::Method::Get, [] () {
|
|
|
|
return "Hello world get";
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/", QHttpServerRequest::Method::Post, [] () {
|
|
|
|
return "Hello world post";
|
|
|
|
});
|
|
|
|
|
2022-07-14 12:54:44 +00:00
|
|
|
httpserver.route("/post-and-get",
|
|
|
|
QHttpServerRequest::Method::Get | QHttpServerRequest::Method::Post,
|
|
|
|
[](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(
|
2022-08-09 10:44:12 +00:00
|
|
|
"/any", QHttpServerRequest::Method::AnyKnown, [](const QHttpServerRequest &request) {
|
2022-07-14 12:54:44 +00:00
|
|
|
static const auto metaEnum = QMetaEnum::fromType<QHttpServerRequest::Method>();
|
|
|
|
return metaEnum.valueToKey(static_cast<int>(request.method()));
|
|
|
|
});
|
2019-03-12 14:26:33 +00:00
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
httpserver.route("/page/", [] (const qint32 number) {
|
|
|
|
return QString("page: %1").arg(number);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/page/<arg>/detail", [] (const quint32 number) {
|
|
|
|
return QString("page: %1 detail").arg(number);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/user/", [] (const QString &name) {
|
|
|
|
return QString("%1").arg(name);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/user/<arg>/", [] (const QString &name, const QByteArray &ba) {
|
|
|
|
return QString("%1-%2").arg(name).arg(QString::fromLatin1(ba));
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/test/", [] (const QUrl &url) {
|
|
|
|
return QString("path: %1").arg(url.path());
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/api/v", [] (const float api) {
|
|
|
|
return QString("api %1v").arg(api);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route("/api/v<arg>/user/", [] (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", [] (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);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.route<QueryRequireRouterRule>(
|
|
|
|
"/custom/",
|
|
|
|
"key",
|
|
|
|
[] (const quint64 num, const QHttpServerRequest &request) {
|
|
|
|
return QString("Custom router rule: %1, key=%2")
|
|
|
|
.arg(num)
|
|
|
|
.arg(request.query().queryItemValue("key"));
|
|
|
|
});
|
|
|
|
|
2022-06-21 20:37:39 +00:00
|
|
|
httpserver.router()->addConverter<CustomArg>("[+-]?\\d+"_L1);
|
2019-01-23 12:34:57 +00:00
|
|
|
httpserver.route("/check-custom-type/", [] (const CustomArg &customArg) {
|
|
|
|
return QString("data = %1").arg(customArg.data);
|
|
|
|
});
|
2019-03-30 09:48:47 +00:00
|
|
|
|
2022-07-14 12:54:44 +00:00
|
|
|
httpserver.route("/post-body", QHttpServerRequest::Method::Post,
|
|
|
|
[](const QHttpServerRequest &request) { return request.body(); });
|
2019-03-30 09:48:47 +00:00
|
|
|
|
2019-04-27 06:54:53 +00:00
|
|
|
httpserver.route("/file/", [] (const QString &file) {
|
2022-06-21 20:37:39 +00:00
|
|
|
return QHttpServerResponse::fromFile(QFINDTESTDATA("data/"_L1 + file));
|
2019-04-27 06:54:53 +00:00
|
|
|
});
|
|
|
|
|
2019-07-06 13:00:17 +00:00
|
|
|
httpserver.route("/json-object/", [] () {
|
|
|
|
return QJsonObject{
|
|
|
|
{"property", "test"},
|
|
|
|
{"value", 1}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-07-06 12:44:56 +00:00
|
|
|
httpserver.route("/json-array/", [] () {
|
|
|
|
return QJsonArray{
|
|
|
|
1, "2",
|
|
|
|
QJsonObject{
|
|
|
|
{"name", "test"}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-08-05 10:46:02 +00:00
|
|
|
httpserver.route("/data-and-custom-status-code/", []() {
|
|
|
|
return QHttpServerResponse(QJsonObject{ { "key", "value" } },
|
|
|
|
QHttpServerResponder::StatusCode::Accepted);
|
|
|
|
});
|
|
|
|
|
2019-07-18 19:30:10 +00:00
|
|
|
httpserver.route("/chunked/", [] (QHttpServerResponder &&responder) {
|
|
|
|
responder.writeStatusLine(QHttpServerResponder::StatusCode::Ok);
|
|
|
|
responder.writeHeaders({
|
|
|
|
{"Content-Type", "text/plain"},
|
|
|
|
{"Transfer-Encoding", "chunked"} });
|
|
|
|
|
|
|
|
auto writeChunk = [&responder] (const char *message) {
|
|
|
|
responder.writeBody(QByteArray::number(qstrlen(message), 16));
|
|
|
|
responder.writeBody("\r\n");
|
|
|
|
responder.writeBody(message);
|
|
|
|
responder.writeBody("\r\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
writeChunk("part 1 of the message, ");
|
|
|
|
writeChunk("part 2 of the message");
|
|
|
|
writeChunk("");
|
|
|
|
});
|
|
|
|
|
2019-07-21 18:52:31 +00:00
|
|
|
httpserver.route("/extra-headers", [] () {
|
|
|
|
QHttpServerResponse resp("");
|
|
|
|
resp.setHeader("Content-Type", "application/x-empty");
|
|
|
|
resp.setHeader("Server", "test server");
|
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
|
2020-04-29 20:06:00 +00:00
|
|
|
httpserver.afterRequest([] (QHttpServerResponse &&resp) {
|
|
|
|
return std::move(resp);
|
|
|
|
});
|
|
|
|
|
2020-03-05 21:54:21 +00:00
|
|
|
#if QT_CONFIG(concurrent)
|
2022-07-14 11:58:17 +00:00
|
|
|
httpserver.route("/future/", [] (int id) {
|
|
|
|
return QtConcurrent::run([id] () -> QHttpServerResponse {
|
|
|
|
if (id == 0)
|
|
|
|
return QHttpServerResponse::StatusCode::NotFound;
|
2020-03-05 21:54:21 +00:00
|
|
|
|
|
|
|
QTest::qSleep(500);
|
|
|
|
return QHttpServerResponse("future is coming");
|
|
|
|
});
|
|
|
|
});
|
2022-08-18 13:57:33 +00:00
|
|
|
|
|
|
|
httpserver.route("/user/<arg>/delayed/", [](const QString &user, unsigned long delayMs) {
|
|
|
|
return QtConcurrent::run([user, delayMs]() -> QHttpServerResponse {
|
|
|
|
QThread::msleep(delayMs);
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
});
|
2020-03-05 21:54:21 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-11 20:40:27 +00:00
|
|
|
quint16 port = httpserver.listen();
|
|
|
|
if (!port)
|
2022-06-23 09:52:37 +00:00
|
|
|
qCritical("Http server listen failed");
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
urlBase = QStringLiteral("http://localhost:%1%2").arg(port);
|
|
|
|
|
|
|
|
#if QT_CONFIG(ssl)
|
|
|
|
httpserver.sslSetup(QSslCertificate(g_certificate),
|
|
|
|
QSslKey(g_privateKey, QSsl::Rsa));
|
|
|
|
|
|
|
|
port = httpserver.listen();
|
2019-11-11 20:40:27 +00:00
|
|
|
if (!port)
|
2022-06-23 09:52:37 +00:00
|
|
|
qCritical("Http server listen failed");
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
sslUrlBase = QStringLiteral("https://localhost:%1%2").arg(port);
|
|
|
|
|
2022-02-10 16:42:51 +00:00
|
|
|
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)),
|
|
|
|
};
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
connect(&networkAccessManager, &QNetworkAccessManager::sslErrors,
|
|
|
|
[expectedSslErrors](QNetworkReply *reply,
|
|
|
|
const QList<QSslError> &errors) {
|
|
|
|
for (const auto &error: errors) {
|
2022-02-10 17:07:40 +00:00
|
|
|
if (!expectedSslErrors.contains(error))
|
|
|
|
qCritical() << "Got unexpected ssl error:" << error << error.certificate();
|
2019-09-26 12:27:07 +00:00
|
|
|
}
|
|
|
|
reply->ignoreSslErrors(expectedSslErrors);
|
|
|
|
});
|
|
|
|
#endif
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "Hello world get";
|
|
|
|
|
|
|
|
QTest::addRow("test msg")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/test")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
|
|
|
<< "text/html"
|
|
|
|
<< "test msg";
|
|
|
|
|
|
|
|
QTest::addRow("not found")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/not-found")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 404
|
2019-08-08 18:45:51 +00:00
|
|
|
<< "application/x-empty"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("arg:int")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/page/10")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "page: 10";
|
|
|
|
|
|
|
|
QTest::addRow("arg:-int")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/page/-10")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "page: -10";
|
|
|
|
|
|
|
|
QTest::addRow("arg:uint")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/page/10/detail")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "page: 10 detail";
|
|
|
|
|
|
|
|
QTest::addRow("arg:-uint")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/page/-10/detail")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 404
|
2019-08-08 18:45:51 +00:00
|
|
|
<< "application/x-empty"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("arg:string")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/user/test")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "test";
|
|
|
|
|
|
|
|
QTest::addRow("arg:string")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/user/test test ,!a+.")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "test test ,!a+.";
|
|
|
|
|
|
|
|
QTest::addRow("arg:string,ba")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/user/james/bond")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "james-bond";
|
|
|
|
|
|
|
|
QTest::addRow("arg:url")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/test/api/v0/cmds?val=1")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "path: api/v0/cmds";
|
|
|
|
|
|
|
|
QTest::addRow("arg:float 5.1")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v5.1")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "api 5.1v";
|
|
|
|
|
|
|
|
QTest::addRow("arg:float 5.")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v5.")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "api 5v";
|
|
|
|
|
|
|
|
QTest::addRow("arg:float 6.0")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v6.0")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "api 6v";
|
|
|
|
|
|
|
|
QTest::addRow("arg:float,uint")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v5.1/user/10")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "api 5.1v, user id - 10";
|
|
|
|
|
|
|
|
QTest::addRow("arg:float,uint,query")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v5.2/user/11/settings?role=admin")
|
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "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")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/api/v5.2/user/11/settings?role=admin#tag")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "api 5.2v, user id - 11, set settings role=admin#''";
|
|
|
|
|
|
|
|
QTest::addRow("custom route rule")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/custom/15")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 404
|
2019-08-08 18:45:51 +00:00
|
|
|
<< "application/x-empty"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("custom route rule + query")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/custom/10?key=11&g=1")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "Custom router rule: 10, key=11";
|
|
|
|
|
|
|
|
QTest::addRow("custom route rule + query key req")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/custom/10?g=1&key=12")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< "Custom router rule: 10, key=12";
|
2019-01-23 12:34:57 +00:00
|
|
|
|
2019-03-12 14:26:33 +00:00
|
|
|
QTest::addRow("post-and-get, get")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/post-and-get")
|
2019-03-12 14:26:33 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-03-12 14:26:33 +00:00
|
|
|
<< "Hello world get";
|
|
|
|
|
|
|
|
QTest::addRow("invalid-rule-method, get")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/invalid-rule-method")
|
2019-03-12 14:26:33 +00:00
|
|
|
<< 404
|
2019-08-08 18:45:51 +00:00
|
|
|
<< "application/x-empty"
|
2019-03-12 14:26:33 +00:00
|
|
|
<< "";
|
|
|
|
|
2019-01-23 12:34:57 +00:00
|
|
|
QTest::addRow("check custom type, data=1")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/check-custom-type/1")
|
2019-01-23 12:34:57 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-01-23 12:34:57 +00:00
|
|
|
<< "data = 1";
|
|
|
|
|
2019-03-12 14:26:33 +00:00
|
|
|
QTest::addRow("any, get")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/any")
|
2019-03-12 14:26:33 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-03-12 14:26:33 +00:00
|
|
|
<< "Get";
|
2019-04-27 06:54:53 +00:00
|
|
|
|
|
|
|
QTest::addRow("response from html file")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/file/text.html")
|
2019-04-27 06:54:53 +00:00
|
|
|
<< 200
|
|
|
|
<< "text/html"
|
|
|
|
<< "<html></html>";
|
|
|
|
|
|
|
|
QTest::addRow("response from json file")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/file/application.json")
|
2019-04-27 06:54:53 +00:00
|
|
|
<< 200
|
|
|
|
<< "application/json"
|
|
|
|
<< "{ \"key\": \"value\" }";
|
2019-07-06 13:00:17 +00:00
|
|
|
|
|
|
|
QTest::addRow("json-object")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/json-object/")
|
2019-07-06 13:00:17 +00:00
|
|
|
<< 200
|
|
|
|
<< "application/json"
|
|
|
|
<< "{\"property\":\"test\",\"value\":1}";
|
2019-07-06 12:44:56 +00:00
|
|
|
|
|
|
|
QTest::addRow("json-array")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/json-array/")
|
2019-07-06 12:44:56 +00:00
|
|
|
<< 200
|
|
|
|
<< "application/json"
|
|
|
|
<< "[1,\"2\",{\"name\":\"test\"}]";
|
2019-07-18 19:30:10 +00:00
|
|
|
|
2022-08-05 10:46:02 +00:00
|
|
|
QTest::addRow("data-and-custom-status-code")
|
|
|
|
<< urlBase.arg("/data-and-custom-status-code/") << 202 << "application/json"
|
|
|
|
<< "{\"key\":\"value\"}";
|
|
|
|
|
2019-07-18 19:30:10 +00:00
|
|
|
QTest::addRow("chunked")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/chunked/")
|
2019-07-18 19:30:10 +00:00
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "part 1 of the message, part 2 of the message";
|
2019-09-26 12:27:07 +00:00
|
|
|
|
2020-03-05 21:54:21 +00:00
|
|
|
#if QT_CONFIG(concurrent)
|
|
|
|
QTest::addRow("future")
|
|
|
|
<< urlBase.arg("/future/1")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "future is coming";
|
|
|
|
|
|
|
|
QTest::addRow("future-not-found")
|
|
|
|
<< urlBase.arg("/future/0")
|
|
|
|
<< 404
|
|
|
|
<< "application/x-empty"
|
|
|
|
<< "";
|
|
|
|
#endif
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
#if QT_CONFIG(ssl)
|
|
|
|
|
|
|
|
QTest::addRow("hello world, ssl")
|
|
|
|
<< sslUrlBase.arg("/")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "Hello world get";
|
|
|
|
|
|
|
|
QTest::addRow("post-and-get, get, ssl")
|
|
|
|
<< sslUrlBase.arg("/post-and-get")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "Hello world get";
|
|
|
|
|
|
|
|
QTest::addRow("invalid-rule-method, get, ssl")
|
|
|
|
<< sslUrlBase.arg("/invalid-rule-method")
|
|
|
|
<< 404
|
|
|
|
<< "application/x-empty"
|
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("check custom type, data=1, ssl")
|
|
|
|
<< sslUrlBase.arg("/check-custom-type/1")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "data = 1";
|
|
|
|
|
|
|
|
#endif // QT_CONFIG(ssl)
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QHttpServer::routeGet()
|
|
|
|
{
|
|
|
|
QFETCH(QString, url);
|
|
|
|
QFETCH(int, code);
|
|
|
|
QFETCH(QString, type);
|
|
|
|
QFETCH(QString, body);
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
auto reply = networkAccessManager.get(QNetworkRequest(url));
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
|
2019-04-27 06:54:53 +00:00
|
|
|
QCOMPARE(reply->readAll().trimmed(), body);
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 15:09:50 +00:00
|
|
|
void tst_QHttpServer::routeKeepAlive()
|
|
|
|
{
|
|
|
|
httpserver.route("/keep-alive", [] (const QHttpServerRequest &req) -> QHttpServerResponse {
|
2022-07-29 12:51:39 +00:00
|
|
|
if (!req.value("Connection").contains("keep-alive"))
|
2019-03-25 15:09:50 +00:00
|
|
|
return QHttpServerResponse::StatusCode::NotFound;
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
|
|
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");
|
2022-08-08 14:26:33 +00:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
|
2019-03-25 15:09:50 +00:00
|
|
|
|
|
|
|
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"));
|
2022-08-08 14:26:33 +00:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
|
2019-03-25 15:09:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
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")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/")
|
2018-10-30 15:23:23 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2018-10-30 15:23:23 +00:00
|
|
|
<< ""
|
|
|
|
<< "Hello world post";
|
2019-03-30 09:48:47 +00:00
|
|
|
|
|
|
|
QTest::addRow("post-and-get, post")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/post-and-get")
|
2019-03-30 09:48:47 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-03-30 09:48:47 +00:00
|
|
|
<< ""
|
|
|
|
<< "Hello world post";
|
|
|
|
|
|
|
|
QTest::addRow("any, post")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/any")
|
2019-03-30 09:48:47 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-03-30 09:48:47 +00:00
|
|
|
<< ""
|
|
|
|
<< "Post";
|
|
|
|
|
|
|
|
QTest::addRow("post-body")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/post-body")
|
2019-03-30 09:48:47 +00:00
|
|
|
<< 200
|
2019-04-12 17:57:22 +00:00
|
|
|
<< "text/plain"
|
2019-03-30 09:48:47 +00:00
|
|
|
<< "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")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/post-body")
|
2019-03-30 09:48:47 +00:00
|
|
|
<< 200
|
2019-04-12 17:57:22 +00:00
|
|
|
<< "text/plain"
|
2019-03-30 09:48:47 +00:00
|
|
|
<< body
|
|
|
|
<< body;
|
2019-09-26 12:27:07 +00:00
|
|
|
|
2019-09-01 19:33:22 +00:00
|
|
|
QTest::addRow("req-and-resp")
|
|
|
|
<< urlBase.arg("/req-and-resp")
|
|
|
|
<< 200
|
|
|
|
<< "text/html"
|
|
|
|
<< "test"
|
|
|
|
<< "test";
|
|
|
|
|
|
|
|
QTest::addRow("resp-and-req")
|
|
|
|
<< urlBase.arg("/resp-and-req")
|
|
|
|
<< 200
|
|
|
|
<< "text/html"
|
|
|
|
<< "test"
|
|
|
|
<< "test";
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
#if QT_CONFIG(ssl)
|
|
|
|
|
|
|
|
QTest::addRow("post-and-get, post, ssl")
|
|
|
|
<< sslUrlBase.arg("/post-and-get")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< ""
|
|
|
|
<< "Hello world post";
|
|
|
|
|
|
|
|
QTest::addRow("any, post, ssl")
|
|
|
|
<< sslUrlBase.arg("/any")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< ""
|
|
|
|
<< "Post";
|
|
|
|
|
|
|
|
QTest::addRow("post-body, ssl")
|
|
|
|
<< sslUrlBase.arg("/post-body")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "some post data"
|
|
|
|
<< "some post data";
|
|
|
|
|
|
|
|
QTest::addRow("post-body - huge body, chunk test, ssl")
|
|
|
|
<< sslUrlBase.arg("/post-body")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< body
|
|
|
|
<< body;
|
|
|
|
|
|
|
|
#endif // QT_CONFIG(ssl)
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QHttpServer::routePost()
|
|
|
|
{
|
|
|
|
QFETCH(QString, url);
|
|
|
|
QFETCH(int, code);
|
|
|
|
QFETCH(QString, type);
|
|
|
|
QFETCH(QString, data);
|
|
|
|
QFETCH(QString, body);
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
QNetworkRequest request(url);
|
2022-08-08 14:26:33 +00:00
|
|
|
if (data.size())
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html"_ba);
|
2019-03-30 09:48:47 +00:00
|
|
|
|
|
|
|
auto reply = networkAccessManager.post(request, data.toUtf8());
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
|
|
|
|
QCOMPARE(reply->readAll(), body);
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 14:26:33 +00:00
|
|
|
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")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/post-and-get")
|
2019-03-12 14:26:33 +00:00
|
|
|
<< 404
|
2019-08-08 18:45:51 +00:00
|
|
|
<< "application/x-empty"
|
2019-03-12 14:26:33 +00:00
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("any, delete")
|
2019-09-26 12:27:07 +00:00
|
|
|
<< urlBase.arg("/any")
|
2019-03-12 14:26:33 +00:00
|
|
|
<< 200
|
2019-05-02 14:59:47 +00:00
|
|
|
<< "text/plain"
|
2019-03-12 14:26:33 +00:00
|
|
|
<< "Delete";
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
#if QT_CONFIG(ssl)
|
|
|
|
|
|
|
|
QTest::addRow("post-and-get, delete, ssl")
|
|
|
|
<< sslUrlBase.arg("/post-and-get")
|
|
|
|
<< 404
|
|
|
|
<< "application/x-empty"
|
|
|
|
<< "";
|
|
|
|
|
|
|
|
QTest::addRow("any, delete, ssl")
|
|
|
|
<< sslUrlBase.arg("/any")
|
|
|
|
<< 200
|
|
|
|
<< "text/plain"
|
|
|
|
<< "Delete";
|
|
|
|
|
|
|
|
#endif // QT_CONFIG(ssl)
|
2019-03-12 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QHttpServer::routeDelete()
|
|
|
|
{
|
|
|
|
QFETCH(QString, url);
|
|
|
|
QFETCH(int, code);
|
|
|
|
QFETCH(QString, type);
|
|
|
|
QFETCH(QString, data);
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
auto reply = networkAccessManager.deleteResource(QNetworkRequest(url));
|
2019-03-12 14:26:33 +00:00
|
|
|
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
2019-03-12 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 18:52:31 +00:00
|
|
|
void tst_QHttpServer::routeExtraHeaders()
|
|
|
|
{
|
|
|
|
const QUrl requestUrl(urlBase.arg("/extra-headers"));
|
|
|
|
auto reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), "application/x-empty");
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
|
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ServerHeader), "test server");
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:34:57 +00:00
|
|
|
struct CustomType {
|
|
|
|
CustomType() {}
|
|
|
|
CustomType(const QString &) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QHttpServer::invalidRouterArguments()
|
|
|
|
{
|
2020-04-28 19:47:07 +00:00
|
|
|
QTest::ignoreMessage(QtWarningMsg, "Can not find converter for type: QDateTime");
|
2019-01-23 12:34:57 +00:00
|
|
|
QCOMPARE(
|
|
|
|
httpserver.route("/datetime/", [] (const QDateTime &datetime) {
|
|
|
|
return QString("datetime: %1").arg(datetime.toString());
|
|
|
|
}),
|
|
|
|
false);
|
|
|
|
|
2020-04-28 19:47:07 +00:00
|
|
|
QTest::ignoreMessage(QtWarningMsg,
|
|
|
|
"CustomType has not registered a converter to QString. "
|
|
|
|
"Use QHttpServerRouter::addConveter<Type>(converter).");
|
2019-01-23 12:34:57 +00:00
|
|
|
QCOMPARE(
|
|
|
|
httpserver.route("/implicit-conversion-to-qstring-has-no-registered/",
|
|
|
|
[] (const CustomType &) {
|
|
|
|
return "";
|
|
|
|
}),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2019-05-03 13:19:00 +00:00
|
|
|
void tst_QHttpServer::checkRouteLambdaCapture()
|
|
|
|
{
|
|
|
|
httpserver.route("/capture-this/", [this] () {
|
|
|
|
return urlBase;
|
|
|
|
});
|
|
|
|
|
|
|
|
QString msg = urlBase + "/pod";
|
|
|
|
httpserver.route("/capture-non-pod-data/", [&msg] () {
|
|
|
|
return msg;
|
|
|
|
});
|
|
|
|
|
2019-09-26 12:27:07 +00:00
|
|
|
checkReply(networkAccessManager.get(
|
|
|
|
QNetworkRequest(QUrl(urlBase.arg("/capture-this/")))),
|
2019-05-03 13:19:00 +00:00
|
|
|
urlBase);
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
checkReply(networkAccessManager.get(
|
|
|
|
QNetworkRequest(QUrl(urlBase.arg("/capture-non-pod-data/")))),
|
|
|
|
msg);
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:06:00 +00:00
|
|
|
void tst_QHttpServer::afterRequest()
|
|
|
|
{
|
|
|
|
httpserver.afterRequest([] (QHttpServerResponse &&resp,
|
|
|
|
const QHttpServerRequest &request) {
|
|
|
|
if (request.url().path() == "/test-after-request")
|
|
|
|
resp.setHeader("Arguments-Order-1", "resp, request");
|
|
|
|
|
|
|
|
return std::move(resp);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpserver.afterRequest([] (const QHttpServerRequest &request,
|
|
|
|
QHttpServerResponse &&resp) {
|
|
|
|
if (request.url().path() == "/test-after-request")
|
|
|
|
resp.setHeader("Arguments-Order-2", "request, resp");
|
|
|
|
|
|
|
|
return std::move(resp);
|
|
|
|
});
|
|
|
|
|
|
|
|
const QUrl requestUrl(urlBase.arg("/test-after-request"));
|
|
|
|
auto reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
|
|
|
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");
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2019-05-03 13:19:00 +00:00
|
|
|
void tst_QHttpServer::checkReply(QNetworkReply *reply, const QString &response) {
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
|
2020-12-14 15:55:30 +00:00
|
|
|
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(), "text/plain");
|
2019-05-03 13:19:00 +00:00
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
|
|
|
|
QCOMPARE(reply->readAll(), response);
|
2019-09-26 12:27:07 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
2019-05-03 13:19:00 +00:00
|
|
|
};
|
|
|
|
|
2020-06-04 21:53:12 +00:00
|
|
|
void tst_QHttpServer::disconnectedInEventLoop()
|
|
|
|
{
|
|
|
|
httpserver.route("/event-loop/", [] () {
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer::singleShot(1000, &loop, &QEventLoop::quit);
|
|
|
|
loop.exec();
|
|
|
|
return QHttpServerResponse::StatusCode::Ok;
|
|
|
|
});
|
|
|
|
|
|
|
|
const QUrl requestUrl(urlBase.arg("/event-loop/"));
|
|
|
|
auto reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
QTimer::singleShot(500, reply, &QNetworkReply::abort); // cancel connection
|
|
|
|
QEventLoop loop;
|
|
|
|
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
|
|
loop.exec();
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2022-07-14 09:04:02 +00:00
|
|
|
void tst_QHttpServer::multipleRequests()
|
|
|
|
{
|
|
|
|
// Test to ensure that the passed lambda is not moved away after the
|
|
|
|
// first handled request
|
|
|
|
httpserver.route("/do-not-move", [v = std::vector<int>{1, 2, 3}] () {
|
|
|
|
return QString::number(v.size());
|
|
|
|
});
|
|
|
|
|
|
|
|
checkReply(networkAccessManager.get(
|
|
|
|
QNetworkRequest(QUrl(urlBase.arg("/do-not-move")))),
|
|
|
|
"3");
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
checkReply(networkAccessManager.get(
|
|
|
|
QNetworkRequest(QUrl(urlBase.arg("/do-not-move")))),
|
|
|
|
"3");
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:15:29 +00:00
|
|
|
void tst_QHttpServer::pipelinedRequests()
|
|
|
|
{
|
|
|
|
QNetworkReply *replies[10];
|
|
|
|
|
|
|
|
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);
|
|
|
|
replies[i] = networkAccessManager.get(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < std::size(replies); i++)
|
|
|
|
checkReply(replies[i], QString::number(i));
|
|
|
|
}
|
|
|
|
|
2022-08-19 14:28:46 +00:00
|
|
|
void tst_QHttpServer::missingHandler()
|
|
|
|
{
|
|
|
|
const QUrl requestUrl(urlBase.arg("/missing"));
|
|
|
|
auto reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto guard = QScopeGuard([this]() { httpserver.setMissingHandler({}); });
|
|
|
|
|
|
|
|
httpserver.setMissingHandler(
|
|
|
|
[](const QHttpServerRequest &, QHttpServerResponder &&responder) {
|
|
|
|
responder.write(QHttpServerResponder::StatusCode::Ok);
|
|
|
|
});
|
|
|
|
|
|
|
|
reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = networkAccessManager.get(QNetworkRequest(requestUrl));
|
|
|
|
QTRY_VERIFY(reply->isFinished());
|
|
|
|
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 404);
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2022-08-18 13:57:33 +00:00
|
|
|
#if QT_CONFIG(concurrent)
|
|
|
|
// Test that responses to pipelined requests come in correct order, see also: QTBUG-105202
|
|
|
|
void tst_QHttpServer::pipelinedFutureRequests()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
replies[i] = networkAccessManager.get(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < replies.size(); i++)
|
|
|
|
checkReply(replies[i], QString::number(i));
|
|
|
|
}
|
|
|
|
#endif // QT_CONFIG(concurrent)
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2019-01-23 12:34:57 +00:00
|
|
|
Q_DECLARE_METATYPE(CustomArg);
|
|
|
|
Q_DECLARE_METATYPE(CustomType);
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
QTEST_MAIN(tst_QHttpServer)
|
|
|
|
|
|
|
|
#include "tst_qhttpserver.moc"
|