|
| 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