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

get_url should accept headers as a dict, instead of only a complicated string #35470

Merged
merged 5 commits into from
Apr 10, 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
13 changes: 9 additions & 4 deletions lib/ansible/modules/net_tools/basics/get_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@
version_added: '1.8'
headers:
description:
- Add custom HTTP headers to a request in the format "key:value,key:value".
- Add custom HTTP headers to a request in hash/dict format. The hash/dict format was added in 2.6.
Previous versions used a C("key:value,key:value") string format. The C("key:value,key:value") string
format is deprecated and will be removed in version 2.10.
version_added: '2.0'
url_username:
description:
Expand Down Expand Up @@ -387,7 +389,7 @@ def main():
sha256sum=dict(type='str', default=''),
checksum=dict(type='str', default=''),
timeout=dict(type='int', default=10),
headers=dict(type='str'),
headers=dict(type='raw'),
tmp_dest=dict(type='path'),
)

Expand All @@ -410,11 +412,14 @@ def main():
tmp_dest = module.params['tmp_dest']

# Parse headers to dict
if module.params['headers']:
if isinstance(module.params['headers'], dict):
headers = module.params['headers']
elif module.params['headers']:
try:
headers = dict(item.split(':', 1) for item in module.params['headers'].split(','))
module.deprecate('Supplying `headers` as a string is deprecated. Please use dict/hash format for `headers`', version='2.10')
except Exception:
module.fail_json(msg="The header parameter requires a key:value,key:value syntax to be properly parsed.")
module.fail_json(msg="The string representation for the `headers` parameter requires a key:value,key:value syntax to be properly parsed.")
else:
headers = None

Expand Down
37 changes: 36 additions & 1 deletion test/integration/targets/get_url/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,45 @@

#https://github.com/ansible/ansible/issues/16191
- name: Test url split with no filename
get_url:
get_url:
url: https://{{ httpbin_host }}
dest: "{{ output_dir }}"

- name: Test headers string
get_url:
url: https://{{ httpbin_host }}/headers
headers: Foo:bar,Baz:qux
dest: "{{ output_dir }}/headers_string.json"

- name: Test headers string
assert:
that:
- (lookup('file', output_dir ~ '/headers_string.json')|from_json).headers.get('Foo') == 'bar'
- (lookup('file', output_dir ~ '/headers_string.json')|from_json).headers.get('Baz') == 'qux'

- name: Test headers string invalid format
get_url:
url: https://{{ httpbin_host }}/headers
headers: Foo
dest: "{{ output_dir }}/headers_string_invalid.json"
register: invalid_string_headers
failed_when:
- invalid_string_headers is successful
- invalid_string_headers.msg != "The string representation for the `headers` parameter requires a key:value,key:value syntax to be properly parsed."

- name: Test headers dict
get_url:
url: https://{{ httpbin_host }}/headers
headers:
Foo: bar
Baz: qux
dest: "{{ output_dir }}/headers_dict.json"

- name: Test headers dict
assert:
that:
- (lookup('file', output_dir ~ '/headers_dict.json')|from_json).headers.get('Foo') == 'bar'
- (lookup('file', output_dir ~ '/headers_dict.json')|from_json).headers.get('Baz') == 'qux'

- name: Test client cert auth, with certs
get_url:
Expand Down