test_ows.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. #
  5. import unittest
  6. import logging
  7. import os,sys
  8. from ConfigParser import ConfigParser
  9. import urllib
  10. import urlparse
  11. OWSVIEWER_DIR=os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
  12. sys.path.append(OWSVIEWER_DIR)
  13. from OWS import *
  14. class TestOWS(unittest.TestCase):
  15. """Base class for OWS services (WFS, WCS)
  16. """
  17. owsUrl = None
  18. config = None
  19. service = None
  20. def setUp(self):
  21. # set logging
  22. logging.basicConfig(level=logging.DEBUG)
  23. # mapfile for original services
  24. mapfile = os.path.join(OWSVIEWER_DIR,"tests","mapfiles","ows.map")
  25. self.config = ConfigParser.ConfigParser()
  26. self.config.read(os.path.join(OWSVIEWER_DIR,"tests","test.cfg"))
  27. self.owsUrl = self._getURLWithMap(mapfile)
  28. os.environ.update({"QUERY_STRING":
  29. "owsService=%s&owsUrl=%s"%(self.service,urllib.quote(self.owsUrl))})
  30. self.service = getService()
  31. def _getURLWithMap(self,mapfile):
  32. # create URL which will produce WCS and WFS services for testing
  33. # purposes
  34. owsUrl = urlparse.urlparse(self.config.get("OWSServer","owsserver"))
  35. owsParams = urlparse.parse_qs(owsUrl[4])
  36. owsParams["map"] = mapfile
  37. return urlparse.urlunparse((owsUrl[0],owsUrl[1],owsUrl[2], owsUrl[3],
  38. urllib.unquote(urllib.urlencode(owsParams,True)),owsUrl[5]))
  39. def _getWMSCapabilities(self):
  40. # download the WMS Capabilities
  41. url = self._getURLWithMap(self.service.mapfilename)
  42. url = urlparse.urlparse(url)
  43. params = urlparse.parse_qs(url[4])
  44. params["REQUEST"] = "GetCapabilities"
  45. params["SERVICE"] = "WMS"
  46. params = urllib.unquote(urllib.urlencode(params,True))
  47. attrs = (url[0],url[1],url[2],url[3],params,url[5])
  48. logging.debug("GetCapabilities URL: " + urlparse.urlunparse(attrs))
  49. resp = objectify.parse(urllib.urlopen(urlparse.urlunparse(attrs)))
  50. return resp.getroot()