From 53d9f99a08d624b2e468569e4efeab28a95353df Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 18 Jun 2024 23:06:55 +0200 Subject: [PATCH] Port tst_QFormDataBuilder to new style [5/6]: specifyMimeType() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit 9bcf320fc1d3b484a39d692fd2c69c96f05e63e4) Reviewed-by: Qt Cherry-pick Bot --- .../qformdatabuilder/tst_qformdatabuilder.cpp | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp b/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp index 9ccdbcf293e..65dd4fb2369 100644 --- a/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp +++ b/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp @@ -361,15 +361,15 @@ void tst_QFormDataBuilder::specifyMimeType_data() QTest::addColumn("name_data"); QTest::addColumn("body_name_data"); QTest::addColumn("mime_type"); - QTest::addColumn("expected_content_type_data"); + QTest::addColumn("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()