|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsBlockingNetworkRequest |
| 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 | + |
| 10 | +from builtins import chr |
| 11 | +from builtins import str |
| 12 | +__author__ = 'Nyall Dawson' |
| 13 | +__date__ = '12/11/2018' |
| 14 | +__copyright__ = 'Copyright 2018, The QGIS Project' |
| 15 | +# This will get replaced with a git SHA1 when you do a git archive |
| 16 | +__revision__ = '$Format:%H$' |
| 17 | + |
| 18 | +import qgis # NOQA |
| 19 | + |
| 20 | +import os |
| 21 | +from qgis.testing import unittest, start_app |
| 22 | +from qgis.core import QgsBlockingNetworkRequest |
| 23 | +from utilities import unitTestDataPath |
| 24 | +from qgis.PyQt.QtCore import QUrl |
| 25 | +from qgis.PyQt.QtTest import QSignalSpy |
| 26 | +from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest |
| 27 | +import socketserver |
| 28 | +import threading |
| 29 | +import http.server |
| 30 | + |
| 31 | +app = start_app() |
| 32 | + |
| 33 | + |
| 34 | +class TestQgsBlockingNetworkRequest(unittest.TestCase): |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def setUpClass(cls): |
| 38 | + # Bring up a simple HTTP server |
| 39 | + os.chdir(unitTestDataPath() + '') |
| 40 | + handler = http.server.SimpleHTTPRequestHandler |
| 41 | + |
| 42 | + cls.httpd = socketserver.TCPServer(('localhost', 0), handler) |
| 43 | + cls.port = cls.httpd.server_address[1] |
| 44 | + |
| 45 | + cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever) |
| 46 | + cls.httpd_thread.setDaemon(True) |
| 47 | + cls.httpd_thread.start() |
| 48 | + |
| 49 | + def testFetchEmptyUrl(self): |
| 50 | + request = QgsBlockingNetworkRequest() |
| 51 | + spy = QSignalSpy(request.downloadFinished) |
| 52 | + err = request.get(QNetworkRequest(QUrl())) |
| 53 | + self.assertEqual(len(spy), 1) |
| 54 | + self.assertEqual(err, QgsBlockingNetworkRequest.ServerExceptionError) |
| 55 | + self.assertEqual(request.errorMessage(), 'Protocol "" is unknown') |
| 56 | + reply = request.reply() |
| 57 | + self.assertFalse(reply.content()) |
| 58 | + |
| 59 | + def testFetchBadUrl(self): |
| 60 | + request = QgsBlockingNetworkRequest() |
| 61 | + spy = QSignalSpy(request.downloadFinished) |
| 62 | + err = request.get(QNetworkRequest(QUrl('http://x'))) |
| 63 | + self.assertEqual(len(spy), 1) |
| 64 | + self.assertEqual(err, QgsBlockingNetworkRequest.ServerExceptionError) |
| 65 | + self.assertEqual(request.errorMessage(), 'Host x not found') |
| 66 | + reply = request.reply() |
| 67 | + self.assertFalse(reply.content()) |
| 68 | + |
| 69 | + def testFetchBadUrl2(self): |
| 70 | + request = QgsBlockingNetworkRequest() |
| 71 | + spy = QSignalSpy(request.downloadFinished) |
| 72 | + err = request.get(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/ffff'))) |
| 73 | + self.assertEqual(len(spy), 1) |
| 74 | + self.assertEqual(err, QgsBlockingNetworkRequest.ServerExceptionError) |
| 75 | + self.assertIn('File not found', request.errorMessage()) |
| 76 | + reply = request.reply() |
| 77 | + self.assertFalse(reply.content()) |
| 78 | + self.assertEqual(reply.rawHeaderList(), []) |
| 79 | + |
| 80 | + def testGet(self): |
| 81 | + request = QgsBlockingNetworkRequest() |
| 82 | + spy = QSignalSpy(request.downloadFinished) |
| 83 | + err = request.get(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/qgis_local_server/index.html'))) |
| 84 | + self.assertEqual(len(spy), 1) |
| 85 | + self.assertEqual(err, QgsBlockingNetworkRequest.NoError) |
| 86 | + self.assertEqual(request.errorMessage(), '') |
| 87 | + reply = request.reply() |
| 88 | + self.assertEqual(reply.error(), QNetworkReply.NoError) |
| 89 | + self.assertEqual(reply.content(), '<!DOCTYPE html>\n<html lang="en">\n<head>\n\t<meta charset="utf-8" />\n\t<title>Local QGIS Server Default Index</title>\n</head>\n<body>\n <h2 style="font-family:Arial;">Web Server Working<h2/>\n</body>\n</html>\n') |
| 90 | + self.assertEqual(reply.rawHeaderList(), [b'Server', |
| 91 | + b'Date', |
| 92 | + b'Content-type', |
| 93 | + b'Content-Length', |
| 94 | + b'Last-Modified']) |
| 95 | + self.assertEqual(reply.rawHeader(b'Content-type'), 'text/html') |
| 96 | + self.assertEqual(reply.rawHeader(b'xxxxxxxxx'), '') |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + unittest.main() |
0 commit comments