From e566a8a9680636708f7f79f45062fbc72c84c7b2 Mon Sep 17 00:00:00 2001 From: Mate Barany Date: Mon, 4 Mar 2024 17:33:50 +0100 Subject: Add a means to send a POST request that has an empty body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Actually this has already worked if a nullptr was casted as a QIODevice*. Add an overload with a nullptr_t type, that does this behind the scenes. Fixes: QTBUG-108309 Change-Id: I2d4b17ae94cf4de2c42257d471ef901c8994fee5 Reviewed-by: Juha Vuolle Reviewed-by: MÃ¥rten Nordheim --- .../access/qnetworkreply/tst_qnetworkreply.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'tests/auto/network/access/qnetworkreply') diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 8bbb683f66..ab5ddfb56c 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -270,6 +270,8 @@ private Q_SLOTS: void postToHttpMultipart_data(); void postToHttpMultipart(); void multipartSkipIndices(); // QTBUG-32534 + void postWithoutBody_data(); + void postWithoutBody(); #if QT_CONFIG(ssl) void putToHttps_data(); void putToHttps(); @@ -646,6 +648,7 @@ public: bool stopTransfer = false; bool checkedContentLength = false; + bool foundContentLength = false; int contentRead = 0; int contentLength = 0; @@ -677,6 +680,7 @@ public: { contentLength = 0; receivedData.clear(); + foundContentLength = false; } protected: @@ -737,6 +741,8 @@ private: if (index == -1) return; + foundContentLength = true; + index += sizeof("content-length:") - 1; const auto end = std::find(receivedData.cbegin() + index, receivedData.cend(), '\r'); auto num = receivedData.mid(index, std::distance(receivedData.cbegin() + index, end)); @@ -3062,6 +3068,47 @@ void tst_QNetworkReply::multipartSkipIndices() // QTBUG-32534 multiPart->deleteLater(); } +void tst_QNetworkReply::postWithoutBody_data() +{ + QTest::addColumn("client_data"); + + QTest::newRow("client_has_data") << true; + QTest::newRow("client_does_not_have_data") << false; +} + +void tst_QNetworkReply::postWithoutBody() +{ + QFETCH(bool, client_data); + + QBuffer buff; + + if (client_data) { + buff.setData("Dummy data from client to server"); + buff.open(QIODevice::ReadOnly); + } + + QByteArray dataFromServerToClient = QByteArray("Some ridiculous dummy data"); + QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: "); + httpResponse += QByteArray::number(dataFromServerToClient.size()); + httpResponse += "\r\n\r\n"; + httpResponse += dataFromServerToClient; + + MiniHttpServer server(httpResponse); + server.doClose = true; + + QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort()))); + request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); + + QNetworkReplyPtr reply; + if (client_data) + reply.reset(manager.post(request, &buff)); + else + reply.reset(manager.post(request, nullptr)); + + QVERIFY2(waitForFinish(reply) == Success, msgWaitForFinished(reply)); + QCOMPARE(server.foundContentLength, client_data); +} + void tst_QNetworkReply::putToHttpMultipart_data() { postToHttpMultipart_data(); -- cgit v1.2.3