Skip to content

Test for XML in response, regardless of mimetype #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion owslib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ def getXMLTree(rsp: ResponseWrapper) -> etree:
content_type = rsp.info().get('Content-Type', 'text/xml')
url = rsp.geturl()

has_xml_tag = raw_text.lstrip().startswith(b'<?xml')

xml_types = ['text/xml', 'application/xml', 'application/vnd.ogc.wms_xml']
if not any(xt in content_type.lower() for xt in xml_types):
if not any(xt in content_type.lower() for xt in xml_types) and not has_xml_tag:
html_body = et.find('BODY') # note this is case-sensitive
if html_body is not None and len(html_body.text) > 0:
response_text = html_body.text.strip("\n")
Expand Down
14 changes: 14 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def test_getXMLTree_valid():
assert et.find('.//Title').text == "Example"


def test_getXMLTree_non_xml_mime_type():

mock_resp = mock.Mock()
mock_resp.url = 'http:///example.org/?service=WFS&request=GetCapabilities&version=2.0.0'
mock_resp.content = b'<?xml version="1.0" encoding="UTF-8"?>\n<WFS_Capabilities><ServiceIdentification>' \
b'<Title>Example</Title></ServiceIdentification></WFS_Capabilities>'
# test with a non-XML mime type, but the response starts with <?xml
mock_resp.headers = {'Content-Type': 'application/octet-stream'}
resp_wrap = ResponseWrapper(mock_resp)

et = getXMLTree(resp_wrap)
assert et.find('.//Title').text == "Example"


def test_getXMLTree_valid_missing_content_type():

mock_resp = mock.Mock()
Expand Down