diff --git a/python/server/qgsserver.sip b/python/server/qgsserver.sip index 74de604b40c..a37ec51a94c 100644 --- a/python/server/qgsserver.sip +++ b/python/server/qgsserver.sip @@ -185,16 +185,17 @@ class QgsServer */ void handleRequest( QgsServerRequest& request, QgsServerResponse& response ); - /** Handles the request from query strinf + /** Handles the request from query string * The query string is normally read from environment * but can be also passed in args and in this case overrides the environment * variable. * - * @param requestMethod QString that indicates the method. Only "GET" or "POST" are supported. - * @param data array of bytes containing post data - * @return the response headers and body QPair of QByteArray + * \param urlstr QString containing the request url (simple quely string must be preceded by '?') + * \param requestMethod QgsServerRequest::Method that indicates the method. Only "GET" or "POST" are supported. + * \param data array of bytes containing post data + * \returns the response headers and body QPair of QByteArray */ - QPair handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr ); + QPair handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod = QgsServerRequest::GetMethod, const char *data = nullptr ); /** Returns a pointer to the server interface */ QgsServerInterface* serverInterface(); diff --git a/src/server/qgsserver.cpp b/src/server/qgsserver.cpp index c6c77a92922..04b5c1d34ac 100644 --- a/src/server/qgsserver.cpp +++ b/src/server/qgsserver.cpp @@ -426,35 +426,26 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res } } -QPair QgsServer::handleRequest( const QString &urlstr, const QString &requestMethod, const char *data ) +QPair QgsServer::handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod, const char *data ) { - /* - * This is mainly for python bindings, passing QUERY_STRING - * to handleRequest without using os.environment - * - * XXX To be removed because query string is now handled in QgsServerRequest - * - */ + QUrl url( urlstr ); - QgsServerRequest::Method method = QgsServerRequest::GetMethod; QByteArray ba; - // XXX This is mainly used in tests - if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "POST" ), Qt::CaseInsensitive ) == 0 ) + if ( requestMethod == QgsServerRequest::PostMethod ) { - method = QgsServerRequest::PostMethod; if ( data ) { ba.append( data ); } } - else if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "GET" ), Qt::CaseInsensitive ) != 0 ) + else if ( requestMethod != QgsServerRequest::GetMethod ) { throw QgsServerException( QStringLiteral( "Invalid method in handleRequest(): only GET or POST is supported" ) ); } - QgsBufferServerRequest request( url, method, &ba ); + QgsBufferServerRequest request( url, requestMethod, &ba ); QgsBufferServerResponse response; handleRequest( request, response ); diff --git a/src/server/qgsserver.h b/src/server/qgsserver.h index b032c81c53a..e99450ee506 100644 --- a/src/server/qgsserver.h +++ b/src/server/qgsserver.h @@ -40,8 +40,8 @@ #include "qgsserverfilter.h" #include "qgsserverinterfaceimpl.h" #include "qgis_server.h" +#include "qgsserverrequest.h" -class QgsServerRequest; class QgsServerResponse; class QgsProject; @@ -79,11 +79,11 @@ class SERVER_EXPORT QgsServer * variable. * * \param urlstr QString containing the request url (simple quely string must be preceded by '?') - * \param requestMethod QString that indicates the method. Only "GET" or "POST" are supported. + * \param requestMethod QgsServerRequest::Method that indicates the method. Only "GET" or "POST" are supported. * \param data array of bytes containing post data * \returns the response headers and body QPair of QByteArray */ - QPair handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr ); + QPair handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod = QgsServerRequest::GetMethod, const char *data = nullptr ); //! Returns a pointer to the server interface QgsServerInterfaceImpl *serverInterface() { return sServerInterface; } diff --git a/tests/src/python/test_qgsserver.py b/tests/src/python/test_qgsserver.py index 0cd926898ba..50a4f200a6f 100644 --- a/tests/src/python/test_qgsserver.py +++ b/tests/src/python/test_qgsserver.py @@ -21,8 +21,8 @@ import urllib.error import email from io import StringIO -from qgis.server import QgsServer -from qgis.core import QgsMessageLog, QgsRenderChecker, QgsApplication +from qgis.server import QgsServer, QgsServerRequest +from qgis.core import QgsRenderChecker, QgsApplication from qgis.testing import unittest from qgis.PyQt.QtCore import QSize from utilities import unitTestDataPath @@ -346,7 +346,7 @@ class TestQgsServer(unittest.TestCase): assert os.path.exists(project), "Project file not found: " + project query_string = '?MAP={}'.format(urllib.parse.quote(project)) - header, body = self.server.handleRequest(query_string, requestMethod="POST", data=request) + header, body = self.server.handleRequest(query_string, requestMethod=QgsServerRequest.PostMethod, data=request) self.result_compare( 'wfs_getfeature_{}.txt'.format(requestid), diff --git a/tests/src/python/test_qgsserver_modules.py b/tests/src/python/test_qgsserver_modules.py index d7eb6fafd7a..ac92a23844b 100644 --- a/tests/src/python/test_qgsserver_modules.py +++ b/tests/src/python/test_qgsserver_modules.py @@ -18,7 +18,7 @@ class Response(QgsServerResponse): self._buffer = QBuffer() self._buffer.open(QIODevice.ReadWrite) - def setReturnCode(self, code): + def setStatusCode(self, code): pass def setHeader(self, key, val): diff --git a/tests/src/python/test_qgsserver_services.py b/tests/src/python/test_qgsserver_services.py index 25465554888..8f685eaf3c9 100644 --- a/tests/src/python/test_qgsserver_services.py +++ b/tests/src/python/test_qgsserver_services.py @@ -17,10 +17,10 @@ class Response(QgsServerResponse): self._buffer = QBuffer() self._buffer.open(QIODevice.ReadWrite) - def setReturnCode(self, code): + def setStatusCode(self, code): self.code = code - def returnCode(self): + def statusCode(self): return self.code def setHeader(self, key, val): @@ -60,7 +60,7 @@ class MyService(QgsService): return self._version def executeRequest(self, request, response): - response.setReturnCode(201) + response.setStatusCode(201) response.write(self._response) @@ -96,7 +96,7 @@ class TestServices(unittest.TestCase): io.seek(0) self.assertEqual(QTextStream(io).readLine(), "Hello world") - self.assertEqual(response.returnCode(), 201) + self.assertEqual(response.statusCode(), 201) def test_0_version_registration(self):