Port tst_QFormDataBuilder to new style [5/6]: specifyMimeType()

We don't want to have to rely on QDebug:toString() and the private
QFormDataPartBuilder::build() function for checks, so use the
framework originally introduced by Máté for the the moveSemantics()
test for specifyMimeType(), too.

Requires to actually open the QBuffer and to revert the needles from
QString back to QByteArray.

Also anchor the needle between two CRLFs, because they each represent
one full header field. This is like anchoring a regex with ^~~~$.

As a drive-by, use QVERIFY2().

Task-number: QTBUG-125985
Change-Id: I8f7830e49d05044ef7388338498c96334a489c90
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
(cherry picked from commit 9bcf320fc1d3b484a39d692fd2c69c96f05e63e4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-06-18 23:06:55 +02:00 committed by Qt Cherry-pick Bot
parent 1fc54987d4
commit 53d9f99a08

View File

@ -361,15 +361,15 @@ void tst_QFormDataBuilder::specifyMimeType_data()
QTest::addColumn<QLatin1StringView>("name_data");
QTest::addColumn<QAnyStringView>("body_name_data");
QTest::addColumn<QAnyStringView>("mime_type");
QTest::addColumn<QString>("expected_content_type_data");
QTest::addColumn<QByteArray>("expected_content_type_data");
QTest::newRow("not-specified") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< QAnyStringView("text/plain"_L1) << uR"("content-type":"text/plain")"_s;
<< QAnyStringView("text/plain"_L1) << "content-type: text/plain"_ba;
QTest::newRow("mime-specified") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< QAnyStringView("text/plain"_L1) << uR"("content-type":"text/plain")"_s;
<< QAnyStringView("text/plain"_L1) << "content-type: text/plain"_ba;
// wrong mime type specified but it is not overridden by the deduction
QTest::newRow("wrong-mime-specified") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< QAnyStringView("image/jpeg"_L1) << uR"("content-type":"image/jpeg)"_s;
<< QAnyStringView("image/jpeg"_L1) << "content-type: image/jpeg"_ba;
}
void tst_QFormDataBuilder::specifyMimeType()
@ -377,22 +377,22 @@ void tst_QFormDataBuilder::specifyMimeType()
QFETCH(const QLatin1StringView, name_data);
QFETCH(const QAnyStringView, body_name_data);
QFETCH(const QAnyStringView, mime_type);
QFETCH(const QString, expected_content_type_data);
QFETCH(const QByteArray, expected_content_type_data);
QBuffer buff;
QVERIFY(buff.open(QIODevice::ReadOnly));
QFormDataBuilder qfdb;
QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&buff, body_name_data);
const auto msg = serialized([&](auto &builder) {
auto &qfdpb = builder.part(name_data).setBodyDevice(&buff, body_name_data);
if (!mime_type.empty())
qfdpb.setBodyDevice(&buff, body_name_data, mime_type);
else
qfdpb.setBodyDevice(&buff, body_name_data);
if (!mime_type.empty())
qfdpb.setBodyDevice(&buff, body_name_data, mime_type);
else
qfdpb.setBodyDevice(&buff, body_name_data);
});
const QHttpPart httpPart = qfdpb.build();
const auto msg = QDebug::toString(httpPart);
QVERIFY(msg.contains(expected_content_type_data));
QVERIFY2(msg.contains(CRLF + expected_content_type_data + CRLF),
msg + " does not contain " + expected_content_type_data);
}
void tst_QFormDataBuilder::picksUtf8NameEncodingIfAsciiDoesNotSuffice_data()