tests: Use _ba instead of QByteArrayLiteral

Pick-to: 6.4
Change-Id: I8d41e169cd3dd489d8de54aeed052ff6a9e3edc3
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Ievgenii Meshcheriakov 2022-08-08 16:11:26 +02:00
parent 0c697c01c7
commit 2cd9c66a25
3 changed files with 33 additions and 29 deletions

View File

@ -171,22 +171,22 @@ void tst_QAbstractHttpServer::servers()
void tst_QAbstractHttpServer::fork()
{
#if defined(Q_OS_UNIX)
const auto message = QByteArrayLiteral("Hello world!");
const auto message = "Hello world!"_ba;
struct HttpServer : QAbstractHttpServer
{
const QByteArray &message;
HttpServer(const QByteArray &message) : message(message) {}
bool handleRequest(const QHttpServerRequest &, QTcpSocket *socket) override
{
socket->write(QByteArrayLiteral("HTTP/1.1 200 OK"));
socket->write(QByteArrayLiteral("\r\n"));
socket->write(QByteArrayLiteral("Content-Length: "));
socket->write("HTTP/1.1 200 OK"_ba);
socket->write("\r\n"_ba);
socket->write("Content-Length: "_ba);
socket->write(QByteArray::number(message.size()));
socket->write(QByteArrayLiteral("\r\n"));
socket->write(QByteArrayLiteral("Connection: close"));
socket->write(QByteArrayLiteral("\r\n"));
socket->write(QByteArrayLiteral("Content-Type: text/html"));
socket->write(QByteArrayLiteral("\r\n\r\n"));
socket->write("\r\n"_ba);
socket->write("Connection: close"_ba);
socket->write("\r\n"_ba);
socket->write("Content-Type: text/html"_ba);
socket->write("\r\n\r\n"_ba);
socket->write(message);
socket->flush();
::kill(::getpid(), SIGKILL); // Avoids continuing running tests in the child process

View File

@ -17,8 +17,10 @@
QT_BEGIN_NAMESPACE
static const QByteArray headerServerString(QByteArrayLiteral("Server"));
static const QByteArray headerServerValue(QByteArrayLiteral("Test server"));
using namespace Qt::Literals;
static const QByteArray headerServerString = "Server"_ba;
static const QByteArray headerServerValue = "Test server"_ba;
class tst_QHttpServerResponder : public QObject
{
@ -80,7 +82,7 @@ void tst_QHttpServerResponder::defaultStatusCodeByteArray()
void tst_QHttpServerResponder::defaultStatusCodeJson()
{
const auto json = QJsonDocument::fromJson(QByteArrayLiteral("{}"));
const auto json = QJsonDocument::fromJson("{}"_ba);
HttpServer server([json](QHttpServerResponder responder) { responder.write(json); });
auto reply = networkAccessManager->get(QNetworkRequest(server.url));
qWaitForFinished(reply);
@ -113,7 +115,7 @@ void tst_QHttpServerResponder::writeStatusCode()
QCOMPARE(reply->bytesAvailable(), 0);
QCOMPARE(reply->error(), networkError);
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(),
QByteArrayLiteral("application/x-empty"));
"application/x-empty"_ba);
}
void tst_QHttpServerResponder::writeStatusCodeExtraHeader()
@ -130,7 +132,7 @@ void tst_QHttpServerResponder::writeStatusCodeExtraHeader()
void tst_QHttpServerResponder::writeJson()
{
const auto json = QJsonDocument::fromJson(QByteArrayLiteral(R"JSON({ "key" : "value" })JSON"));
const auto json = QJsonDocument::fromJson(R"JSON({ "key" : "value" })JSON"_ba);
HttpServer server([json](QHttpServerResponder responder) { responder.write(json); });
auto reply = networkAccessManager->get(QNetworkRequest(server.url));
qWaitForFinished(reply);
@ -142,7 +144,7 @@ void tst_QHttpServerResponder::writeJson()
void tst_QHttpServerResponder::writeJsonExtraHeader()
{
const auto json = QJsonDocument::fromJson(QByteArrayLiteral(R"JSON({ "key" : "value" })JSON"));
const auto json = QJsonDocument::fromJson(R"JSON({ "key" : "value" })JSON"_ba);
HttpServer server([json](QHttpServerResponder responder) {
responder.write(json, {{ headerServerString, headerServerValue }});
});

View File

@ -10,6 +10,8 @@
QT_BEGIN_NAMESPACE
using namespace Qt::Literals;
class tst_QHttpServerResponse : public QObject
{
Q_OBJECT
@ -29,11 +31,11 @@ void tst_QHttpServerResponse::mimeTypeDetection_data()
QTest::addRow("application/x-zerosize")
<< QFINDTESTDATA("data/empty")
<< QByteArrayLiteral("application/x-zerosize");
<< "application/x-zerosize"_ba;
QTest::addRow("text/plain")
<< QFINDTESTDATA("data/text.plain")
<< QByteArrayLiteral("text/plain");
<< "text/plain"_ba;
QTest::addRow("text/html")
<< QFINDTESTDATA("data/text.html")
@ -41,15 +43,15 @@ void tst_QHttpServerResponse::mimeTypeDetection_data()
QTest::addRow("image/png")
<< QFINDTESTDATA("data/image.png")
<< QByteArrayLiteral("image/png");
<< "image/png"_ba;
QTest::addRow("image/jpeg")
<< QFINDTESTDATA("data/image.jpeg")
<< QByteArrayLiteral("image/jpeg");
<< "image/jpeg"_ba;
QTest::addRow("image/svg+xml")
<< QFINDTESTDATA("data/image.svg")
<< QByteArrayLiteral("image/svg+xml");
<< "image/svg+xml"_ba;
}
void tst_QHttpServerResponse::mimeTypeDetection()
@ -72,11 +74,11 @@ void tst_QHttpServerResponse::mimeTypeDetectionFromFile_data()
QTest::addRow("application/x-zerosize")
<< QFINDTESTDATA("data/empty")
<< QByteArrayLiteral("application/x-zerosize");
<< "application/x-zerosize"_ba;
QTest::addRow("text/plain")
<< QFINDTESTDATA("data/text.plain")
<< QByteArrayLiteral("text/plain");
<< "text/plain"_ba;
QTest::addRow("text/html")
<< QFINDTESTDATA("data/text.html")
@ -84,19 +86,19 @@ void tst_QHttpServerResponse::mimeTypeDetectionFromFile_data()
QTest::addRow("image/png")
<< QFINDTESTDATA("data/image.png")
<< QByteArrayLiteral("image/png");
<< "image/png"_ba;
QTest::addRow("image/jpeg")
<< QFINDTESTDATA("data/image.jpeg")
<< QByteArrayLiteral("image/jpeg");
<< "image/jpeg"_ba;
QTest::addRow("image/svg+xml")
<< QFINDTESTDATA("data/image.svg")
<< QByteArrayLiteral("image/svg+xml");
<< "image/svg+xml"_ba;
QTest::addRow("application/json")
<< QFINDTESTDATA("data/application.json")
<< QByteArrayLiteral("application/json");
<< "application/json"_ba;
}
void tst_QHttpServerResponse::mimeTypeDetectionFromFile()
@ -111,9 +113,9 @@ void tst_QHttpServerResponse::headers()
{
QHttpServerResponse resp("");
const QByteArray test1 = QByteArrayLiteral("test1");
const QByteArray test2 = QByteArrayLiteral("test2");
const QByteArray zero = QByteArrayLiteral("application/x-zerosize");
const QByteArray test1 = "test1"_ba;
const QByteArray test2 = "test2"_ba;
const QByteArray zero = "application/x-zerosize"_ba;
const auto &contentTypeHeader = QHttpServerLiterals::contentTypeHeader();
const auto &contentLengthHeader = QHttpServerLiterals::contentLengthHeader();