Skip to content

Commit

Permalink
[Server][Tests] Add PyQgsServerWFS test
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Oct 12, 2017
1 parent d7a48c6 commit b8ebbc1
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 256 deletions.
13 changes: 13 additions & 0 deletions src/server/services/wfs/qgswfsparameters.cpp
Expand Up @@ -136,6 +136,11 @@ namespace QgsWfs
else if ( fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0 )
f = Format::GeoJSON;

if ( f == Format::NONE &&
request().compare( QLatin1String( "describefeaturetype" ), Qt::CaseInsensitive ) == 0 &&
fStr.compare( QLatin1String( "xmlschema" ), Qt::CaseInsensitive ) == 0 )
f = Format::GML2;

return f;
}

Expand All @@ -144,6 +149,14 @@ namespace QgsWfs
return value( ParameterName::SRSNAME ).toString();
}

QString QgsWfsParameters::request() const
{
if ( mRequestParameters.contains( "REQUEST" ) )
return mRequestParameters["REQUEST"];
else
return QString();
}

QString QgsWfsParameters::version() const
{
// VERSION parameter is not managed with other parameters because
Expand Down
4 changes: 0 additions & 4 deletions src/server/services/wfs/qgswfsparameters.h
Expand Up @@ -85,9 +85,6 @@ namespace QgsWfs
*/
void dump() const;

<<<<<<< HEAD
/** Returns VERSION parameter as a string or an empty string if not
=======
/**
* Returns REQUEST parameter as a string or an empty string if not
* defined.
Expand All @@ -97,7 +94,6 @@ namespace QgsWfs

/**
* Returns VERSION parameter as a string or an empty string if not
>>>>>>> 747f00d... QgsWfsParameters
* defined.
* \returns version
*/
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -223,6 +223,7 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsServerProjectUtils test_qgsserver_projectutils.py)
ADD_PYTHON_TEST(PyQgsServerSecurity test_qgsserver_security.py)
ADD_PYTHON_TEST(PyQgsServerAccessControl test_qgsserver_accesscontrol.py)
ADD_PYTHON_TEST(PyQgsServerWFS test_qgsserver_wfs.py)
ADD_PYTHON_TEST(PyQgsServerWFST test_qgsserver_wfst.py)
ADD_PYTHON_TEST(PyQgsOfflineEditingWFS test_offline_editing_wfs.py)
ADD_PYTHON_TEST(PyQgsAuthManagerPasswordOWSTest test_authmanager_password_ows.py)
Expand Down
150 changes: 0 additions & 150 deletions tests/src/python/test_qgsserver.py
Expand Up @@ -255,156 +255,6 @@ def test_api(self):
expected = self.strip_version_xmlns(b'<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc">\n <ServiceException code="Service configuration error">Service unknown or unsupported</ServiceException>\n</ServiceExceptionReport>\n')
self.assertEqual(self.strip_version_xmlns(body), expected)

# WFS tests
def wfs_request_compare(self, request):
project = self.testdata_path + "test_project_wfs.qgs"
assert os.path.exists(project), "Project file not found: " + project

query_string = '?MAP=%s&SERVICE=WFS&VERSION=1.0.0&REQUEST=%s' % (urllib.parse.quote(project), request)
header, body = self._execute_request(query_string)
self.assert_headers(header, body)
response = header + body
reference_path = self.testdata_path + 'wfs_' + request.lower() + '.txt'
self.store_reference(reference_path, response)
f = open(reference_path, 'rb')
expected = f.read()
f.close()
response = re.sub(RE_STRIP_UNCHECKABLE, b'', response)
expected = re.sub(RE_STRIP_UNCHECKABLE, b'', expected)

self.assertXMLEqual(response, expected, msg="request %s failed.\n Query: %s" % (query_string, request))

def test_project_wfs(self):
"""Test some WFS request"""
for request in ('GetCapabilities', 'DescribeFeatureType'):
self.wfs_request_compare(request)

def wfs_getfeature_compare(self, requestid, request):
project = self.testdata_path + "test_project_wfs.qgs"
assert os.path.exists(project), "Project file not found: " + project

query_string = '?MAP=%s&SERVICE=WFS&VERSION=1.0.0&REQUEST=%s' % (urllib.parse.quote(project), request)
header, body = self._execute_request(query_string)
self.result_compare(
'wfs_getfeature_' + requestid + '.txt',
"request %s failed.\n Query: %s" % (
query_string,
request,
),
header, body
)

