From bc69aeca7f7c8187a304d139667338084037b63f Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 19 Sep 2018 17:53:16 +0200 Subject: [PATCH] Fixing HTTPError case of fetch_url for Python 3 compatibility. (#45628) * Fixing HTTPError case of fetch_url for Python 3 compatibility. * Adding unit test. * PEP8. * Changelog. --- changelogs/fragments/45628-fetch_url-error-headers.yaml | 2 ++ lib/ansible/module_utils/urls.py | 3 ++- test/units/module_utils/urls/test_fetch_url.py | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/45628-fetch_url-error-headers.yaml diff --git a/changelogs/fragments/45628-fetch_url-error-headers.yaml b/changelogs/fragments/45628-fetch_url-error-headers.yaml new file mode 100644 index 00000000000..52cc7d861e7 --- /dev/null +++ b/changelogs/fragments/45628-fetch_url-error-headers.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "fetch_url did not always return lower-case header names in case of HTTP errors (https://github.com/ansible/ansible/pull/45628)." diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 9434778e1fb..c5e1632181c 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -1303,7 +1303,8 @@ def fetch_url(module, url, data=None, headers=None, method=None, # Try to add exception info to the output but don't fail if we can't try: - info.update(dict(**e.info())) + # Lowercase keys, to conform to py2 behavior, so that py3 and py2 are predictable + info.update(dict((k.lower(), v) for k, v in e.info().items())) except: pass diff --git a/test/units/module_utils/urls/test_fetch_url.py b/test/units/module_utils/urls/test_fetch_url.py index ac1fe591134..49373571ecc 100644 --- a/test/units/module_utils/urls/test_fetch_url.py +++ b/test/units/module_utils/urls/test_fetch_url.py @@ -178,13 +178,14 @@ def test_fetch_url_httperror(open_url_mock, fake_ansible_module): 'http://ansible.com/', 500, 'Internal Server Error', - {}, + {'Content-Type': 'application/json'}, StringIO('TESTS') ) r, info = fetch_url(fake_ansible_module, 'http://ansible.com/') - assert info == {'msg': 'HTTP Error 500: Internal Server Error', 'body': 'TESTS', 'status': 500, 'url': 'http://ansible.com/'} + assert info == {'msg': 'HTTP Error 500: Internal Server Error', 'body': 'TESTS', + 'status': 500, 'url': 'http://ansible.com/', 'content-type': 'application/json'} def test_fetch_url_urlerror(open_url_mock, fake_ansible_module):