Skip to content
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

Fixing HTTPError case of fetch_url for Python 3 compatibility. #45628

Merged
merged 4 commits into from
Sep 19, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/45628-fetch_url-error-headers.yaml
Original file line number Diff line number Diff line change
@@ -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)."
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions test/units/module_utils/urls/test_fetch_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down