Port tst_QFormDataBuilder to new style [4/6]: setHeadersDoesNotAffectHeaderFieldsManagedByBuilder()

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 setHeadersDoesNotAffectHeaderFieldsManagedByBuilder(), too.

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

Also anchor the needles with a CRLF on each side, because they each
represent one full header field. This is like anchoring a regex using
^~~~$.

As a drive-by, fix indentation and brace placement of initializer_list
QList consruction and rely on CTAD to deduce QByteArrayList from QList{}.

Task-number: QTBUG-125985
Change-Id: I405b5d67212e906a3b914d9e5b815835bdee5bc6
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
(cherry picked from commit ab859d5f96855eeec50f3b39c925203ba7def73f)
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 a8f7a59b07
commit 1fc54987d4

View File

@ -286,37 +286,41 @@ void tst_QFormDataBuilder::setHeadersDoesNotAffectHeaderFieldsManagedByBuilder_d
QTest::addColumn<QAnyStringView>("body_name_data"); QTest::addColumn<QAnyStringView>("body_name_data");
QTest::addColumn<bool>("overwrite"); QTest::addColumn<bool>("overwrite");
QTest::addColumn<bool>("extra_headers"); QTest::addColumn<bool>("extra_headers");
QTest::addColumn<QStringList>("expected_headers"); QTest::addColumn<QByteArrayList>("expected_headers");
QTest::newRow("content-disposition-is-set-by-default") QTest::newRow("content-disposition-is-set-by-default")
<< "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< false << false << false << false
<< QStringList{ << QList{
uR"("content-disposition":"form-data; name=\"text\"; filename=\"rfc3252.txt\"")"_s, R"(content-disposition: form-data; name="text"; filename="rfc3252.txt")"_ba ,
uR"("content-type":"text/plain")"_s}; "content-type: text/plain"_ba,
};
QTest::newRow("default-overwrites-preset-content-disposition") QTest::newRow("default-overwrites-preset-content-disposition")
<< "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< true << false << true << false
<< QStringList{ << QList{
uR"("content-disposition":"form-data; name=\"text\"; filename=\"rfc3252.txt\"")"_s, R"(content-disposition: form-data; name="text"; filename="rfc3252.txt")"_ba ,
uR"("content-type":"text/plain")"_s}; "content-type: text/plain"_ba,
};
QTest::newRow("added-extra-header") QTest::newRow("added-extra-header")
<< "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< false << true << false << true
<< QStringList{ << QList{
uR"("content-disposition":"form-data; name=\"text\"; filename=\"rfc3252.txt\"")"_s, R"(content-disposition: form-data; name="text"; filename="rfc3252.txt")"_ba ,
uR"("content-type":"text/plain")"_s, "content-type: text/plain"_ba,
uR"("content-length":"70")"_s}; "content-length: 70"_ba,
};
QTest::newRow("extra-header-and-overwrite") QTest::newRow("extra-header-and-overwrite")
<< "text"_L1 << QAnyStringView("rfc3252.txt"_L1) << "text"_L1 << QAnyStringView("rfc3252.txt"_L1)
<< true << true << true << true
<< QStringList{ << QList{
uR"("content-disposition":"form-data; name=\"text\"; filename=\"rfc3252.txt\"")"_s, R"(content-disposition: form-data; name="text"; filename="rfc3252.txt")"_ba ,
uR"("content-type":"text/plain")"_s, "content-type: text/plain"_ba,
uR"("content-length":"70")"_s}; "content-length: 70"_ba,
};
} }
void tst_QFormDataBuilder::setHeadersDoesNotAffectHeaderFieldsManagedByBuilder() void tst_QFormDataBuilder::setHeadersDoesNotAffectHeaderFieldsManagedByBuilder()
@ -325,32 +329,31 @@ void tst_QFormDataBuilder::setHeadersDoesNotAffectHeaderFieldsManagedByBuilder()
QFETCH(const QAnyStringView, body_name_data); QFETCH(const QAnyStringView, body_name_data);
QFETCH(const bool, overwrite); QFETCH(const bool, overwrite);
QFETCH(const bool, extra_headers); QFETCH(const bool, extra_headers);
QFETCH(const QStringList, expected_headers); QFETCH(const QByteArrayList, expected_headers);
QBuffer buff; QBuffer buff;
QVERIFY(buff.open(QIODevice::ReadOnly));
QFormDataBuilder qfdb; const auto msg = serialized([&](auto &builder) {
QFormDataPartBuilder &qfdpb = qfdb.part(name_data).setBodyDevice(&buff, body_name_data); auto &qfdpb = builder.part(name_data).setBodyDevice(&buff, body_name_data);
if (overwrite || extra_headers) { if (overwrite || extra_headers) {
QHttpHeaders headers; QHttpHeaders headers;
if (overwrite) { if (overwrite) {
headers.append(QHttpHeaders::WellKnownHeader::ContentType, "attachment"); headers.append(QHttpHeaders::WellKnownHeader::ContentType, "attachment");
qfdpb.setHeaders(headers); qfdpb.setHeaders(headers);
} }
if (extra_headers) { if (extra_headers) {
headers.append(QHttpHeaders::WellKnownHeader::ContentLength, "70"); headers.append(QHttpHeaders::WellKnownHeader::ContentLength, "70");
qfdpb.setHeaders(std::move(headers)); qfdpb.setHeaders(std::move(headers));
} }
} }
});
const QHttpPart httpPart = qfdpb.build();
const auto msg = QDebug::toString(httpPart);
for (const auto &header : expected_headers) for (const auto &header : expected_headers)
QVERIFY2(msg.contains(header), qPrintable(header)); QVERIFY2(msg.contains(CRLF + header + CRLF), header);
} }
void tst_QFormDataBuilder::specifyMimeType_data() void tst_QFormDataBuilder::specifyMimeType_data()