summaryrefslogtreecommitdiffstats
path: root/tests/auto/qnetworkreply/tst_qnetworkreply.cpp
diff options
context:
space:
mode:
authorMarkus Goetz <Markus.Goetz@nokia.com>2010-02-19 12:49:58 (GMT)
committerMarkus Goetz <Markus.Goetz@nokia.com>2010-02-19 14:02:53 (GMT)
commit7ce06afb341240b102edcc83b33d0da3d9e7ab5e (patch)
tree639a86a5033969ebf3be2ad3f925923a165a67f8 /tests/auto/qnetworkreply/tst_qnetworkreply.cpp
parent53fce0ced68ae5d828dc19d05caa09e05b4e5151 (diff)
downloadQt-7ce06afb341240b102edcc83b33d0da3d9e7ab5e.zip
Qt-7ce06afb341240b102edcc83b33d0da3d9e7ab5e.tar.gz
Qt-7ce06afb341240b102edcc83b33d0da3d9e7ab5e.tar.bz2
tst_qnetworkreply: Check if TCP/HTTP connection got re-used
Reviewed-by: Peter Hartmann
Diffstat (limited to 'tests/auto/qnetworkreply/tst_qnetworkreply.cpp')
-rw-r--r--tests/auto/qnetworkreply/tst_qnetworkreply.cpp131
1 files changed, 124 insertions, 7 deletions
diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp
index 049bbb8..78b4d98 100644
--- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp
+++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp
@@ -257,6 +257,11 @@ private Q_SLOTS:
void httpConnectionCount();
+ void httpReUsingConnectionSequential_data();
+ void httpReUsingConnectionSequential();
+ void httpReUsingConnectionFromFinishedSlot_data();
+ void httpReUsingConnectionFromFinishedSlot();
+
void httpRecursiveCreation();
#ifndef QT_NO_OPENSSL
@@ -314,18 +319,20 @@ QT_END_NAMESPACE
QFAIL(qPrintable(errorMsg)); \
} while (0);
+
+// Does not work for POST/PUT!
class MiniHttpServer: public QTcpServer
{
Q_OBJECT
- QTcpSocket *client;
-
public:
+ QTcpSocket *client; // always the last one that was received
QByteArray dataToTransmit;
QByteArray receivedData;
bool doClose;
+ bool multiple;
int totalConnections;
- MiniHttpServer(const QByteArray &data) : client(0), dataToTransmit(data), doClose(true), totalConnections(0)
+ MiniHttpServer(const QByteArray &data) : client(0), dataToTransmit(data), doClose(true), multiple(false), totalConnections(0)
{
listen();
connect(this, SIGNAL(newConnection()), this, SLOT(doAccept()));
@@ -335,15 +342,21 @@ public slots:
void doAccept()
{
client = nextPendingConnection();
+ client->setParent(this);
++totalConnections;
- connect(client, SIGNAL(readyRead()), this, SLOT(sendData()));
+ connect(client, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
}
- void sendData()
+ void readyReadSlot()
{
receivedData += client->readAll();
- if (receivedData.contains("\r\n\r\n") ||
- receivedData.contains("\n\n")) {
+ int doubleEndlPos = receivedData.indexOf("\r\n\r\n");
+
+ if (doubleEndlPos != -1) {
+ // multiple requests incoming. remove the bytes of the current one
+ if (multiple)
+ receivedData.remove(0, doubleEndlPos+4);
+
client->write(dataToTransmit);
if (doClose) {
client->disconnectFromHost();
@@ -3790,6 +3803,110 @@ void tst_QNetworkReply::httpConnectionCount()
#endif
}
+void tst_QNetworkReply::httpReUsingConnectionSequential_data()
+{
+ QTest::addColumn<bool>("doDeleteLater");
+ QTest::newRow("deleteLater") << true;
+ QTest::newRow("noDeleteLater") << false;
+}
+
+void tst_QNetworkReply::httpReUsingConnectionSequential()
+{
+ QFETCH(bool, doDeleteLater);
+
+ QByteArray response("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
+ MiniHttpServer server(response);
+ server.multiple = true;
+ server.doClose = false;
+
+ QUrl url;
+ url.setScheme("http");
+ url.setPort(server.serverPort());
+ url.setHost("127.0.0.1");
+ // first request
+ QNetworkReply* reply1 = manager.get(QNetworkRequest(url));
+ connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ QTestEventLoop::instance().enterLoop(2);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+ QVERIFY(!reply1->error());
+ int reply1port = server.client->peerPort();
+
+ if (doDeleteLater)
+ reply1->deleteLater();
+
+ // finished received, send the next one
+ QNetworkReply*reply2 = manager.get(QNetworkRequest(url));
+ connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ QTestEventLoop::instance().enterLoop(2);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+ QVERIFY(!reply2->error());
+ int reply2port = server.client->peerPort(); // should still be the same object
+
+ QVERIFY(reply1port > 0);
+ QCOMPARE(server.totalConnections, 1);
+ QCOMPARE(reply2port, reply1port);
+
+ if (!doDeleteLater)
+ reply1->deleteLater(); // only do it if it was not done earlier
+ reply2->deleteLater();
+}
+
+class HttpReUsingConnectionFromFinishedSlot : public QObject {
+ Q_OBJECT;
+public:
+ QNetworkReply* reply1;
+ QNetworkReply* reply2;
+ QUrl url;
+ QNetworkAccessManager manager;
+public slots:
+ void finishedSlot() {
+ QVERIFY(!reply1->error());
+
+ QFETCH(bool, doDeleteLater);
+ if (doDeleteLater) {
+ reply1->deleteLater();
+ reply1 = 0;
+ }
+
+ // kick off 2nd request and exit the loop when it is done
+ reply2 = manager.get(QNetworkRequest(url));
+ reply2->setParent(this);
+ connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ }
+};
+
+void tst_QNetworkReply::httpReUsingConnectionFromFinishedSlot_data()
+{
+ httpReUsingConnectionSequential_data();
+}
+
+void tst_QNetworkReply::httpReUsingConnectionFromFinishedSlot()
+{
+ QByteArray response("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
+ MiniHttpServer server(response);
+ server.multiple = true;
+ server.doClose = false;
+
+ HttpReUsingConnectionFromFinishedSlot helper;
+ helper.reply1 = 0;
+ helper.reply2 = 0;
+ helper.url.setScheme("http");
+ helper.url.setPort(server.serverPort());
+ helper.url.setHost("127.0.0.1");
+
+ // first request
+ helper.reply1 = helper.manager.get(QNetworkRequest(helper.url));
+ helper.reply1->setParent(&helper);
+ connect(helper.reply1, SIGNAL(finished()), &helper, SLOT(finishedSlot()));
+ QTestEventLoop::instance().enterLoop(4);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ QVERIFY(helper.reply2);
+ QVERIFY(!helper.reply2->error());
+
+ QCOMPARE(server.totalConnections, 1);
+}
+
class HttpRecursiveCreationHelper : public QObject {
Q_OBJECT
public: