Skip to content

Commit

Permalink
QHttpServerResponse: add ctor for QJsonArray
Browse files Browse the repository at this point in the history
Task-number: QTBUG-76619
Change-Id: Ibce12e33754e950f467c8fb291d447cfc0694062
Reviewed-by: Tasuku Suzuki <[email protected]>
Reviewed-by: Mårten Nordheim <[email protected]>
  • Loading branch information
msvetkin committed Jul 11, 2019
1 parent a8777eb commit b63b086
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/httpserver/qhttpserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ QHttpServerResponse::QHttpServerResponse(const QJsonObject &data)
{
}

QHttpServerResponse::QHttpServerResponse(const QJsonArray &data)
: QHttpServerResponse(mimeApplicationJson,
QJsonDocument(data).toJson(QJsonDocument::Compact))
{
}

QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType,
const QByteArray &data,
const StatusCode status)
Expand Down
1 change: 1 addition & 0 deletions src/httpserver/qhttpserverresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Q_HTTPSERVER_EXPORT QHttpServerResponse
QHttpServerResponse(const QString &data);
explicit QHttpServerResponse(const QByteArray &data);
QHttpServerResponse(const QJsonObject &data);
QHttpServerResponse(const QJsonArray &data);
QHttpServerResponse(const QByteArray &mimeType,
const QByteArray &data,
const StatusCode status = StatusCode::Ok);
Expand Down
16 changes: 16 additions & 0 deletions tests/auto/qhttpserver/tst_qhttpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <QtCore/qmetaobject.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qjsonarray.h>

#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
Expand Down Expand Up @@ -205,6 +206,15 @@ void tst_QHttpServer::initTestCase()
};
});

httpserver.route("/json-array/", [] () {
return QJsonArray{
1, "2",
QJsonObject{
{"name", "test"}
}
};
});

urlBase = QStringLiteral("http:https://localhost:%1%2").arg(httpserver.listen());
}

Expand Down Expand Up @@ -375,6 +385,12 @@ void tst_QHttpServer::routeGet_data()
<< 200
<< "application/json"
<< "{\"property\":\"test\",\"value\":1}";

QTest::addRow("json-array")
<< "/json-array/"
<< 200
<< "application/json"
<< "[1,\"2\",{\"name\":\"test\"}]";
}

void tst_QHttpServer::routeGet()
Expand Down

0 comments on commit b63b086

Please sign in to comment.