Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reimplement QgsNetworkContentFetcher test with a local server
Reduces false alarms in unit tests due to network problems
  • Loading branch information
m-kuhn committed Apr 29, 2015
1 parent 96576aa commit 06180fe
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 43 deletions.
43 changes: 0 additions & 43 deletions tests/src/core/testqgsnetworkcontentfetcher.cpp
Expand Up @@ -36,8 +36,6 @@ class TestQgsNetworkContentFetcher : public QObject
void cleanup();// will be called after every testfunction.
void fetchEmptyUrl(); //test fetching blank url
void fetchBadUrl(); //test fetching bad url
void fetchUrlContent(); //test fetching url content
void doubleFetch(); //fetch content while already fetching content
void fetchEncodedContent(); //test fetching url content encoded as utf-8

void contentLoaded();
Expand Down Expand Up @@ -94,47 +92,6 @@ void TestQgsNetworkContentFetcher::fetchBadUrl()
QVERIFY( fetcher.reply()->error() != QNetworkReply::NoError );
}

void TestQgsNetworkContentFetcher::fetchUrlContent()
{
QgsNetworkContentFetcher fetcher;
//test fetching content from the QGIS homepage - ideally a dedicate page should be created for these tests so
//that we do not rely on content from the homepage
mLoaded = false;
fetcher.fetchContent( QUrl( "http://www.qgis.org/en/site/" ) );
connect( &fetcher, SIGNAL( finished() ), this, SLOT( contentLoaded() ) );
while ( !mLoaded )
{
qApp->processEvents();
}
QVERIFY( fetcher.reply()->error() == QNetworkReply::NoError );

//test retrieved content
QString mFetchedHtml = fetcher.contentAsString();
QVERIFY( mFetchedHtml.contains( QString( "QGIS" ) ) );
}

void TestQgsNetworkContentFetcher::doubleFetch()
{
QgsNetworkContentFetcher fetcher;
//fetch content from the QGIS homepage - ideally a dedicate page should be created for these tests so
//that we do not rely on content from the homepage
mLoaded = false;
fetcher.fetchContent( QUrl( "http://www.osgeo.org/" ) );
//double fetch - this should happen before previous request finishes
fetcher.fetchContent( QUrl( "http://www.qgis.org/en/site/" ) );

connect( &fetcher, SIGNAL( finished() ), this, SLOT( contentLoaded() ) );

while ( !mLoaded )
{
qApp->processEvents();
}
QVERIFY( fetcher.reply()->error() == QNetworkReply::NoError );

//test retrieved content
QString mFetchedHtml = fetcher.contentAsString();
QVERIFY( mFetchedHtml.contains( QString( "QGIS" ) ) );
}

void TestQgsNetworkContentFetcher::fetchEncodedContent()
{
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -41,6 +41,7 @@ ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRendererV2 test_qgsgraduatedsymbolrendererv2.py)
ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
IF (WITH_APIDOC)
ADD_PYTHON_TEST(PyQgsDocCoverage test_qgsdoccoverage.py)
ENDIF (WITH_APIDOC)
120 changes: 120 additions & 0 deletions tests/src/python/test_qgsnetworkcontentfetcher.py
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsNetworkContentFetcher
.. note:: 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.
"""
__author__ = 'Matthias Kuhn'
__date__ = '4/28/2015'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis
import os
from utilities import unittest, TestCase, unitTestDataPath
from qgis.utils import qgsfunction
from qgis.core import QgsNetworkContentFetcher
from PyQt4.QtCore import QUrl, QCoreApplication
from PyQt4.QtNetwork import QNetworkReply
import SocketServer
import threading
import SimpleHTTPServer

PORT = 8080

class TestQgsNetworkContentFetcher(TestCase):
@classmethod
def setUpClass(cls):
# Bring up a simple HTTP server
os.chdir( unitTestDataPath() + '' )
handler = SimpleHTTPServer.SimpleHTTPRequestHandler

cls.httpd = SocketServer.TCPServer(('localhost', PORT), handler)

cls.httpd_thread = threading.Thread( target=cls.httpd.serve_forever )
cls.httpd_thread.setDaemon( True )
cls.httpd_thread.start()

def __init__(self, methodName):
"""Run once on class initialisation."""
unittest.TestCase.__init__(self, methodName)

self.loaded = False

self.app = QCoreApplication([])


def contentLoaded(self):
self.loaded = True

def testFetchEmptyUrl(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
fetcher.fetchContent( QUrl() )
fetcher.finished.connect( self.contentLoaded )
while not self.loaded:
self.app.processEvents()

r = fetcher.reply()
assert r.error() != QNetworkReply.NoError

def testFetchBadUrl(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
fetcher.fetchContent( QUrl( 'http://x' ) )
fetcher.finished.connect( self.contentLoaded )
while not self.loaded:
self.app.processEvents()

r = fetcher.reply()
assert r.error() != QNetworkReply.NoError

def testFetchUrlContent(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/qgis_local_server/index.html' ) )
fetcher.finished.connect( self.contentLoaded )
while not self.loaded:
self.app.processEvents()

r = fetcher.reply()
assert r.error() == QNetworkReply.NoError, r.error()

html = fetcher.contentAsString()
assert 'QGIS' in html

def testDoubleFetch(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
fetcher.fetchContent( QUrl( 'http://www.qgis.org/' ) )
# double fetch - this should happen before previous request finishes
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/qgis_local_server/index.html' ) )
fetcher.finished.connect( self.contentLoaded )
while not self.loaded:
self.app.processEvents()

r = fetcher.reply()
assert r.error() == QNetworkReply.NoError, r.error()

html = fetcher.contentAsString()
assert 'QGIS' in html

def testFetchEncodedContent(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/encoded_html.html' ) )
fetcher.finished.connect( self.contentLoaded )
while not self.loaded:
self.app.processEvents()

r = fetcher.reply()
assert r.error() == QNetworkReply.NoError, r.error()

html = fetcher.contentAsString()
assert unichr(6040) in html

if __name__ == "__main__":
unittest.main()

0 comments on commit 06180fe

Please sign in to comment.