From a499c1b08763acb11373b6384fd1f5d89bda5ff8 Mon Sep 17 00:00:00 2001 From: peppelinux Date: Wed, 10 Feb 2021 18:36:15 +0100 Subject: [PATCH] null value error on metadata parser --- testenv/tests/test_parsers.py | 3 +++ testenv/utils.py | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/testenv/tests/test_parsers.py b/testenv/tests/test_parsers.py index 16ced12d..ed805c71 100644 --- a/testenv/tests/test_parsers.py +++ b/testenv/tests/test_parsers.py @@ -224,3 +224,6 @@ def test_broken_metadata_xml_valuerror(self): def test_stupid_metadata_xml_valuerror(self): saml_to_dict('test.stupido') + + def test_nullmetadata_xml_valuerror(self): + self.assertEqual({}, saml_to_dict(None)) diff --git a/testenv/utils.py b/testenv/utils.py index d7caec88..ac053f5b 100644 --- a/testenv/utils.py +++ b/testenv/utils.py @@ -89,14 +89,17 @@ def saml_to_dict(xmlstr): _err_msg = 'SP Metadata Parse Error' _trunc = 254 + if not xmlstr: + logger.error(f'{_err_msg} [Null Value Error] on xmlstr') + return {} # sometimes a bytes objects, sometimes a '_io.TextIOWrapper' object ... - if isinstance(xmlstr, io.TextIOWrapper): + elif isinstance(xmlstr, io.TextIOWrapper): xmlstr = xmlstr.read() try: root = objectify.fromstring(xmlstr) except ValueError: - logger.error(f'{_err_msg} [ValuerError] on: ' + logger.error(f'{_err_msg} [ValueError] on: ' f'{xmlstr[0:_trunc]}') return {} # that's for resiliency ...