Refactor test cases in tst_qformdatabuilder

Hide the construction details of QFormDataPartBuilder, change
the verification to be QString based instead of QByteArray based,
and use raw string literals (where useful) for readability.

Task-number: QTBUG-114647
Change-Id: I16fb6c943cf6fd7385e78bbffe3b2780ab0a6ac8
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit cb8abb718da4e18232de525ef1bb611f3a5dd929)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mate Barany 2024-06-12 09:24:11 +02:00 committed by Qt Cherry-pick Bot
parent b14c1c1824
commit 0a57b0e542

View File

@ -36,43 +36,42 @@ void tst_QFormDataBuilder::generateQHttpPartWithDevice_data()
QTest::addColumn<QLatin1StringView>("name_data"); QTest::addColumn<QLatin1StringView>("name_data");
QTest::addColumn<QString>("real_file_name"); QTest::addColumn<QString>("real_file_name");
QTest::addColumn<QString>("body_name_data"); QTest::addColumn<QString>("body_name_data");
QTest::addColumn<QByteArray>("expected_content_type_data"); QTest::addColumn<QString>("expected_content_type_data");
QTest::addColumn<QByteArray>("expected_content_disposition_data"); QTest::addColumn<QString>("expected_content_disposition_data");
QTest::newRow("txt-ascii") << "text"_L1 << "rfc3252.txt" << "rfc3252.txt" << "text/plain"_ba QTest::newRow("txt-ascii") << "text"_L1 << u"rfc3252.txt"_s << u"rfc3252.txt"_s << u"text/plain"_s
<< "form-data; name=\"text\"; filename=rfc3252.txt"_ba; << uR"(form-data; name="text"; filename=rfc3252.txt)"_s;
QTest::newRow("txt-latin") << "text"_L1 << "rfc3252.txt" << "szöveg.txt" << "text/plain"_ba QTest::newRow("txt-latin") << "text"_L1 << u"rfc3252.txt"_s << u"szöveg.txt"_s << u"text/plain"_s
<< "form-data; name=\"text\"; filename*=ISO-8859-1''sz%F6veg.txt"_ba; << uR"(form-data; name="text"; filename*=ISO-8859-1''sz%F6veg.txt)"_s;
QTest::newRow("txt-unicode") << "text"_L1 << "rfc3252.txt" << "テキスト.txt" << "text/plain"_ba QTest::newRow("txt-unicode") << "text"_L1 << u"rfc3252.txt"_s << u"テキスト.txt"_s << u"text/plain"_s
<< "form-data; name=\"text\"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt"_ba; << uR"(form-data; name="text"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt)"_s;
QTest::newRow("jpg-ascii") << "image"_L1 << "image1.jpg" << "image1.jpg" << "image/jpeg"_ba QTest::newRow("jpg-ascii") << "image"_L1 << u"image1.jpg"_s << u"image1.jpg"_s << u"image/jpeg"_s
<< "form-data; name=\"image\"; filename=image1.jpg"_ba; << uR"(form-data; name="image"; filename=image1.jpg)"_s;
QTest::newRow("jpg-latin") << "image"_L1 << "image1.jpg" << "kép.jpg" << "image/jpeg"_ba QTest::newRow("jpg-latin") << "image"_L1 << u"image1.jpg"_s << u"kép.jpg"_s << u"image/jpeg"_s
<< "form-data; name=\"image\"; filename*=ISO-8859-1''k%E9p.jpg"_ba; << uR"(form-data; name="image"; filename*=ISO-8859-1''k%E9p.jpg)"_s;
QTest::newRow("jpg-unicode") << "image"_L1 << "image1.jpg" << "絵.jpg" << "image/jpeg"_ba QTest::newRow("jpg-unicode") << "image"_L1 << u"image1.jpg"_s << u"絵.jpg"_s << u"image/jpeg"_s
<< "form-data; name=\"image\"; filename*=UTF-8''%E7%B5%B5"_ba; << uR"(form-data; name="image"; filename*=UTF-8''%E7%B5%B5)"_s;
QTest::newRow("doc-ascii") << "text"_L1 << "document.docx" << "word.docx" QTest::newRow("doc-ascii") << "text"_L1 << u"document.docx"_s << u"word.docx"_s
<< "application/vnd.openxmlformats-officedocument.wordprocessingml.document"_ba << u"application/vnd.openxmlformats-officedocument.wordprocessingml.document"_s
<< "form-data; name=\"text\"; filename=word.docx"_ba; << uR"(form-data; name="text"; filename=word.docx)"_s;
QTest::newRow("doc-latin") << "text"_L1 << "document.docx" << "szöveg.docx" QTest::newRow("doc-latin") << "text"_L1 << u"document.docx"_s << u"szöveg.docx"_s
<< "application/vnd.openxmlformats-officedocument.wordprocessingml.document"_ba << u"application/vnd.openxmlformats-officedocument.wordprocessingml.document"_s
<< "form-data; name=\"text\"; filename*=ISO-8859-1''sz%F6veg.docx"_ba; << uR"(form-data; name="text"; filename*=ISO-8859-1''sz%F6veg.docx)"_s;
QTest::newRow("doc-unicode") << "text"_L1 << "document.docx" << "テキスト.docx" QTest::newRow("doc-unicode") << "text"_L1 << u"document.docx"_s << u"テキスト.docx"_s
<< "application/vnd.openxmlformats-officedocument.wordprocessingml.document"_ba << u"application/vnd.openxmlformats-officedocument.wordprocessingml.document"_s
<< "form-data; name=\"text\"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.docx"_ba; << uR"(form-data; name="text"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.docx)"_s;
QTest::newRow("xls-ascii") << "spreadsheet"_L1 << "sheet.xlsx" << "sheet.xlsx"
<< "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_ba
<< "form-data; name=\"spreadsheet\"; filename=sheet.xlsx"_ba;
QTest::newRow("xls-latin") << "spreadsheet"_L1 << "sheet.xlsx" << "szöveg.xlsx"
<< "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_ba
<< "form-data; name=\"spreadsheet\"; filename*=ISO-8859-1''sz%F6veg.xlsx"_ba;
QTest::newRow("xls-unicode") << "spreadsheet"_L1 << "sheet.xlsx" << "テキスト.xlsx"
<< "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_ba
<< "form-data; name=\"spreadsheet\"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.xlsx"_ba;
QTest::newRow("xls-ascii") << "spreadsheet"_L1 << u"sheet.xlsx"_s << u"sheet.xlsx"_s
<< u"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_s
<< uR"(form-data; name="spreadsheet"; filename=sheet.xlsx)"_s;
QTest::newRow("xls-latin") << "spreadsheet"_L1 << u"sheet.xlsx"_s << u"szöveg.xlsx"_s
<< u"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_s
<< uR"(form-data; name="spreadsheet"; filename*=ISO-8859-1''sz%F6veg.xlsx)"_s;
QTest::newRow("xls-unicode") << "spreadsheet"_L1 << u"sheet.xlsx"_s << u"テキスト.xlsx"_s
<< u"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"_s
<< uR"(form-data; name="spreadsheet"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.xlsx)"_s;
} }
void tst_QFormDataBuilder::generateQHttpPartWithDevice() void tst_QFormDataBuilder::generateQHttpPartWithDevice()
@ -80,24 +79,17 @@ void tst_QFormDataBuilder::generateQHttpPartWithDevice()
QFETCH(const QLatin1StringView, name_data); QFETCH(const QLatin1StringView, name_data);
QFETCH(const QString, real_file_name); QFETCH(const QString, real_file_name);
QFETCH(const QString, body_name_data); QFETCH(const QString, body_name_data);
QFETCH(const QByteArray, expected_content_type_data); QFETCH(const QString, expected_content_type_data);
QFETCH(const QByteArray, expected_content_disposition_data); QFETCH(const QString, expected_content_disposition_data);
QString testData = QFileInfo(QFINDTESTDATA(real_file_name)).absoluteFilePath(); QString testData = QFileInfo(QFINDTESTDATA(real_file_name)).absoluteFilePath();
QFile data_file(testData); QFile data_file(testData);
QHttpPart httpPart = QFormDataPartBuilder(name_data, QFormDataPartBuilder::PrivateConstructor()) QFormDataBuilder qfdb;
.setBodyDevice(&data_file, body_name_data) QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&data_file, body_name_data);
.build(); const QHttpPart httpPart = qfdpb.build();
QByteArray msg;
{
QBuffer buf(&msg);
QVERIFY(buf.open(QIODevice::WriteOnly));
QDebug debug(&buf);
debug << httpPart;
}
const auto msg = QDebug::toString(httpPart);
QVERIFY(msg.contains(expected_content_type_data)); QVERIFY(msg.contains(expected_content_type_data));
QVERIFY(msg.contains(expected_content_disposition_data)); QVERIFY(msg.contains(expected_content_disposition_data));
} }
@ -106,49 +98,42 @@ void tst_QFormDataBuilder::escapesBackslashAndQuotesInFilenameAndName_data()
{ {
QTest::addColumn<QLatin1StringView>("name_data"); QTest::addColumn<QLatin1StringView>("name_data");
QTest::addColumn<QString>("body_name_data"); QTest::addColumn<QString>("body_name_data");
QTest::addColumn<QByteArray>("expected_content_type_data"); QTest::addColumn<QString>("expected_content_type_data");
QTest::addColumn<QByteArray>("expected_content_disposition_data"); QTest::addColumn<QString>("expected_content_disposition_data");
QTest::newRow("quote") << "t\"ext"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("quote") << "t\"ext"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\"ext"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\"ext"; filename=rfc3252.txt)"_s;
QTest::newRow("slash") << "t\\ext"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("slash") << "t\\ext"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\\ext"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\\ext"; filename=rfc3252.txt)"_s;
QTest::newRow("quotes") << "t\"e\"xt"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("quotes") << "t\"e\"xt"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\"e\"xt"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\"e\"xt"; filename=rfc3252.txt)"_s;
QTest::newRow("slashes") << "t\\\\ext"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("slashes") << "t\\\\ext"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\\\\ext"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\\\\ext"; filename=rfc3252.txt)"_s;
QTest::newRow("quote-slash") << "t\"ex\\t"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("quote-slash") << "t\"ex\\t"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\"ex\\t"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\"ex\\t"; filename=rfc3252.txt)"_s;
QTest::newRow("quotes-slashes") << "t\"e\"x\\t\\"_L1 << "rfc3252.txt" << "text/plain"_ba QTest::newRow("quotes-slashes") << "t\"e\"x\\t\\"_L1 << "rfc3252.txt" << u"text/plain"_s
<< R"(form-data; name="t\"e\"x\\t\\"; filename=rfc3252.txt)"_ba; << uR"(form-data; name="t\"e\"x\\t\\"; filename=rfc3252.txt)"_s;
} }
void tst_QFormDataBuilder::escapesBackslashAndQuotesInFilenameAndName() void tst_QFormDataBuilder::escapesBackslashAndQuotesInFilenameAndName()
{ {
QFETCH(const QLatin1StringView, name_data); QFETCH(const QLatin1StringView, name_data);
QFETCH(const QString, body_name_data); QFETCH(const QString, body_name_data);
QFETCH(const QByteArray, expected_content_type_data); QFETCH(const QString, expected_content_type_data);
QFETCH(const QByteArray, expected_content_disposition_data); QFETCH(const QString, expected_content_disposition_data);
QFile dummy_file(body_name_data); QFile dummy_file(body_name_data);
QHttpPart httpPart = QFormDataPartBuilder(name_data, QFormDataPartBuilder::PrivateConstructor()) QFormDataBuilder qfdb;
.setBodyDevice(&dummy_file, body_name_data) QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&dummy_file, body_name_data);
.build(); const QHttpPart httpPart = qfdpb.build();
QByteArray msg;
{
QBuffer buf(&msg);
QVERIFY(buf.open(QIODevice::WriteOnly));
QDebug debug(&buf);
debug << httpPart;
}
const auto msg = QDebug::toString(httpPart);
QVERIFY(msg.contains(expected_content_type_data)); QVERIFY(msg.contains(expected_content_type_data));
QVERIFY(msg.contains(expected_content_disposition_data)); QVERIFY(msg.contains(expected_content_disposition_data));
} }
@ -157,49 +142,41 @@ void tst_QFormDataBuilder::picksUtf8EncodingOnlyIfL1OrAsciiDontSuffice_data()
{ {
QTest::addColumn<QLatin1StringView>("name_data"); QTest::addColumn<QLatin1StringView>("name_data");
QTest::addColumn<QAnyStringView>("body_name_data"); QTest::addColumn<QAnyStringView>("body_name_data");
QTest::addColumn<QByteArray>("expected_content_type_data"); QTest::addColumn<QString>("expected_content_type_data");
QTest::addColumn<QByteArray>("expected_content_disposition_data"); QTest::addColumn<QString>("expected_content_disposition_data");
QTest::newRow("latin1-ascii") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text/plain"_ba QTest::newRow("latin1-ascii") << "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << u"text/plain"_s
<< "form-data; name=\"text\"; filename=rfc3252.txt"_ba; << uR"(form-data; name="text"; filename=rfc3252.txt)"_s;
QTest::newRow("u8-ascii") << "text"_L1 << QAnyStringView(u8"rfc3252.txt") << "text/plain"_ba QTest::newRow("u8-ascii") << "text"_L1 << QAnyStringView(u8"rfc3252.txt") << u"text/plain"_s
<< "form-data; name=\"text\"; filename=rfc3252.txt"_ba; << uR"(form-data; name="text"; filename=rfc3252.txt)"_s;
QTest::newRow("u-ascii") << "text"_L1 << QAnyStringView(u"rfc3252.txt") << "text/plain"_ba QTest::newRow("u-ascii") << "text"_L1 << QAnyStringView(u"rfc3252.txt") << u"text/plain"_s
<< "form-data; name=\"text\"; filename=rfc3252.txt"_ba; << uR"(form-data; name="text"; filename=rfc3252.txt)"_s;
QTest::newRow("latin1-latin") << "text"_L1 << QAnyStringView("sz\366veg.txt"_L1) << u"text/plain"_s
<< uR"(form-data; name="text"; filename*=ISO-8859-1''sz%F6veg.txt)"_s;
QTest::newRow("u8-latin") << "text"_L1 << QAnyStringView(u8"szöveg.txt") << u"text/plain"_s
<< uR"(form-data; name="text"; filename*=ISO-8859-1''sz%F6veg.txt)"_s;
QTest::newRow("u-latin") << "text"_L1 << QAnyStringView(u"szöveg.txt") << u"text/plain"_s
<< uR"(form-data; name="text"; filename*=ISO-8859-1''sz%F6veg.txt)"_s;
QTest::newRow("latin1-latin") << "text"_L1 << QAnyStringView("sz\366veg.txt"_L1) << "text/plain"_ba QTest::newRow("u8-u8") << "text"_L1 << QAnyStringView(u8"テキスト.txt") << u"text/plain"_s
<< "form-data; name=\"text\"; filename*=ISO-8859-1''sz%F6veg.txt"_ba; << uR"(form-data; name="text"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt)"_s;
QTest::newRow("u8-latin") << "text"_L1 << QAnyStringView(u8"szöveg.txt") << "text/plain"_ba
<< "form-data; name=\"text\"; filename*=ISO-8859-1''sz%F6veg.txt"_ba;
QTest::newRow("u-latin") << "text"_L1 << QAnyStringView(u"szöveg.txt") << "text/plain"_ba
<< "form-data; name=\"text\"; filename*=ISO-8859-1''sz%F6veg.txt"_ba;
QTest::newRow("u8-u8") << "text"_L1 << QAnyStringView(u8"テキスト.txt") << "text/plain"_ba
<< "form-data; name=\"text\"; filename*=UTF-8''%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.txt"_ba;
} }
void tst_QFormDataBuilder::picksUtf8EncodingOnlyIfL1OrAsciiDontSuffice() void tst_QFormDataBuilder::picksUtf8EncodingOnlyIfL1OrAsciiDontSuffice()
{ {
QFETCH(const QLatin1StringView, name_data); QFETCH(const QLatin1StringView, name_data);
QFETCH(const QAnyStringView, body_name_data); QFETCH(const QAnyStringView, body_name_data);
QFETCH(const QByteArray, expected_content_type_data); QFETCH(const QString, expected_content_type_data);
QFETCH(const QByteArray, expected_content_disposition_data); QFETCH(const QString, expected_content_disposition_data);
QBuffer buff; QBuffer buff;
QHttpPart httpPart = QFormDataPartBuilder(name_data, QFormDataPartBuilder::PrivateConstructor()) QFormDataBuilder qfdb;
.setBodyDevice(&buff, body_name_data) QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&buff, body_name_data);
.build(); const QHttpPart httpPart = qfdpb.build();
QByteArray msg;
{
QBuffer buf(&msg);
QVERIFY(buf.open(QIODevice::WriteOnly));
QDebug debug(&buf);
debug << httpPart;
}
const auto msg = QDebug::toString(httpPart);
QVERIFY(msg.contains(expected_content_type_data)); QVERIFY(msg.contains(expected_content_type_data));
QVERIFY(msg.contains(expected_content_disposition_data)); QVERIFY(msg.contains(expected_content_disposition_data));
} }