Skip to content

Commit d6a546e

Browse files
authored
Test for xml in response, regardless of mimetype (#984)
1 parent ea64e17 commit d6a546e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

owslib/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ def getXMLTree(rsp: ResponseWrapper) -> etree:
346346
content_type = rsp.info().get('Content-Type', 'text/xml')
347347
url = rsp.geturl()
348348

349+
has_xml_tag = raw_text.lstrip().startswith(b'<?xml')
350+
349351
xml_types = ['text/xml', 'application/xml', 'application/vnd.ogc.wms_xml']
350-
if not any(xt in content_type.lower() for xt in xml_types):
352+
if not any(xt in content_type.lower() for xt in xml_types) and not has_xml_tag:
351353
html_body = et.find('BODY') # note this is case-sensitive
352354
if html_body is not None and len(html_body.text) > 0:
353355
response_text = html_body.text.strip("\n")

tests/test_util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ def test_getXMLTree_valid():
7171
assert et.find('.//Title').text == "Example"
7272

7373

74+
def test_getXMLTree_non_xml_mime_type():
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'<?xml version="1.0" encoding="UTF-8"?>\n<WFS_Capabilities><ServiceIdentification>' \
79+
b'<Title>Example</Title></ServiceIdentification></WFS_Capabilities>'
80+
# test with a non-XML mime type, but the response starts with <?xml
81+
mock_resp.headers = {'Content-Type': 'application/octet-stream'}
82+
resp_wrap = ResponseWrapper(mock_resp)
83+
84+
et = getXMLTree(resp_wrap)
85+
assert et.find('.//Title').text == "Example"
86+
87+
7488
def test_getXMLTree_valid_missing_content_type():
7589

7690
mock_resp = mock.Mock()

0 commit comments

Comments
 (0)