def testGet(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.downloadFinished) handler = mockedwebserver.SequentialHandler() handler.add('GET', '/test.html', 200, {'Content-type': 'text/html'}, '<html></html>\n') with mockedwebserver.install_http_handler(handler): err = request.get( QNetworkRequest( QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/test.html')), True) self.assertEqual(len(spy), 1) self.assertEqual(err, QgsBlockingNetworkRequest.NoError) self.assertEqual(request.errorMessage(), '') reply = request.reply() self.assertEqual(reply.error(), QNetworkReply.NoError) self.assertEqual(reply.content(), '<html></html>\n') self.assertEqual( reply.rawHeaderList(), [b'Server', b'Date', b'Content-type', b'Content-Length']) self.assertEqual(reply.rawHeader(b'Content-type'), 'text/html') self.assertEqual(reply.rawHeader(b'xxxxxxxxx'), '') self.assertEqual( reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200) self.assertEqual( reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute), 'OK') self.assertEqual( reply.attribute(QNetworkRequest.RedirectionTargetAttribute), None)
def testHead(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.finished) handler = mockedwebserver.SequentialHandler() handler.add('HEAD', '/test.html', 200, {'Content-type': 'text/html'}) with mockedwebserver.install_http_handler(handler): err = request.head(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/test.html')), True) self.assertEqual(len(spy), 1) self.assertEqual(err, QgsBlockingNetworkRequest.NoError) self.assertEqual(request.errorMessage(), '')
def testDelete(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.finished) handler = mockedwebserver.SequentialHandler() handler.add('DELETE', '/test.html', 200) with mockedwebserver.install_http_handler(handler): err = request.deleteResource(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/test.html'))) self.assertEqual(len(spy), 1) self.assertEqual(err, QgsBlockingNetworkRequest.NoError) self.assertEqual(request.errorMessage(), '')
def testPost(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.finished) handler = mockedwebserver.SequentialHandler() handler.add('POST', '/test.html', 200, expected_body=b"foo") with mockedwebserver.install_http_handler(handler): req = QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/test.html')) req.setHeader(QNetworkRequest.ContentTypeHeader, 'text/plain') err = request.post(req, b"foo") self.assertEqual(err, QgsBlockingNetworkRequest.NoError) self.assertEqual(request.errorMessage(), '')
def testHTTPRequestsOverrider(self): """ Test that GDAL curl network requests are redirected through QGIS networking """ with mockedwebserver.install_http_server() as port: handler = mockedwebserver.SequentialHandler() # Check failed network requests # Check that the driver requested Accept header is well propagated handler.add('GET', '/collections/foo', 404, expected_headers={'Accept': 'application/json'}) with mockedwebserver.install_http_handler(handler): QgsVectorLayer("OAPIF:http://127.0.0.1:%d/collections/foo" % port, 'test', 'ogr') # Error coming from Qt network stack, not GDAL/CURL one assert 'server replied: Not Found' in gdal.GetLastErrorMsg() # Test a nominal case handler = mockedwebserver.SequentialHandler() handler.add('GET', '/collections/foo', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }') handler.add('GET', '/collections/foo/items?limit=10', 200, {'Content-Type': 'application/geo+json'}, '{ "type": "FeatureCollection", "features": [] }') handler.add('GET', '/collections/foo/items?limit=10', 200, {'Content-Type': 'application/geo+json'}, '{ "type": "FeatureCollection", "features": [] }') handler.add('GET', '/collections/foo/items?limit=10', 200, {'Content-Type': 'application/geo+json'}, '{ "type": "FeatureCollection", "features": [] }') with mockedwebserver.install_http_handler(handler): vl = QgsVectorLayer("OAPIF:http://127.0.0.1:%d/collections/foo" % port, 'test', 'ogr') assert vl.isValid() # More complicated test using an anthentication configuration config = QgsAuthMethodConfig() config.setName('Basic') config.setMethod('Basic') config.setConfig('username', 'username') config.setConfig('password', 'password') QgsApplication.authManager().storeAuthenticationConfig(config) handler = mockedwebserver.SequentialHandler() # Check that the authcfg gets expanded during the network request ! handler.add('GET', '/collections/foo', 404, expected_headers={ 'Authorization': 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='}) with mockedwebserver.install_http_handler(handler): QgsVectorLayer("OAPIF:http://127.0.0.1:%d/collections/foo authcfg='%s'" % (port, config.id()), 'test', 'ogr')
def testFetchBadUrl2(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.finished) handler = mockedwebserver.SequentialHandler() handler.add('GET', '/ffff', 404, {}, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n <head>\n <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n <title>Error response</title>\n </head>\n <body>\n <h1>Error response</h1>\n <p>Error code: 404</p>\n <p>Message: File not found.</p>\n <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>\n </body>\n</html>\n') with mockedwebserver.install_http_handler(handler): err = request.get(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/ffff'))) self.assertEqual(len(spy), 1) self.assertEqual(err, QgsBlockingNetworkRequest.ServerExceptionError) self.assertIn('Not Found', request.errorMessage()) reply = request.reply() self.assertEqual(reply.error(), QNetworkReply.ContentNotFoundError) self.assertEqual(reply.content(), '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n <head>\n <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n <title>Error response</title>\n </head>\n <body>\n <h1>Error response</h1>\n <p>Error code: 404</p>\n <p>Message: File not found.</p>\n <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>\n </body>\n</html>\n')
def testFetchBadUrl2(self): request = QgsBlockingNetworkRequest() spy = QSignalSpy(request.downloadFinished) handler = mockedwebserver.SequentialHandler() handler.add('GET', '/ffff', 404) with mockedwebserver.install_http_handler(handler): err = request.get(QNetworkRequest(QUrl('http://localhost:' + str(TestQgsBlockingNetworkRequest.port) + '/ffff'))) self.assertEqual(len(spy), 1) self.assertEqual(err, QgsBlockingNetworkRequest.ServerExceptionError) self.assertIn('Not Found', request.errorMessage()) reply = request.reply() self.assertEqual(reply.error(), QNetworkReply.ContentNotFoundError) self.assertFalse(reply.content())