aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2019-03-06 17:14:56 +0100
committerSona Kurazyan <sona.kurazyan@qt.io>2019-03-11 16:53:31 +0000
commit872ab757fd2ab19f2b004b38a176ac7700de9926 (patch)
tree2d10b2afa9d5f82069ec1ae136caa7afd9e838cf
parentd9d26033bb3945cf69654b505f85f9786e42eade (diff)
Hide APIs for specifying the request method
Since the request method is set anyway when sending a request (get, put, post, etc.), allowing users to specify the request method is fatuous (it will be overridden anyway when sending the request). Change-Id: Ibf079bd513f145c791d5d703aa5685bfe66dda7d Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/coap/qcoapclient.cpp30
-rw-r--r--src/coap/qcoaprequest.cpp21
-rw-r--r--src/coap/qcoaprequest.h11
-rw-r--r--tests/auto/qcoapclient/tst_qcoapclient.cpp14
-rw-r--r--tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp8
-rw-r--r--tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp8
-rw-r--r--tests/auto/qcoaprequest/tst_qcoaprequest.cpp29
7 files changed, 46 insertions, 75 deletions
diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp
index 09e171c..6787092 100644
--- a/src/coap/qcoapclient.cpp
+++ b/src/coap/qcoapclient.cpp
@@ -228,12 +228,6 @@ QCoapReply *QCoapClient::get(const QCoapRequest &request)
{
Q_D(QCoapClient);
- if (request.method() != QtCoap::Invalid
- && request.method() != QtCoap::Get) {
- qWarning("QCoapClient::get: Overriding method specified on request:"
- "using 'Get' instead.");
- }
-
QCoapRequest copyRequest(request, QtCoap::Get);
copyRequest.adjustUrl(d->connection->isSecure());
@@ -263,12 +257,6 @@ QCoapReply *QCoapClient::put(const QCoapRequest &request, const QByteArray &data
{
Q_D(QCoapClient);
- if (request.method() != QtCoap::Invalid
- && request.method() != QtCoap::Put) {
- qWarning("QCoapClient::put: Overriding method specified on request:"
- "using 'Put' instead.");
- }
-
QCoapRequest copyRequest(request, QtCoap::Put);
copyRequest.setPayload(data);
copyRequest.adjustUrl(d->connection->isSecure());
@@ -315,12 +303,6 @@ QCoapReply *QCoapClient::post(const QCoapRequest &request, const QByteArray &dat
{
Q_D(QCoapClient);
- if (request.method() != QtCoap::Invalid
- && request.method() != QtCoap::Post) {
- qWarning("QCoapClient::post: Overriding method specified on request:"
- "using 'Post' instead.");
- }
-
QCoapRequest copyRequest(request, QtCoap::Post);
copyRequest.setPayload(data);
copyRequest.adjustUrl(d->connection->isSecure());
@@ -370,12 +352,6 @@ QCoapReply *QCoapClient::deleteResource(const QCoapRequest &request)
{
Q_D(QCoapClient);
- if (request.method() != QtCoap::Invalid
- && request.method() != QtCoap::Delete) {
- qWarning("QCoapClient::deleteResource: Overriding method specified on request:"
- "using 'Delete' instead.");
- }
-
QCoapRequest copyRequest(request, QtCoap::Delete);
copyRequest.adjustUrl(d->connection->isSecure());
@@ -473,12 +449,6 @@ QCoapDiscoveryReply *QCoapClient::discover(const QUrl &url, const QString &disco
*/
QCoapReply *QCoapClient::observe(const QCoapRequest &request)
{
- if (request.method() != QtCoap::Invalid
- && request.method() != QtCoap::Get) {
- qWarning("QCoapClient::observe: Overriding method specified on request:"
- "using 'Get' instead.");
- }
-
QCoapRequest copyRequest(request, QtCoap::Get);
copyRequest.enableObserve();
diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp
index f3fdca8..c1ab691 100644
--- a/src/coap/qcoaprequest.cpp
+++ b/src/coap/qcoaprequest.cpp
@@ -125,15 +125,24 @@ QCoapRequest::QCoapRequest(const char *url, MessageType type) :
}
/*!
- Constructs a copy of the \a other QCoapRequest. Optionally allows to
- overwrite the QtCoap::Method of the request with the \a method
- argument.
+ Constructs a copy of the \a other QCoapRequest.
*/
-QCoapRequest::QCoapRequest(const QCoapRequest &other, QtCoap::Method method) :
+QCoapRequest::QCoapRequest(const QCoapRequest &other) :
//! No private data sharing, as QCoapRequestPrivate!=QCoapMessagePrivate
//! and the d_ptr is a QSharedDataPointer<QCoapMessagePrivate>
QCoapMessage(*new QCoapRequestPrivate(*other.d_func()))
{
+}
+
+/*!
+ \internal
+
+ Constructs a copy of the \a other QCoapRequest and sets the request
+ method to \a method.
+*/
+QCoapRequest::QCoapRequest(const QCoapRequest &other, QtCoap::Method method) :
+ QCoapRequest(other)
+{
if (method != QtCoap::Invalid)
setMethod(method);
}
@@ -170,8 +179,6 @@ QUrl QCoapRequest::proxyUrl() const
/*!
Returns the method of the request.
-
- \sa setMethod()
*/
QtCoap::Method QCoapRequest::method() const
{
@@ -215,6 +222,8 @@ void QCoapRequest::setProxyUrl(const QUrl &proxyUrl)
}
/*!
+ \internal
+
Sets the method of the request to the given \a method.
\sa method()
diff --git a/src/coap/qcoaprequest.h b/src/coap/qcoaprequest.h
index 7d6c7a5..600a635 100644
--- a/src/coap/qcoaprequest.h
+++ b/src/coap/qcoaprequest.h
@@ -50,8 +50,7 @@ public:
const QUrl &proxyUrl = QUrl());
explicit QCoapRequest(const char* url,
MessageType type = NonConfirmable);
- QCoapRequest(const QCoapRequest &other,
- QtCoap::Method method = QtCoap::Invalid);
+ QCoapRequest(const QCoapRequest &other);
~QCoapRequest();
QCoapRequest &operator=(const QCoapRequest &other);
@@ -62,7 +61,6 @@ public:
bool isObserve() const;
void setUrl(const QUrl &url);
void setProxyUrl(const QUrl &proxyUrl);
- void setMethod(QtCoap::Method method);
void enableObserve();
void adjustUrl(bool secure);
@@ -70,11 +68,18 @@ public:
static bool isUrlValid(const QUrl &url);
static QUrl adjustedUrl(const QUrl &url, bool secure);
+protected:
+ QCoapRequest(const QCoapRequest &other, QtCoap::Method method);
+
+ void setMethod(QtCoap::Method method);
+
private:
// Q_DECLARE_PRIVATE equivalent for shared data pointers
inline QCoapRequestPrivate* d_func();
const QCoapRequestPrivate* d_func() const
{ return reinterpret_cast<const QCoapRequestPrivate*>(d_ptr.constData()); }
+
+ friend class QCoapClient;
};
QT_END_NAMESPACE
diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp
index 266832e..5338326 100644
--- a/tests/auto/qcoapclient/tst_qcoapclient.cpp
+++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp
@@ -226,23 +226,15 @@ void tst_QCoapClient::methods_data()
QTest::addColumn<QUrl>("url");
QTest::addColumn<QtCoap::Method>("method");
- QTest::newRow("get_no_op") << QUrl(testServerResource()) << QtCoap::Invalid;
QTest::newRow("get") << QUrl(testServerResource()) << QtCoap::Get;
- QTest::newRow("get_incorrect_op") << QUrl(testServerResource()) << QtCoap::Put;
QTest::newRow("get_no_port")
<< QUrl("coap://" + testServerHost() + "/test") << QtCoap::Get;
QTest::newRow("get_no_scheme_no_port") << QUrl(testServerHost() + "/test") << QtCoap::Get;
- QTest::newRow("post_no_op") << QUrl(testServerResource()) << QtCoap::Invalid;
QTest::newRow("post") << QUrl(testServerResource()) << QtCoap::Post;
- QTest::newRow("post_incorrect_op") << QUrl(testServerResource()) << QtCoap::Delete;
QTest::newRow("post_no_scheme_no_port") << QUrl(testServerHost() + "/test") << QtCoap::Post;
- QTest::newRow("put_no_op") << QUrl(testServerResource()) << QtCoap::Invalid;
QTest::newRow("put") << QUrl(testServerResource()) << QtCoap::Put;
- QTest::newRow("put_incorrect_op") << QUrl(testServerResource()) << QtCoap::Post;
QTest::newRow("put_no_scheme_no_port") << QUrl(testServerHost() + "/test") << QtCoap::Put;
- QTest::newRow("delete_no_op") << QUrl(testServerResource()) << QtCoap::Invalid;
QTest::newRow("delete") << QUrl(testServerResource()) << QtCoap::Delete;
- QTest::newRow("delete_incorrect_op") << QUrl(testServerResource()) << QtCoap::Get;
QTest::newRow("delete_no_scheme_no_port") << QUrl(testServerHost() + "/test") << QtCoap::Delete;
}
@@ -252,10 +244,7 @@ void tst_QCoapClient::methods()
QFETCH(QtCoap::Method, method);
QCoapClient client;
-
QCoapRequest request(url);
- if (method != QtCoap::Invalid)
- request.setMethod(method);
QSignalSpy spyClientFinished(&client, SIGNAL(finished(QCoapReply *)));
@@ -299,6 +288,7 @@ void tst_QCoapClient::methods()
QFAIL(qPrintable(error));
}
}
+ QCOMPARE(reply->request().method(), method);
}
void tst_QCoapClient::separateMethod()
@@ -696,6 +686,7 @@ void tst_QCoapClient::discover()
const auto discoverUrl = QUrl(url.toString() + "/.well-known/core");
QCOMPARE(resourcesReply->url(), QCoapRequest::adjustedUrl(discoverUrl, false));
QCOMPARE(resourcesReply->resources().length(), resourceNumber);
+ QCOMPARE(resourcesReply->request().method(), QtCoap::Get);
//! TODO Test discovery content too
}
@@ -760,6 +751,7 @@ void tst_QCoapClient::observe()
QTRY_COMPARE_WITH_TIMEOUT(spyReplyNotified.count(), 3, 30000);
client.cancelObserve(reply.data());
QCOMPARE(reply->url(), QCoapRequest::adjustedUrl(url, false));
+ QCOMPARE(reply->request().method(), QtCoap::Get);
QVERIFY2(!spyReplyNotified.wait(7000), "'Notify' signal received after cancelling observe");
QCOMPARE(spyReplyFinished.count(), 1);
diff --git a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp
index 5166d69..f6d4160 100644
--- a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp
+++ b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp
@@ -53,6 +53,12 @@ private Q_SLOTS:
void isMulticast();
};
+struct QCoapRequestForTest : public QCoapRequest
+{
+ QCoapRequestForTest(const QUrl& url) : QCoapRequest(url) {}
+ using QCoapRequest::setMethod;
+};
+
void tst_QCoapInternalRequest::requestToFrame_data()
{
QTest::addColumn<QUrl>("url");
@@ -147,7 +153,7 @@ void tst_QCoapInternalRequest::requestToFrame()
QFETCH(QString, pduHeader);
QFETCH(QString, pduPayload);
- QCoapRequest request(url);
+ QCoapRequestForTest request(url);
request.setType(type);
request.setMethod(method);
request.setPayload(pduPayload.toUtf8());
diff --git a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp
index e94ed6b..61043ed 100644
--- a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp
+++ b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp
@@ -45,6 +45,12 @@
using namespace QtCoapNetworkSettings;
+struct QCoapRequestForTest : public QCoapRequest
+{
+ QCoapRequestForTest(const QUrl &url) : QCoapRequest(url) {}
+ using QCoapRequest::setMethod;
+};
+
class tst_QCoapQUdpConnection : public QObject
{
Q_OBJECT
@@ -160,7 +166,7 @@ void tst_QCoapQUdpConnection::sendRequest()
QSignalSpy spySocketReadyRead(connection.socket(), &QUdpSocket::readyRead);
QSignalSpy spyConnectionReadyRead(&connection, &QCoapQUdpConnection::readyRead);
- QCoapRequest request(protocol + host + path);
+ QCoapRequestForTest request(protocol + host + path);
request.setMessageId(24806);
request.setToken(QByteArray("abcd"));
request.setMethod(method);
diff --git a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp
index 5ac0b6d..4513bb9 100644
--- a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp
+++ b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp
@@ -46,12 +46,15 @@ private Q_SLOTS:
void adjustUrl();
void setUrl_data();
void setUrl();
- void setMethod_data();
- void setMethod();
void enableObserve();
void copyAndDetach();
};
+struct QCoapRequestForTest : public QCoapRequest
+{
+ using QCoapRequest::setMethod;
+};
+
void tst_QCoapRequest::ctor_data()
{
QTest::addColumn<QUrl>("url");
@@ -141,26 +144,6 @@ void tst_QCoapRequest::setUrl()
QCOMPARE(request.url(), expectedUrl);
}
-void tst_QCoapRequest::setMethod_data()
-{
- QTest::addColumn<QtCoap::Method>("method");
-
- QTest::newRow("get") << QtCoap::Get;
- QTest::newRow("put") << QtCoap::Put;
- QTest::newRow("post") << QtCoap::Post;
- QTest::newRow("delete") << QtCoap::Delete;
- QTest::newRow("other") << QtCoap::Other;
-}
-
-void tst_QCoapRequest::setMethod()
-{
- QFETCH(QtCoap::Method, method);
-
- QCoapRequest request;
- request.setMethod(method);
- QCOMPARE(request.method(), method);
-}
-
void tst_QCoapRequest::enableObserve()
{
QCoapRequest request;
@@ -173,7 +156,7 @@ void tst_QCoapRequest::enableObserve()
void tst_QCoapRequest::copyAndDetach()
{
- QCoapRequest a;
+ QCoapRequestForTest a;
a.setMessageId(3);
a.setPayload("payload");
a.setToken("token");