diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-05-25 11:34:34 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-06-02 15:35:07 +0200 |
commit | 3fdd85ec486caa7ff87e88afd6532908ae1e8b91 (patch) | |
tree | 62a511f3dd443ebd3da942a597e11f8855c21aa8 | |
parent | 83acbeace83cd9be1d598746e0f4c770b4ecaed8 (diff) |
Do not overwrite the payload of a QCoapRequest
When sending POST and PUT requests via overloads taking QCoapRequest and
QByteArray data, the payload of the request will be overwritten by an empty
byte array, if no data has been passed when calling post()/put(). Do
not overwrite the payload of a QCoapRequest, if the data parameter is
empty.
Pick-to: 5.15
Change-Id: Iffa40b00ce841a5cebb09dcf59ea22a2a5f9df50
Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/coap/qcoapclient.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qcoapclient/tst_qcoapclient.cpp | 15 |
2 files changed, 18 insertions, 9 deletions
diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 367ef5d..82e8f4e 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -263,7 +263,8 @@ QCoapReply *QCoapClient::get(const QUrl &url) /*! Sends the \a request using the PUT method and returns a new QCoapReply - object. Uses \a data as the payload for this request. + object. Uses \a data as the payload for this request. If \a data is empty, + the payload of the \a request will be used. \sa get(), post(), deleteResource(), observe(), discover() */ @@ -273,7 +274,8 @@ QCoapReply *QCoapClient::put(const QCoapRequest &request, const QByteArray &data QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Put, d->connection->isSecure()); - copyRequest.setPayload(data); + if (!data.isEmpty()) + copyRequest.setPayload(data); return d->sendRequest(copyRequest); } @@ -308,7 +310,8 @@ QCoapReply *QCoapClient::put(const QUrl &url, const QByteArray &data) /*! Sends the \a request using the POST method and returns a new QCoapReply - object. Uses \a data as the payload for this request. + object. Uses \a data as the payload for this request. If \a data is empty, + the payload of the \a request will be used. \sa get(), put(), deleteResource(), observe(), discover() */ @@ -318,7 +321,8 @@ QCoapReply *QCoapClient::post(const QCoapRequest &request, const QByteArray &dat QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Post, d->connection->isSecure()); - copyRequest.setPayload(data); + if (!data.isEmpty()) + copyRequest.setPayload(data); return d->sendRequest(copyRequest); } diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index e28a48b..c9067c0 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -363,16 +363,19 @@ void tst_QCoapClient::methods() QSignalSpy spyClientFinished(&client, SIGNAL(finished(QCoapReply *))); + const QByteArray payload = "test payload"; QScopedPointer<QCoapReply> reply; - if (qstrncmp(QTest::currentDataTag(), "get", 3) == 0) + if (qstrncmp(QTest::currentDataTag(), "get", 3) == 0) { reply.reset(client.get(request)); - else if (qstrncmp(QTest::currentDataTag(), "post", 4) == 0) + } else if (qstrncmp(QTest::currentDataTag(), "post", 4) == 0) { + request.setPayload(payload); reply.reset(client.post(request)); - else if (qstrncmp(QTest::currentDataTag(), "put", 3) == 0) + } else if (qstrncmp(QTest::currentDataTag(), "put", 3) == 0) { + request.setPayload(payload); reply.reset(client.put(request)); - else if (qstrncmp(QTest::currentDataTag(), "delete", 6) == 0) + } else if (qstrncmp(QTest::currentDataTag(), "delete", 6) == 0) { reply.reset(client.deleteResource(request)); - else { + } else { QString error = QLatin1String("Unrecognized method '") + QTest::currentDataTag() + "'"; QFAIL(qPrintable(error)); } @@ -395,9 +398,11 @@ void tst_QCoapClient::methods() } else if (qstrncmp(QTest::currentDataTag(), "post", 4) == 0) { QVERIFY(replyData.isEmpty()); QCOMPARE(reply->responseCode(), QtCoap::ResponseCode::Created); + QCOMPARE(reply->request().payload(), payload); } else if (qstrncmp(QTest::currentDataTag(), "put", 3) == 0) { QVERIFY(replyData.isEmpty()); QCOMPARE(reply->responseCode(), QtCoap::ResponseCode::Changed); + QCOMPARE(reply->request().payload(), payload); } else if (qstrncmp(QTest::currentDataTag(), "delete", 6) == 0) { QVERIFY(replyData.isEmpty()); QCOMPARE(reply->responseCode(), QtCoap::ResponseCode::Deleted); |