From df6a26d9f0df8358077a6a99a3c3a033affc98b3 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 [3/6]: picksUtf8FilenameEncodingIfAsciiDontSuffice() 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 picksUtf8FilenameEncodingIfAsciiDontSuffice(), too. Requires to actually open the QBuffer and to revert the needles from QString back to QByteArray. Task-number: QTBUG-125985 Change-Id: I13c41e8df86296ae8652c460039798eb529d4628 Reviewed-by: Juha Vuolle (cherry picked from commit 2541f1945d2a64de7b2c13465a178ef70d96da89) Reviewed-by: Qt Cherry-pick Bot --- .../qformdatabuilder/tst_qformdatabuilder.cpp | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp b/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp index 9ec67bed942..c6bcdf50cca 100644 --- a/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp +++ b/tests/auto/network/access/qformdatabuilder/tst_qformdatabuilder.cpp @@ -222,58 +222,59 @@ void tst_QFormDataBuilder::picksUtf8FilenameEncodingIfAsciiDontSuffice_data() { QTest::addColumn("name_data"); QTest::addColumn("body_name_data"); - QTest::addColumn("expected_content_type_data"); - QTest::addColumn("expected_content_disposition_data"); - QTest::addColumn("content_disposition_must_not_contain_data"); + QTest::addColumn("expected_content_type_data"); + QTest::addColumn("expected_content_disposition_data"); + QTest::addColumn("content_disposition_must_not_contain_data"); - QTest::newRow("latin1-ascii") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << u"text/plain"_s - << uR"(form-data; name="text"; filename="rfc3252.txt")"_s - << u"filename*"_s; - QTest::newRow("u8-ascii") << "text"_L1 << QAnyStringView(u8"rfc3252.txt") << u"text/plain"_s - << uR"(form-data; name="text"; filename="rfc3252.txt")"_s - << u"filename*"_s; - QTest::newRow("u-ascii") << "text"_L1 << QAnyStringView(u"rfc3252.txt") << u"text/plain"_s - << uR"(form-data; name="text"; filename="rfc3252.txt")"_s - << u"filename*"_s; + QTest::newRow("latin1-ascii") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text/plain"_ba + << R"(form-data; name="text"; filename="rfc3252.txt")"_ba + << "filename*"_ba; + QTest::newRow("u8-ascii") << "text"_L1 << QAnyStringView(u8"rfc3252.txt") << "text/plain"_ba + << R"(form-data; name="text"; filename="rfc3252.txt")"_ba + << "filename*"_ba; + QTest::newRow("u-ascii") << "text"_L1 << QAnyStringView(u"rfc3252.txt") << "text/plain"_ba + << R"(form-data; name="text"; filename="rfc3252.txt")"_ba + << "filename*"_ba; // 0xF6 is 'ö', use hex value with Latin-1 to avoid interpretation as UTF-8 (0xC3 0xB6) - QTest::newRow("latin1-latin") << "text"_L1 << QAnyStringView("sz\xF6veg.txt"_L1) << u"text/plain"_s - << uR"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_s - << u""_s; - QTest::newRow("u8-latin") << "text"_L1 << QAnyStringView(u8"szöveg.txt") << u"text/plain"_s - << uR"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_s - << u""_s; - QTest::newRow("u-latin") << "text"_L1 << QAnyStringView(u"szöveg.txt") << u"text/plain"_s - << uR"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_s - << u""_s; + QTest::newRow("latin1-latin") << "text"_L1 << QAnyStringView("sz\xF6veg.txt"_L1) << "text/plain"_ba + << R"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_ba + << ""_ba; + QTest::newRow("u8-latin") << "text"_L1 << QAnyStringView(u8"szöveg.txt") << "text/plain"_ba + << R"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_ba + << ""_ba; + QTest::newRow("u-latin") << "text"_L1 << QAnyStringView(u"szöveg.txt") << "text/plain"_ba + << R"(form-data; name="text"; filename="szöveg.txt"; filename*=UTF-8''sz%C3%B6veg.txt)"_ba + << ""_ba; - QTest::newRow("u8-u8") << "text"_L1 << QAnyStringView(u8"テキスト.txt") << u"text/plain"_s - << uR"(form-data; name="text"; filename="テキスト.txt"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt)"_s - << u""_s; + QTest::newRow("u8-u8") << "text"_L1 << QAnyStringView(u8"テキスト.txt") << "text/plain"_ba + << R"(form-data; name="text"; filename="テキスト.txt"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt)"_ba + << ""_ba; } void tst_QFormDataBuilder::picksUtf8FilenameEncodingIfAsciiDontSuffice() { QFETCH(const QLatin1StringView, name_data); QFETCH(const QAnyStringView, body_name_data); - QFETCH(const QString, expected_content_type_data); - QFETCH(const QString, expected_content_disposition_data); - QFETCH(const QString, content_disposition_must_not_contain_data); + QFETCH(const QByteArray, expected_content_type_data); + QFETCH(const QByteArray, expected_content_disposition_data); + QFETCH(const QByteArray, content_disposition_must_not_contain_data); QBuffer buff; - QFormDataBuilder qfdb; - QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&buff, body_name_data); - const QHttpPart httpPart = qfdpb.build(); + QVERIFY(buff.open(QIODevice::ReadOnly)); + + const auto msg = serialized([&](auto &builder) { + builder.part(name_data).setBodyDevice(&buff, body_name_data); + }); - const auto msg = QDebug::toString(httpPart); QVERIFY2(msg.contains(expected_content_type_data), - qPrintable(u"content-type not found : "_s + expected_content_type_data)); + "content-type not found : " + expected_content_type_data); QVERIFY2(msg.contains(expected_content_disposition_data), - qPrintable(u"content-disposition not found : "_s + expected_content_disposition_data)); + "content-disposition not found : " + expected_content_disposition_data); if (!content_disposition_must_not_contain_data.isEmpty()) { QVERIFY2(!msg.contains(content_disposition_must_not_contain_data), - qPrintable(u"content-disposition contained data it shouldn't : "_s - + content_disposition_must_not_contain_data)); + "content-disposition contained data it shouldn't : " + + content_disposition_must_not_contain_data); } }