Skip to content

Commit 3d937e1

Browse files
committed
Simple python tests of WFS responses
1 parent a1a19d2 commit 3d937e1

File tree

5 files changed

+730
-0
lines changed

5 files changed

+730
-0
lines changed

tests/src/python/test_qgsserver.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import re
1717
import unittest
1818
import urllib
19+
from mimetools import Message
20+
from StringIO import StringIO
1921
from qgis.server import QgsServer
2022
from qgis.core import QgsMessageLog
2123
from utilities import unitTestDataPath
@@ -166,6 +168,79 @@ def test_project_wms(self):
166168
for request in ('GetCapabilities', 'GetProjectSettings'):
167169
self.wms_request_compare(request)
168170

171+
# WFS tests
172+
def wfs_request_compare(self, request):
173+
project = self.testdata_path + "test+project_wfs.qgs"
174+
assert os.path.exists(project), "Project file not found: " + project
175+
176+
query_string = 'MAP=%s&SERVICE=WFS&VERSION=1.1.0&REQUEST=%s' % (urllib.quote(project), request)
177+
header, body = [str(_v) for _v in self.server.handleRequest(query_string)]
178+
self.assert_headers(header, body)
179+
response = header + body
180+
f = open(self.testdata_path + 'wfs_'+ request.lower() + '.txt')
181+
expected = f.read()
182+
f.close()
183+
# Store the output for debug or to regenerate the reference documents:
184+
"""
185+
f = open(os.path.dirname(__file__) + '/wfs_' + request.lower() + '_expected.txt', 'w+')
186+
f.write(expected)
187+
f.close()
188+
f = open(os.path.dirname(__file__) + '/wfs_' + request.lower() + '_response.txt', 'w+')
189+
f.write(response)
190+
f.close()
191+
"""
192+
response = re.sub(RE_STRIP_PATH, '', response)
193+
expected = re.sub(RE_STRIP_PATH, '', expected)
194+
self.assertEqual(response, expected, msg="request %s failed.\n Query: %s\n Expected:\n%s\n\n Response:\n%s" % (query_string, request, expected, response))
195+
196+
def test_project_wfs(self):
197+
"""Test some WMS request"""
198+
for request in ('GetCapabilities', 'DescribeFeatureType'):
199+
self.wfs_request_compare(request)
200+
201+
def wfs_getfeature_compare(self, requestid, request):
202+
project = self.testdata_path + "test+project_wfs.qgs"
203+
assert os.path.exists(project), "Project file not found: " + project
204+
205+
query_string = 'MAP=%s&SERVICE=WFS&VERSION=1.0.0&REQUEST=%s' % (urllib.quote(project), request)
206+
header, body = [str(_v) for _v in self.server.handleRequest(query_string)]
207+
self.assert_headers(header, body)
208+
response = header + body
209+
f = open(self.testdata_path + 'wfs_getfeature_'+ requestid + '.txt')
210+
expected = f.read()
211+
f.close()
212+
# Store the output for debug or to regenerate the reference documents:
213+
"""
214+
f = open(os.path.dirname(__file__) + '/wfs_getfeature_' + requestid + '_expected.txt', 'w+')
215+
f.write(expected)
216+
f.close()
217+
f = open(os.path.dirname(__file__) + '/wfs_getfeature_' + requestid + '_response.txt', 'w+')
218+
f.write(response)
219+
f.close()
220+
"""
221+
response = re.sub(RE_STRIP_PATH, '', response)
222+
expected = re.sub(RE_STRIP_PATH, '', expected)
223+
self.assertEqual(response, expected, msg=u"request %s failed.\n Query: %s\n Expected:\n%s\n\n Response:\n%s"
224+
% (query_string,
225+
request,
226+
unicode(expected, errors='replace'),
227+
unicode(response, errors='replace')))
228+
229+
def test_getfeature(self):
230+
tests = []
231+
tests.append(('nobbox', u'GetFeature&TYPENAME=testlayer&SRSNAME=EPSG:4326'))
232+
233+
for id, req in tests:
234+
self.wfs_getfeature_compare(id, req)
235+
236+
def assert_headers(self, header, body):
237+
headers = Message(StringIO(header))
238+
if headers.has_key('content-length'):
239+
content_length = int(headers['content-length'])
240+
body_length = len(body)
241+
self.assertEqual(content_length, body_length, msg= "Header reported content-length: %d Actual body length was: %d" %(content_length, body_length) )
242+
243+
169244
# The following code was used to test type conversion in python bindings
170245
# def test_qpair(self):
171246
# """Test QPair bindings"""

0 commit comments

Comments
 (0)