summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-08 05:46:12 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-08 05:46:12 (GMT)
commitd608d21f78c8b561ada1aa0d2e9c6e59cb431035 (patch)
treef9d7e36a5b90cd4fb8de98eb92a1ce3531ff43c5
parent2dd8be99e85d5117e8c61f8eda98c6d7b1ccb839 (diff)
Extract method which generates json request + use qCDebug
-rw-r--r--webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt5
-rw-r--r--webengineviewer/src/checkphishingurl/checkphishingurljob.cpp82
-rw-r--r--webengineviewer/src/checkphishingurl/checkphishingurljob.h1
3 files changed, 49 insertions, 39 deletions
diff --git a/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt b/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
index a0c21b4..ac870a2 100644
--- a/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
+++ b/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
@@ -1,4 +1,7 @@
-ecm_add_test(checkphishingurljobtest.cpp ../checkphishingurljob.cpp
+set(testwebengineviewer_webengine_common_SRCS)
+ecm_qt_declare_logging_category(testwebengineviewer_webengine_common_SRCS HEADER webengineviewer_debug.h IDENTIFIER WEBENGINEVIEWER_LOG CATEGORY_NAME org.kde.pim.webengineviewer)
+
+ecm_add_test(checkphishingurljobtest.cpp ../checkphishingurljob.cpp ${testwebengineviewer_webengine_common_SRCS}
TEST_NAME checkphishingurljobtest
NAME_PREFIX "messageviewer-checkphishingurl-"
LINK_LIBRARIES Qt5::Test Qt5::WebKitWidgets KF5::WebEngineViewer
diff --git a/webengineviewer/src/checkphishingurl/checkphishingurljob.cpp b/webengineviewer/src/checkphishingurl/checkphishingurljob.cpp
index 0724d00..5c80210 100644
--- a/webengineviewer/src/checkphishingurl/checkphishingurljob.cpp
+++ b/webengineviewer/src/checkphishingurl/checkphishingurljob.cpp
@@ -23,6 +23,7 @@
#include <QNetworkReply>
#include <PimCommon/NetworkManager>
#include <QJsonDocument>
+#include <webengineviewer_debug.h>
using namespace WebEngineViewer;
@@ -51,7 +52,7 @@ void CheckPhishingUrlJob::slotCheckUrlFinished(QNetworkReply *reply)
{
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
- qDebug() << " info : " << document.toJson();
+ qCDebug(WEBENGINEVIEWER_LOG) << " info : " << document.toJson();
//TODO extract info from
//TODO Q_EMIT result(MessageViewer::CheckPhishingUrlJob:: ?);
reply->deleteLater();
@@ -63,17 +64,8 @@ void CheckPhishingUrlJob::setUrl(const QUrl &url)
mUrl = url;
}
-void CheckPhishingUrlJob::start()
+QByteArray CheckPhishingUrlJob::jsonRequest() const
{
- if (!PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline()) {
- Q_EMIT result(WebEngineViewer::CheckPhishingUrlJob::BrokenNetwork, mUrl);
- deleteLater();
- } else if (canStart()) {
- QUrl safeUrl = QUrl(QStringLiteral("https://safebrowsing.googleapis.com/v4/threatMatches:find"));
- safeUrl.addQueryItem(QStringLiteral("key"), apiKey());
- qDebug() << " safeUrl" << safeUrl;
- QNetworkRequest request(safeUrl);
- request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
#if 0
{
"client": {
@@ -92,37 +84,51 @@ void CheckPhishingUrlJob::start()
}
}
#endif
+ QVariantMap clientMap;
+ QVariantMap map;
+
+ clientMap.insert(QStringLiteral("clientId"), QStringLiteral("KDE"));
+ clientMap.insert(QStringLiteral("clientVersion"), QStringLiteral("5.4.0")); //FIXME
+ map.insert(QStringLiteral("client"), clientMap);
+
+ QVariantMap threatMap;
+ const QVariantList platformList = { QStringLiteral("WINDOWS") };
+ threatMap.insert(QStringLiteral("platformTypes"), platformList);
+
+ const QVariantList threatTypesList = { QStringLiteral("MALWARE") };
+ threatMap.insert(QStringLiteral("threatTypes"), threatTypesList);
+ const QVariantList threatEntryTypesList = { QStringLiteral("URL") };
+ threatMap.insert(QStringLiteral("threatEntryTypes"), threatEntryTypesList);
+ QVariantList threatEntriesList;
+ QVariantMap urlMap;
+ urlMap.insert(QStringLiteral("url"), mUrl.toString());
+ threatEntriesList.append(urlMap);
+ threatMap.insert(QStringLiteral("threatEntries"), threatEntriesList);
+
+ map.insert(QStringLiteral("threatInfo"), threatMap);
+
+ const QJsonDocument postData = QJsonDocument::fromVariant(map);
+ const QByteArray baPostData = postData.toJson();
+ return baPostData;
+}
- QVariantMap clientMap;
- QVariantMap map;
-
- clientMap.insert(QStringLiteral("clientId"), QStringLiteral("KDE"));
- clientMap.insert(QStringLiteral("clientVersion"), QStringLiteral("5.4.0")); //FIXME
- map.insert(QStringLiteral("client"), clientMap);
-
- QVariantMap threatMap;
- const QVariantList platformList = { QStringLiteral("WINDOWS") };
- threatMap.insert(QStringLiteral("platformTypes"), platformList);
-
- const QVariantList threatTypesList = { QStringLiteral("MALWARE") };
- threatMap.insert(QStringLiteral("threatTypes"), threatTypesList);
- const QVariantList threatEntryTypesList = { QStringLiteral("URL") };
- threatMap.insert(QStringLiteral("threatEntryTypes"), threatEntryTypesList);
- QVariantList threatEntriesList;
- QVariantMap urlMap;
- urlMap.insert(QStringLiteral("url"), mUrl.toString());
- threatEntriesList.append(urlMap);
- threatMap.insert(QStringLiteral("threatEntries"), threatEntriesList);
-
- map.insert(QStringLiteral("threatInfo"), threatMap);
+void CheckPhishingUrlJob::start()
+{
+ if (!PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline()) {
+ Q_EMIT result(WebEngineViewer::CheckPhishingUrlJob::BrokenNetwork, mUrl);
+ deleteLater();
+ } else if (canStart()) {
+ QUrl safeUrl = QUrl(QStringLiteral("https://safebrowsing.googleapis.com/v4/threatMatches:find"));
+ safeUrl.addQueryItem(QStringLiteral("key"), apiKey());
+ qDebug() << " safeUrl" << safeUrl;
+ QNetworkRequest request(safeUrl);
+ request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
- const QJsonDocument postData = QJsonDocument::fromVariant(map);
- const QByteArray baPostData = postData.toJson();
- qDebug() <<" postData.toJson()"<<baPostData;
+ const QByteArray baPostData = jsonRequest();
+ qCDebug(WEBENGINEVIEWER_LOG) <<" postData.toJson()"<<baPostData;
Q_EMIT debugJson(baPostData);
//curl -H "Content-Type: application/json" -X POST -d '{"client":{"clientId":"KDE","clientVersion":"5.4.0"},"threatInfo":{"platformTypes":["WINDOWS"],"threatEntries":[{"url":"http://www.kde.org"}],"threatEntryTypes":["URL"],"threatTypes":["MALWARE"]}}' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyBS62pXATjabbH2RM_jO2EzDg1mTMHlnyo
-
QNetworkReply *reply = mNetworkAccessManager->post(request, baPostData);
connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &CheckPhishingUrlJob::slotError);
} else {
@@ -134,7 +140,7 @@ void CheckPhishingUrlJob::start()
void CheckPhishingUrlJob::slotError(QNetworkReply::NetworkError error)
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
- qDebug()<<" error "<<reply->errorString();
+ qCDebug(WEBENGINEVIEWER_LOG)<<" error "<<reply->errorString();
//mErrorMsg = reply->errorString();
//FIXME
reply->deleteLater();
diff --git a/webengineviewer/src/checkphishingurl/checkphishingurljob.h b/webengineviewer/src/checkphishingurl/checkphishingurljob.h
index 5a3b03e..19d06c1 100644
--- a/webengineviewer/src/checkphishingurl/checkphishingurljob.h
+++ b/webengineviewer/src/checkphishingurl/checkphishingurljob.h
@@ -48,6 +48,7 @@ public:
void start();
bool canStart() const;
+ QByteArray jsonRequest() const;
Q_SIGNALS:
void result(WebEngineViewer::CheckPhishingUrlJob::UrlStatus status, const QUrl &url);
void debugJson(const QByteArray &ba);