tst_qnetworkreply: add a test for http abort.
authorMartin Petersson <martin.petersson@nokia.com>
Wed, 1 Jun 2011 10:38:34 +0000 (12:38 +0200)
committerQt Continuous Integration System <qt-info@nokia.com>
Wed, 1 Jun 2011 12:47:50 +0000 (14:47 +0200)
Change-Id: Iec5fe195ff2befe92e759f77768240728bef31bd
Reviewed-on: http://codereview.qt.nokia.com/302
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Markus Goetz
tests/auto/qnetworkreply/tst_qnetworkreply.cpp

index 67b6e5c..7219e71 100644 (file)
@@ -6193,16 +6193,62 @@ void tst_QNetworkReply::synchronousRequestSslFailure()
 }
 #endif
 
-void tst_QNetworkReply::httpAbort()
+class HttpAbortHelper : public QObject
 {
-    // FIXME: Implement a test that aborts a big HTTP reply
-    // a) after the first readyRead()
-    // b) immediatly after the get()
-    // c) after the finished()
-    // The goal is no crash and no irrelevant signals after the abort
+    Q_OBJECT
+public:
+    HttpAbortHelper(QNetworkReply *parent)
+    : QObject(parent)
+    {
+        mReply = parent;
+        connect(parent, SIGNAL(readyRead()), this, SLOT(readyRead()));
+    }
+
+    ~HttpAbortHelper()
+    {
+    }
+
+public slots:
+    void readyRead()
+    {
+        mReply->abort();
+        QMetaObject::invokeMethod(&QTestEventLoop::instance(), "exitLoop", Qt::QueuedConnection);
+    }
+
+private:
+    QNetworkReply *mReply;
+};
 
+void tst_QNetworkReply::httpAbort()
+{
     // FIXME Also implement one where we do a big upload and then abort().
     // It must not crash either.
+
+    // Abort after the first readyRead()
+    QNetworkRequest request("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile");
+    QNetworkReplyPtr reply;
+    reply = manager.get(request);
+    HttpAbortHelper replyHolder(reply);
+    QTestEventLoop::instance().enterLoop(10);
+    QVERIFY(!QTestEventLoop::instance().timeout());
+    QCOMPARE(reply->error(), QNetworkReply::OperationCanceledError);
+    QVERIFY(reply->isFinished());
+
+    // Abort immediatly after the get()
+    QNetworkReplyPtr reply2 = manager.get(request);
+    connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+    reply2->abort();
+    QCOMPARE(reply2->error(), QNetworkReply::OperationCanceledError);
+    QVERIFY(reply2->isFinished());
+
+    // Abort after the finished()
+    QNetworkRequest request3("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
+    QNetworkReplyPtr reply3 = manager.get(request3);
+    connect(reply3, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+    QTestEventLoop::instance().enterLoop(10);
+    QVERIFY(reply3->isFinished());
+    reply3->abort();
+    QCOMPARE(reply3->error(), QNetworkReply::NoError);
 }
 
 void tst_QNetworkReply::dontInsertPartialContentIntoTheCache()