From 16158cd65e16af99bc07bbf2af7a9ee3417ff7a4 Mon Sep 17 00:00:00 2001 From: Martin Petersson Date: Wed, 1 Jun 2011 12:38:34 +0200 Subject: tst_qnetworkreply: add a test for http abort. Change-Id: Iec5fe195ff2befe92e759f77768240728bef31bd (cherry picked from commit 752d807797a0021e412a20b0a8fcc34b4fd8e380) --- tests/auto/qnetworkreply/tst_qnetworkreply.cpp | 58 +++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp index 25925bd..45f501c 100644 --- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp @@ -6195,16 +6195,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() -- cgit v0.12