|
1 | 1 | # -*- coding: UTF-8 -*- |
2 | 2 | import codecs |
3 | | -from owslib.util import clean_ows_url, build_get_url, strip_bom, extract_time |
| 3 | +from unittest import mock |
| 4 | +import pytest |
| 5 | +from owslib.util import clean_ows_url, build_get_url, strip_bom, extract_time, ResponseWrapper, getXMLTree |
4 | 6 | from owslib.etree import etree |
5 | 7 | from datetime import datetime, timezone |
6 | 8 |
|
@@ -56,6 +58,35 @@ def test_build_get_url_overwrite(): |
56 | 58 | 'http://example.org/ows?SERVICE=WMS' |
57 | 59 |
|
58 | 60 |
|
| 61 | +def test_getXMLTree_valid(): |
| 62 | + |
| 63 | + mock_resp = mock.Mock() |
| 64 | + mock_resp.url = 'http:///example.org/?service=WFS&request=GetCapabilities&version=2.0.0' |
| 65 | + mock_resp.content = b'<?xml version="1.0" encoding="UTF-8"?>\n<WFS_Capabilities><ServiceIdentification>' \ |
| 66 | + b'<Title>Example</Title></ServiceIdentification></WFS_Capabilities>' |
| 67 | + mock_resp.headers = {'Content-Type': 'text/xml; charset=UTF-8'} |
| 68 | + resp_wrap = ResponseWrapper(mock_resp) |
| 69 | + |
| 70 | + et = getXMLTree(resp_wrap) |
| 71 | + assert et.find('.//Title').text == "Example" |
| 72 | + |
| 73 | + |
| 74 | +def test_getXMLTree_invalid(): |
| 75 | + |
| 76 | + mock_resp = mock.Mock() |
| 77 | + mock_resp.url = 'http:///example.org/?service=WFS&request=GetCapabilities&version=2.0.0' |
| 78 | + mock_resp.content = b'<HTML><HEAD></HEAD><BODY BGCOLOR="#FFFFFF">\nmsCGILoadMap(): Web application error. ' \ |
| 79 | + b'CGI variable "map" is not set.\n</BODY></HTML>' |
| 80 | + mock_resp.headers = {'Content-Type': 'text/html'} |
| 81 | + resp_wrap = ResponseWrapper(mock_resp) |
| 82 | + |
| 83 | + with pytest.raises(ValueError) as ex: |
| 84 | + getXMLTree(resp_wrap) |
| 85 | + |
| 86 | + assert str(ex.value) == 'http:///example.org/?service=WFS&request=GetCapabilities&version=2.0.0 responded with Content-Type \'text/html\'' \ |
| 87 | + ': \'msCGILoadMap(): Web application error. CGI variable \"map\" is not set.\'' |
| 88 | + |
| 89 | + |
59 | 90 | def test_time_zone_utc(): |
60 | 91 | now = datetime.utcnow() |
61 | 92 | as_utc = now.replace(tzinfo=timezone.utc) |
|
0 commit comments