Skip to content

Commit 06180fe

Browse files
committed
Reimplement QgsNetworkContentFetcher test with a local server
Reduces false alarms in unit tests due to network problems
1 parent 96576aa commit 06180fe

File tree

3 files changed

+121
-43
lines changed

3 files changed

+121
-43
lines changed

tests/src/core/testqgsnetworkcontentfetcher.cpp

-43
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class TestQgsNetworkContentFetcher : public QObject
3636
void cleanup();// will be called after every testfunction.
3737
void fetchEmptyUrl(); //test fetching blank url
3838
void fetchBadUrl(); //test fetching bad url
39-
void fetchUrlContent(); //test fetching url content
40-
void doubleFetch(); //fetch content while already fetching content
4139
void fetchEncodedContent(); //test fetching url content encoded as utf-8
4240

4341
void contentLoaded();
@@ -94,47 +92,6 @@ void TestQgsNetworkContentFetcher::fetchBadUrl()
9492
QVERIFY( fetcher.reply()->error() != QNetworkReply::NoError );
9593
}
9694

97-
void TestQgsNetworkContentFetcher::fetchUrlContent()
98-
{
99-
QgsNetworkContentFetcher fetcher;
100-
//test fetching content from the QGIS homepage - ideally a dedicate page should be created for these tests so
101-
//that we do not rely on content from the homepage
102-
mLoaded = false;
103-
fetcher.fetchContent( QUrl( "http://www.qgis.org/en/site/" ) );
104-
connect( &fetcher, SIGNAL( finished() ), this, SLOT( contentLoaded() ) );
105-
while ( !mLoaded )
106-
{
107-
qApp->processEvents();
108-
}
109-
QVERIFY( fetcher.reply()->error() == QNetworkReply::NoError );
110-
111-
//test retrieved content
112-
QString mFetchedHtml = fetcher.contentAsString();
113-
QVERIFY( mFetchedHtml.contains( QString( "QGIS" ) ) );
114-
}
115-
116-
void TestQgsNetworkContentFetcher::doubleFetch()
117-
{
118-
QgsNetworkContentFetcher fetcher;
119-
//fetch content from the QGIS homepage - ideally a dedicate page should be created for these tests so
120-
//that we do not rely on content from the homepage
121-
mLoaded = false;
122-
fetcher.fetchContent( QUrl( "http://www.osgeo.org/" ) );
123-
//double fetch - this should happen before previous request finishes
124-
fetcher.fetchContent( QUrl( "http://www.qgis.org/en/site/" ) );
125-
126-
connect( &fetcher, SIGNAL( finished() ), this, SLOT( contentLoaded() ) );
127-
128-
while ( !mLoaded )
129-
{
130-
qApp->processEvents();
131-
}
132-
QVERIFY( fetcher.reply()->error() == QNetworkReply::NoError );
133-
134-
//test retrieved content
135-
QString mFetchedHtml = fetcher.contentAsString();
136-
QVERIFY( mFetchedHtml.contains( QString( "QGIS" ) ) );
137-
}
13895

13996
void TestQgsNetworkContentFetcher::fetchEncodedContent()
14097
{

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
4141
ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
4242
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
4343
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRendererV2 test_qgsgraduatedsymbolrendererv2.py)
44+
ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
4445
IF (WITH_APIDOC)
4546
ADD_PYTHON_TEST(PyQgsDocCoverage test_qgsdoccoverage.py)
4647
ENDIF (WITH_APIDOC)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsNetworkContentFetcher
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Matthias Kuhn'
10+
__date__ = '4/28/2015'
11+
__copyright__ = 'Copyright 2015, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis
16+
import os
17+
from utilities import unittest, TestCase, unitTestDataPath
18+
from qgis.utils import qgsfunction
19+
from qgis.core import QgsNetworkContentFetcher
20+
from PyQt4.QtCore import QUrl, QCoreApplication
21+
from PyQt4.QtNetwork import QNetworkReply
22+
import SocketServer
23+
import threading
24+
import SimpleHTTPServer
25+
26+
PORT = 8080
27+
28+
class TestQgsNetworkContentFetcher(TestCase):
29+
@classmethod
30+
def setUpClass(cls):
31+
# Bring up a simple HTTP server
32+
os.chdir( unitTestDataPath() + '' )
33+
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
34+
35+
cls.httpd = SocketServer.TCPServer(('localhost', PORT), handler)
36+
37+
cls.httpd_thread = threading.Thread( target=cls.httpd.serve_forever )
38+
cls.httpd_thread.setDaemon( True )
39+
cls.httpd_thread.start()
40+
41+
def __init__(self, methodName):
42+
"""Run once on class initialisation."""
43+
unittest.TestCase.__init__(self, methodName)
44+
45+
self.loaded = False
46+
47+
self.app = QCoreApplication([])
48+
49+
50+
def contentLoaded(self):
51+
self.loaded = True
52+
53+
def testFetchEmptyUrl(self):
54+
fetcher = QgsNetworkContentFetcher()
55+
self.loaded = False
56+
fetcher.fetchContent( QUrl() )
57+
fetcher.finished.connect( self.contentLoaded )
58+
while not self.loaded:
59+
self.app.processEvents()
60+
61+
r = fetcher.reply()
62+
assert r.error() != QNetworkReply.NoError
63+
64+
def testFetchBadUrl(self):
65+
fetcher = QgsNetworkContentFetcher()
66+
self.loaded = False
67+
fetcher.fetchContent( QUrl( 'http://x' ) )
68+
fetcher.finished.connect( self.contentLoaded )
69+
while not self.loaded:
70+
self.app.processEvents()
71+
72+
r = fetcher.reply()
73+
assert r.error() != QNetworkReply.NoError
74+
75+
def testFetchUrlContent(self):
76+
fetcher = QgsNetworkContentFetcher()
77+
self.loaded = False
78+
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/qgis_local_server/index.html' ) )
79+
fetcher.finished.connect( self.contentLoaded )
80+
while not self.loaded:
81+
self.app.processEvents()
82+
83+
r = fetcher.reply()
84+
assert r.error() == QNetworkReply.NoError, r.error()
85+
86+
html = fetcher.contentAsString()
87+
assert 'QGIS' in html
88+
89+
def testDoubleFetch(self):
90+
fetcher = QgsNetworkContentFetcher()
91+
self.loaded = False
92+
fetcher.fetchContent( QUrl( 'http://www.qgis.org/' ) )
93+
# double fetch - this should happen before previous request finishes
94+
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/qgis_local_server/index.html' ) )
95+
fetcher.finished.connect( self.contentLoaded )
96+
while not self.loaded:
97+
self.app.processEvents()
98+
99+
r = fetcher.reply()
100+
assert r.error() == QNetworkReply.NoError, r.error()
101+
102+
html = fetcher.contentAsString()
103+
assert 'QGIS' in html
104+
105+
def testFetchEncodedContent(self):
106+
fetcher = QgsNetworkContentFetcher()
107+
self.loaded = False
108+
fetcher.fetchContent( QUrl( 'http://localhost:' + str( PORT ) + '/encoded_html.html' ) )
109+
fetcher.finished.connect( self.contentLoaded )
110+
while not self.loaded:
111+
self.app.processEvents()
112+
113+
r = fetcher.reply()
114+
assert r.error() == QNetworkReply.NoError, r.error()
115+
116+
html = fetcher.contentAsString()
117+
assert unichr(6040) in html
118+
119+
if __name__ == "__main__":
120+
unittest.main()

0 commit comments

Comments
 (0)