def test_getfeature(self):
tests = []
tests.append(('nobbox', 'GetFeature&TYPENAME=testlayer'))
tests.append(('startindex2', 'GetFeature&TYPENAME=testlayer&STARTINDEX=2'))
tests.append(('limit2', 'GetFeature&TYPENAME=testlayer&MAXFEATURES=2'))
tests.append(('start1_limit1', 'GetFeature&TYPENAME=testlayer&MAXFEATURES=1&STARTINDEX=1'))

for id, req in tests:
self.wfs_getfeature_compare(id, req)

def test_wfs_getcapabilities_url(self):
"""Check that URL in GetCapabilities response is complete"""
# empty url in project
project = os.path.join(self.testdata_path, "test_project_without_urls.qgs")
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(project),
"SERVICE": "WFS",
"VERSION": "1.3.0",
"REQUEST": "GetCapabilities",
"STYLES": ""
}.items())])

r, h = self._result(self._execute_request(qs))

for item in str(r).split("\\n"):
if "onlineResource" in item:
self.assertEqual("onlineResource=\"?" in item, True)

# url well defined in query string
project = os.path.join(self.testdata_path, "test_project_without_urls.qgs")
qs = "https://www.qgis-server.org?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(project),
"SERVICE": "WFS",
"VERSION": "1.3.0",
"REQUEST": "GetCapabilities",
"STYLES": ""
}.items())])

r, h = self._result(self._execute_request(qs))

for item in str(r).split("\\n"):
if "onlineResource" in item:
self.assertTrue("onlineResource=\"https://www.qgis-server.org?" in item, True)

# url well defined in project
project = os.path.join(self.testdata_path, "test_project_with_urls.qgs")
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(project),
"SERVICE": "WFS",
"VERSION": "1.3.0",
"REQUEST": "GetCapabilities",
"STYLES": ""
}.items())])

r, h = self._result(self._execute_request(qs))

for item in str(r).split("\\n"):
if "onlineResource" in item:
self.assertEqual("onlineResource=\"my_wfs_advertised_url\"" in item, True)

def result_compare(self, file_name, error_msg_header, header, body):
self.assert_headers(header, body)
response = header + body
reference_path = self.testdata_path + file_name
self.store_reference(reference_path, response)
f = open(reference_path, 'rb')
expected = f.read()
f.close()
response = re.sub(RE_STRIP_UNCHECKABLE, b'', response)
expected = re.sub(RE_STRIP_UNCHECKABLE, b'', expected)
self.assertXMLEqual(response, expected, msg="%s\n" % (error_msg_header))

def wfs_getfeature_post_compare(self, requestid, request):
project = self.testdata_path + "test_project_wfs.qgs"
assert os.path.exists(project), "Project file not found: " + project

query_string = '?MAP={}'.format(urllib.parse.quote(project))
header, body = self._execute_request(query_string, requestMethod=QgsServerRequest.PostMethod, data=request.encode('utf-8'))

self.result_compare(
'wfs_getfeature_{}.txt'.format(requestid),
"GetFeature in POST for '{}' failed.".format(requestid),
header, body,
)

def test_getfeature_post(self):
template = """<?xml version="1.0" encoding="UTF-8"?>
<wfs:GetFeature service="WFS" version="1.0.0" {} xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
<wfs:Query typeName="testlayer" xmlns:feature="http://www.qgis.org/gml">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:BBOX>
<ogc:PropertyName>geometry</ogc:PropertyName>
<gml:Envelope xmlns:gml="http://www.opengis.net/gml">
<gml:lowerCorner>8 44</gml:lowerCorner>
<gml:upperCorner>9 45</gml:upperCorner>
</gml:Envelope>
</ogc:BBOX>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>
"""

tests = []
tests.append(('nobbox_post', template.format("")))
tests.append(('startindex2_post', template.format('startIndex="2"')))
tests.append(('limit2_post', template.format('maxFeatures="2"')))
tests.append(('start1_limit1_post', template.format('startIndex="1" maxFeatures="1"')))

for id, req in tests:
self.wfs_getfeature_post_compare(id, req)

# WCS tests
def wcs_request_compare(self, request):
project = self.projectPath
Expand Down

0 comments on commit b8ebbc1

Please sign in to comment.