forked from user-none/arora
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create a primary NetworkAcessManagerProxy class which can be tied to a
specific QWebPage and will insert a pointer to the webpage in all QNetworkRequests before passing it to the main QNetworkAccessManager. Using this class requires that the primary QNetworkAccessManager is a subclass of NetworkAcessManagerProxy.
- Loading branch information
Showing
15 changed files
with
521 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
networkaccessmanagerproxy |
12 changes: 12 additions & 0 deletions
12
autotests/utils/networkaccessmanagerproxy/networkaccessmanagerproxy.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TEMPLATE = app | ||
TARGET = | ||
DEPENDPATH += . | ||
INCLUDEPATH += . | ||
|
||
include(../../autotests.pri) | ||
|
||
# Input | ||
SOURCES = tst_networkaccessmanagerproxy.cpp networkaccessmanagerproxy.cpp webpageproxy.cpp | ||
HEADERS = networkaccessmanagerproxy.h webpageproxy.h | ||
FORMS = | ||
RESOURCES = |
146 changes: 146 additions & 0 deletions
146
autotests/utils/networkaccessmanagerproxy/tst_networkaccessmanagerproxy.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include <qtest.h> | ||
|
||
#include <networkaccessmanagerproxy.h> | ||
#include <webpageproxy.h> | ||
|
||
#include <qnetworkcookie.h> | ||
#include <qnetworkrequest.h> | ||
#include <qnetworkreply.h> | ||
|
||
class tst_NetworkAccessManagerProxy : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public slots: | ||
void initTestCase(); | ||
void cleanupTestCase(); | ||
void init(); | ||
void cleanup(); | ||
|
||
private slots: | ||
void networkaccessmanagerproxy_data(); | ||
void networkaccessmanagerproxy(); | ||
|
||
void primaryNetworkAccessManager(); | ||
void webPage(); | ||
|
||
void createRequest_data(); | ||
void createRequest(); | ||
}; | ||
|
||
// Subclass that exposes the protected functions. | ||
class SubNetworkAccessManagerProxy : public NetworkAccessManagerProxy | ||
{ | ||
public: | ||
QNetworkReply *call_createRequest(QNetworkAccessManager::Operation op, QNetworkRequest const &request, QIODevice *outgoingData = 0) | ||
{ return SubNetworkAccessManagerProxy::createRequest(op, request, outgoingData); } | ||
}; | ||
|
||
// This will be called before the first test function is executed. | ||
// It is only called once. | ||
void tst_NetworkAccessManagerProxy::initTestCase() | ||
{ | ||
} | ||
|
||
// This will be called after the last test function is executed. | ||
// It is only called once. | ||
void tst_NetworkAccessManagerProxy::cleanupTestCase() | ||
{ | ||
} | ||
|
||
// This will be called before each test function is executed. | ||
void tst_NetworkAccessManagerProxy::init() | ||
{ | ||
} | ||
|
||
// This will be called after every test function. | ||
void tst_NetworkAccessManagerProxy::cleanup() | ||
{ | ||
} | ||
|
||
void tst_NetworkAccessManagerProxy::networkaccessmanagerproxy_data() | ||
{ | ||
} | ||
|
||
void tst_NetworkAccessManagerProxy::networkaccessmanagerproxy() | ||
{ | ||
SubNetworkAccessManagerProxy proxy; | ||
QVERIFY(proxy.call_createRequest(QNetworkAccessManager::GetOperation, | ||
QNetworkRequest(), | ||
(QIODevice*)0)); | ||
} | ||
|
||
// public void setPrimaryNetworkAccessManager(NetworkAccessManagerProxy *primaryManager) | ||
void tst_NetworkAccessManagerProxy::primaryNetworkAccessManager() | ||
{ | ||
SubNetworkAccessManagerProxy proxy; | ||
|
||
NetworkAccessManagerProxy primaryManager; | ||
|
||
proxy.setPrimaryNetworkAccessManager(&primaryManager); | ||
QCOMPARE(&primaryManager, proxy.primaryNetworkAccessManager()); | ||
QVERIFY(primaryManager.cookieJar()->parent() == &primaryManager); | ||
} | ||
|
||
// public void setWebPage(WebPageProxy *page) | ||
void tst_NetworkAccessManagerProxy::webPage() | ||
{ | ||
SubNetworkAccessManagerProxy proxy; | ||
|
||
WebPageProxy webPage; | ||
proxy.setWebPage(&webPage); | ||
QCOMPARE(&webPage, proxy.webPage()); | ||
} | ||
|
||
void tst_NetworkAccessManagerProxy::createRequest_data() | ||
{ | ||
QTest::addColumn<bool>("primary"); | ||
QTest::newRow("true") << true; | ||
QTest::newRow("false") << false; | ||
} | ||
|
||
// protected QNetworkReply *createRequest(QNetworkAccessManager::Operation op, QNetworkRequest const &request, QIODevice *outgoingData = 0) | ||
void tst_NetworkAccessManagerProxy::createRequest() | ||
{ | ||
QFETCH(bool, primary); | ||
|
||
SubNetworkAccessManagerProxy proxy; | ||
SubNetworkAccessManagerProxy primaryManager; | ||
WebPageProxy webPage; | ||
if (!primary) { | ||
proxy.setPrimaryNetworkAccessManager(&primaryManager); | ||
proxy.setWebPage(&webPage); | ||
} | ||
|
||
QIODevice *outgoingData = 0; | ||
QNetworkRequest request; | ||
QNetworkReply *reply = proxy.call_createRequest(QNetworkAccessManager::GetOperation, request, outgoingData); | ||
QVERIFY(reply); | ||
if (primary) | ||
QCOMPARE(reply->request(), request); | ||
else | ||
QVERIFY(reply->request() != request); | ||
} | ||
|
||
QTEST_MAIN(tst_NetworkAccessManagerProxy) | ||
#include "tst_networkaccessmanagerproxy.moc" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
webpageproxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include <qtest.h> | ||
|
||
#include <webpageproxy.h> | ||
#include <qnetworkrequest.h> | ||
|
||
class tst_WebPageProxy : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public slots: | ||
void initTestCase(); | ||
void cleanupTestCase(); | ||
void init(); | ||
void cleanup(); | ||
|
||
private slots: | ||
void webpageproxy_data(); | ||
void webpageproxy(); | ||
|
||
void populateNetworkRequest(); | ||
}; | ||
|
||
// Subclass that exposes the protected functions. | ||
class SubWebPageProxy : public WebPageProxy | ||
{ | ||
public: | ||
void call_populateNetworkRequest(QNetworkRequest &request) | ||
{ return SubWebPageProxy::populateNetworkRequest(request); } | ||
}; | ||
|
||
// This will be called before the first test function is executed. | ||
// It is only called once. | ||
void tst_WebPageProxy::initTestCase() | ||
{ | ||
} | ||
|
||
// This will be called after the last test function is executed. | ||
// It is only called once. | ||
void tst_WebPageProxy::cleanupTestCase() | ||
{ | ||
} | ||
|
||
// This will be called before each test function is executed. | ||
void tst_WebPageProxy::init() | ||
{ | ||
} | ||
|
||
// This will be called after every test function. | ||
void tst_WebPageProxy::cleanup() | ||
{ | ||
} | ||
|
||
void tst_WebPageProxy::webpageproxy_data() | ||
{ | ||
} | ||
|
||
void tst_WebPageProxy::webpageproxy() | ||
{ | ||
SubWebPageProxy proxy; | ||
QCOMPARE(proxy.pageAttributeId(), 1100); | ||
QNetworkRequest request; | ||
proxy.call_populateNetworkRequest(request); | ||
} | ||
|
||
// protected void populateNetworkRequest(QNetworkRequest &request) | ||
void tst_WebPageProxy::populateNetworkRequest() | ||
{ | ||
SubWebPageProxy proxy; | ||
|
||
QNetworkRequest emptyRequest; | ||
QNetworkRequest request = emptyRequest; | ||
proxy.call_populateNetworkRequest(request); | ||
|
||
QVERIFY(request != emptyRequest); | ||
QVariant v = request.attribute((QNetworkRequest::Attribute)(proxy.pageAttributeId())); | ||
QVERIFY(v.isValid()); | ||
QWebPage *webPage = (QWebPage*)(v.value<void*>()); | ||
QVERIFY(webPage); | ||
QCOMPARE(webPage, &proxy); | ||
} | ||
|
||
QTEST_MAIN(tst_WebPageProxy) | ||
#include "tst_webpageproxy.moc" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TEMPLATE = app | ||
TARGET = | ||
DEPENDPATH += . | ||
INCLUDEPATH += . | ||
|
||
include(../../autotests.pri) | ||
|
||
# Input | ||
SOURCES = tst_webpageproxy.cpp webpageproxy.cpp | ||
HEADERS = webpageproxy.h | ||
FORMS = | ||
RESOURCES = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
#include "networkaccessmanagerproxy.h" | ||
|
||
#include "webpageproxy.h" | ||
|
||
#include <qnetworkcookie.h> | ||
#include <qnetworkrequest.h> | ||
|
||
NetworkAccessManagerProxy *NetworkAccessManagerProxy::m_primaryManager = 0; | ||
|
||
/* | ||
NetworkAccessManagerProxy inserts a pointer to the QWebPage in the | ||
QNetworkRequest before passing it on. | ||
*/ | ||
NetworkAccessManagerProxy::NetworkAccessManagerProxy(QObject *parent) | ||
: QNetworkAccessManager(parent) | ||
, m_webPage(0) | ||
{ | ||
} | ||
|
||
void NetworkAccessManagerProxy::setWebPage(WebPageProxy *page) | ||
{ | ||
Q_ASSERT(page); | ||
m_webPage = page; | ||
} | ||
|
||
void NetworkAccessManagerProxy::setPrimaryNetworkAccessManager(NetworkAccessManagerProxy *manager) | ||
{ | ||
Q_ASSERT(manager); | ||
m_primaryManager = manager; | ||
setCookieJar(m_primaryManager->cookieJar()); | ||
// Do not steal ownership! | ||
cookieJar()->setParent(manager); | ||
} | ||
|
||
QNetworkReply *NetworkAccessManagerProxy::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData) | ||
{ | ||
if (m_primaryManager && m_webPage) { | ||
QNetworkRequest pageRequest = request; | ||
m_webPage->populateNetworkRequest(pageRequest); | ||
return m_primaryManager->createRequest(op, pageRequest, outgoingData); | ||
} | ||
return QNetworkAccessManager::createRequest(op, request, outgoingData); | ||
} | ||
|
Oops, something went wrong.