Beispiel #1
0
    def testOgcApiHandlerContentType(self):
        """Test OGC API Handler content types"""

        project = QgsProject()
        project.read(unitTestDataPath('qgis_server') + '/test_project.qgs')
        request = QgsBufferServerRequest(
            'http://server.qgis.org/api3/handlerthree?value1=9.5')
        response = QgsBufferServerResponse()

        # Add handler to API and test for /api3
        ctx = QgsServerApiContext('/services/api3', request, response, project,
                                  self.server.serverInterface())
        api = QgsServerOgcApi(self.server.serverInterface(), '/api3',
                              'apithree', 'a third api', '1.2')
        h3 = Handler3()
        api.registerHandler(h3)

        ctx = QgsServerApiContext('/services/api3/', request, response,
                                  project, self.server.serverInterface())
        api.executeRequest(ctx)
        self.assertEqual(
            json.loads(bytes(ctx.response().data()))['value1'], 9.5)

        # Call HTML
        ctx.request().setUrl(
            QtCore.QUrl(
                'http://server.qgis.org/api3/handlerthree.html?value1=9.5'))
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(str(ex.exception), 'Unsupported Content-Type: HTML')

        h3.setContentTypes([QgsServerOgcApi.HTML])
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(str(ex.exception),
                         'Template not found: handlerThree.html')

        # Define a template path
        dir = QtCore.QTemporaryDir()
        with open(dir.path() + '/handlerThree.html', 'w+') as f:
            f.write("Hello world")
        h3.templatePathOverride = dir.path() + '/handlerThree.html'
        ctx.response().clear()
        api.executeRequest(ctx)
        self.assertEqual(bytes(ctx.response().data()), b"Hello world")
Beispiel #2
0
    def testOgcApiHandler(self):
        """Test OGC API Handler"""

        project = QgsProject()
        project.read(unitTestDataPath('qgis_server') + '/test_project.qgs')
        request = QgsBufferServerRequest(
            'http://server.qgis.org/wfs3/collections/testlayer%20èé/items?limit=-1'
        )
        response = QgsBufferServerResponse()

        ctx = QgsServerApiContext('/services/api1', request, response, project,
                                  self.server.serverInterface())
        h = Handler1()
        self.assertTrue(
            h.staticPath(ctx).endswith('/resources/server/api/ogc/static'))
        self.assertEqual(h.path(), QtCore.QRegularExpression("/handlerone"))
        self.assertEqual(h.description(), 'The first handler ever')
        self.assertEqual(h.operationId(), 'handlerOne')
        self.assertEqual(h.summary(), 'First of its name')
        self.assertEqual(h.linkTitle(), 'Handler One Link Title')
        self.assertEqual(h.linkType(), QgsServerOgcApi.data)
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            h.handleRequest(ctx)
        self.assertEqual(str(ex.exception),
                         'Missing required argument: \'value1\'')

        r = ctx.response()
        self.assertEqual(r.data(), '')

        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            h.values(ctx)
        self.assertEqual(str(ex.exception),
                         'Missing required argument: \'value1\'')

        # Add handler to API and test for /api2
        ctx = QgsServerApiContext('/services/api2', request, response, project,
                                  self.server.serverInterface())
        api = QgsServerOgcApi(self.server.serverInterface(), '/api2', 'apitwo',
                              'a second api', '1.2')
        api.registerHandler(h)
        # Add a second handler (will be tested later)
        h2 = Handler2()
        api.registerHandler(h2)

        ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/services/api1'))
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(
            str(ex.exception),
            'Requested URI does not match any registered API handler')

        ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/services/api2'))
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(
            str(ex.exception),
            'Requested URI does not match any registered API handler')

        ctx.request().setUrl(
            QtCore.QUrl('http://www.qgis.org/services/api2/handlerone'))
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(str(ex.exception),
                         'Missing required argument: \'value1\'')

        ctx.request().setUrl(
            QtCore.QUrl(
                'http://www.qgis.org/services/api2/handlerone?value1=not+a+double'
            ))
        with self.assertRaises(QgsServerApiBadRequestException) as ex:
            api.executeRequest(ctx)
        self.assertEqual(
            str(ex.exception),
            'Argument \'value1\' could not be converted to Double')

        ctx.request().setUrl(
            QtCore.QUrl(
                'http://www.qgis.org/services/api2/handlerone?value1=1.2345'))
        params = h.values(ctx)
        self.assertEqual(params, {'value1': 1.2345})
        api.executeRequest(ctx)
        self.assertEqual(
            json.loads(bytes(ctx.response().data()))['value1'], 1.2345)

        # Test path fragments extraction
        ctx.request().setUrl(
            QtCore.QUrl(
                'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345'
            ))
        params = h2.values(ctx)
        self.assertEqual(params, {
            'code1': '00',
            'value1': 1.2345,
            'value2': None
        })

        # Test string encoding
        ctx.request().setUrl(
            QtCore.QUrl(
                'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345&value2=a%2Fstring%20some'
            ))
        params = h2.values(ctx)
        self.assertEqual(params, {
            'code1': '00',
            'value1': 1.2345,
            'value2': 'a/string some'
        })

        # Test links
        self.assertEqual(
            h2.href(ctx),
            'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345&value2=a%2Fstring%20some'
        )
        self.assertEqual(
            h2.href(ctx, '/extra'),
            'http://www.qgis.org/services/api2/handlertwo/00/555/extra?value1=1.2345&value2=a%2Fstring%20some'
        )
        self.assertEqual(
            h2.href(ctx, '/extra', 'json'),
            'http://www.qgis.org/services/api2/handlertwo/00/555/extra.json?value1=1.2345&value2=a%2Fstring%20some'
        )

        # Test template path
        self.assertTrue(
            h2.templatePath(ctx).endswith(
                '/resources/server/api/ogc/templates/services/api2/handlerTwo.html'
            ))