2014-07-03 08:32:31 +02:00
# test code for the uri module
# (c) 2014, Leonid Evdokimov <leon@darkk.net.ru>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2018-05-02 01:28:42 +02:00
# along with Ansible. If not, see <https://www.gnu.org/licenses/>.
2014-07-03 08:32:31 +02:00
- name : set role facts
set_fact :
http_port : 15260
2015-05-14 23:21:29 +02:00
files_dir : '{{ output_dir|expanduser }}/files'
2014-07-03 08:32:31 +02:00
checkout_dir : '{{ output_dir }}/git'
2015-05-14 23:21:29 +02:00
- name : create a directory to serve files from
file :
dest : "{{ files_dir }}"
state : directory
- copy :
src : "{{ item }}"
dest : "{{files_dir}}/{{ item }}"
with_sequence : start=0 end=4 format=pass%d.json
- copy :
src : "{{ item }}"
dest : "{{files_dir}}/{{ item }}"
with_sequence : start=0 end=30 format=fail%d.json
2015-05-21 19:59:57 +02:00
- copy :
src : "testserver.py"
dest : "{{ output_dir }}/testserver.py"
2014-07-03 08:32:31 +02:00
- name : start SimpleHTTPServer
2016-09-03 19:19:52 +02:00
shell : cd {{ files_dir }} && {{ ansible_python.executable }} {{ output_dir}}/testserver.py {{ http_port }}
2017-01-24 23:08:24 +01:00
async : 120 # this test set can take ~1m to run on FreeBSD (via Shippable)
2014-07-05 07:15:57 +02:00
poll : 0
2014-07-03 08:32:31 +02:00
- wait_for : port={{ http_port }}
2015-05-14 23:21:29 +02:00
- name : checksum pass_json
stat : path={{ files_dir }}/{{ item }}.json get_checksum=yes
register : pass_checksum
2014-07-03 08:32:31 +02:00
with_sequence : start=0 end=4 format=pass%d
- name : fetch pass_json
uri : return_content=yes url=http://localhost:{{ http_port }}/{{ item }}.json
2019-05-17 11:02:03 +02:00
register : fetch_pass_json
2014-07-03 08:32:31 +02:00
with_sequence : start=0 end=4 format=pass%d
- name : check pass_json
assert :
that :
- '"json" in item.1'
2015-05-14 23:21:29 +02:00
- item.0.stat.checksum == item.1.content | checksum
2014-07-03 08:32:31 +02:00
with_together :
2016-02-08 19:13:15 +01:00
- "{{pass_checksum.results}}"
2019-05-17 11:02:03 +02:00
- "{{fetch_pass_json.results}}"
2014-07-03 08:32:31 +02:00
2015-05-14 23:21:29 +02:00
- name : checksum fail_json
stat : path={{ files_dir }}/{{ item }}.json get_checksum=yes
register : fail_checksum
2014-07-03 08:32:31 +02:00
with_sequence : start=0 end=30 format=fail%d
- name : fetch fail_json
uri : return_content=yes url=http://localhost:{{ http_port }}/{{ item }}.json
register : fail
with_sequence : start=0 end=30 format=fail%d
- name : check fail_json
assert :
that :
2015-05-14 23:21:29 +02:00
- item.0.stat.checksum == item.1.content | checksum
2014-07-03 08:32:31 +02:00
- '"json" not in item.1'
with_together :
2016-02-08 19:13:15 +01:00
- "{{fail_checksum.results}}"
- "{{fail.results}}"
2015-05-29 00:35:45 +02:00
- name : test https fetch to a site with mismatched hostname and certificate
uri :
2016-05-11 05:43:07 +02:00
url : "https://{{ badssl_host }}/"
2015-05-29 00:35:45 +02:00
dest : "{{ output_dir }}/shouldnotexist.html"
ignore_errors : True
register : result
- stat :
path : "{{ output_dir }}/shouldnotexist.html"
register : stat_result
- name : Assert that the file was not downloaded
assert :
that :
2019-05-23 16:17:17 +02:00
- result.failed == true
2019-05-31 22:35:26 +02:00
- "'Failed to validate the SSL certificate' in result.msg or 'Hostname mismatch' in result.msg or (result.msg is match('hostname .* doesn.t match .*'))"
2019-05-23 16:17:17 +02:00
- stat_result.stat.exists == false
- result.status is defined
- result.status == -1
- result.url == 'https://' ~ badssl_host ~ '/'
2015-05-29 00:35:45 +02:00
2015-07-14 20:48:41 +02:00
- name : Clean up any cruft from the results directory
file :
name : "{{ output_dir }}/kreitz.html"
state : absent
2015-05-29 00:35:45 +02:00
- name : test https fetch to a site with mismatched hostname and certificate and validate_certs=no
2016-04-15 23:10:54 +02:00
uri :
2016-05-11 05:43:07 +02:00
url : "https://{{ badssl_host }}/"
2015-05-29 00:35:45 +02:00
dest : "{{ output_dir }}/kreitz.html"
validate_certs : no
register : result
- stat :
path : "{{ output_dir }}/kreitz.html"
register : stat_result
2015-05-29 02:02:48 +02:00
- name : Assert that the file was downloaded
2015-05-29 00:35:45 +02:00
assert :
that :
- "stat_result.stat.exists == true"
2015-07-14 20:48:41 +02:00
- "result.changed == true"
2016-02-05 19:14:57 +01:00
- name : test redirect without follow_redirects
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/redirect/2'
2016-02-05 19:14:57 +01:00
follow_redirects : 'none'
status_code : 302
register : result
- name : Assert location header
assert :
that :
2018-05-02 01:28:42 +02:00
- 'result.location|default("") == "https://{{ httpbin_host }}/relative-redirect/1"'
2016-02-05 19:14:57 +01:00
2016-02-23 18:48:09 +01:00
- name : Check SSL with redirect
uri :
2016-05-11 05:43:07 +02:00
url : 'https://{{ httpbin_host }}/redirect/2'
2016-02-23 18:48:09 +01:00
register : result
- name : Assert SSL with redirect
assert :
that :
2016-05-11 05:43:07 +02:00
- 'result.url|default("") == "https://{{ httpbin_host }}/get"'
2016-02-23 18:48:09 +01:00
- name : redirect to bad SSL site
uri :
2016-05-11 05:43:07 +02:00
url : 'http://{{ badssl_host }}'
2016-02-23 18:48:09 +01:00
register : result
ignore_errors : true
- name : Ensure bad SSL site reidrect fails
assert :
that :
2017-11-27 23:58:08 +01:00
- result is failed
2016-05-11 05:43:07 +02:00
- 'badssl_host in result.msg'
2016-02-23 18:48:09 +01:00
2016-02-05 19:14:57 +01:00
- name : test basic auth
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/basic-auth/user/passwd'
2016-02-05 19:14:57 +01:00
user : user
password : passwd
- name : test basic forced auth
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/hidden-basic-auth/user/passwd'
2016-02-05 19:14:57 +01:00
force_basic_auth : true
user : user
password : passwd
2016-12-22 03:11:42 +01:00
- name : test digest auth
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/digest-auth/auth/user/passwd'
2016-12-22 03:11:42 +01:00
user : user
password : passwd
2017-03-01 17:35:48 +01:00
headers :
Cookie : "fake=fake_value"
2016-12-22 03:11:42 +01:00
2016-02-05 19:14:57 +01:00
- name : test PUT
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/put'
2016-02-05 19:14:57 +01:00
method : PUT
body : 'foo=bar'
- name : test OPTIONS
uri :
2018-05-02 01:28:42 +02:00
url : 'https://{{ httpbin_host }}/'
2016-02-05 19:14:57 +01:00
method : OPTIONS
register : result
- name : Assert we got an allow header
assert :
that :
2017-03-18 05:58:02 +01:00
- 'result.allow.split(", ")|sort == ["GET", "HEAD", "OPTIONS"]'
2016-04-05 02:35:47 +02:00
# Ubuntu12.04 doesn't have python-urllib3, this makes handling required dependencies a pain across all variations
# We'll use this to just skip 12.04 on those tests. We should be sufficiently covered with other OSes and versions
- name : Set fact if running on Ubuntu 12.04
set_fact :
is_ubuntu_precise : "{{ ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'precise' }}"
- name : Test that SNI succeeds on python versions that have SNI
uri :
2016-05-11 05:43:07 +02:00
url : 'https://{{ sni_host }}/'
2016-04-05 02:35:47 +02:00
return_content : true
when : ansible_python.has_sslcontext
register : result
- name : Assert SNI verification succeeds on new python
assert :
that :
2017-11-27 23:58:08 +01:00
- result is successful
2016-12-13 23:26:47 +01:00
- 'sni_host in result.content'
2016-04-05 02:35:47 +02:00
when : ansible_python.has_sslcontext
- name : Verify SNI verification fails on old python without urllib3 contrib
uri :
2016-05-11 05:43:07 +02:00
url : 'https://{{ sni_host }}'
2016-04-05 02:35:47 +02:00
ignore_errors : true
when : not ansible_python.has_sslcontext
register : result
- name : Assert SNI verification fails on old python
assert :
that :
2017-11-27 23:58:08 +01:00
- result is failed
when : result is not skipped
2016-04-05 02:35:47 +02:00
2019-03-15 05:52:03 +01:00
- name : check if urllib3 is installed as an OS package
package :
name : "{{ uri_os_packages[ansible_os_family].urllib3 }}"
check_mode : yes
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool and uri_os_packages[ansible_os_family].urllib3|default
register : urllib3
2019-03-15 01:47:19 +01:00
- name : uninstall conflicting urllib3 pip package
pip :
name : urllib3
state : absent
2019-03-15 05:52:03 +01:00
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool and uri_os_packages[ansible_os_family].urllib3|default and urllib3.changed
2019-03-15 01:47:19 +01:00
2016-04-05 02:35:47 +02:00
- name : install OS packages that are needed for SNI on old python
package :
name : "{{ item }}"
2017-01-06 18:03:10 +01:00
with_items : "{{ uri_os_packages[ansible_os_family].step1 | default([]) }}"
2016-04-05 02:35:47 +02:00
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : install python modules for Older Python SNI verification
pip :
name : "{{ item }}"
with_items :
- ndg-httpsclient
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
2016-05-31 23:47:36 +02:00
- name : Verify SNI verification succeeds on old python with urllib3 contrib
2016-04-05 02:35:47 +02:00
uri :
2016-05-11 05:43:07 +02:00
url : 'https://{{ sni_host }}'
2016-04-05 02:35:47 +02:00
return_content : true
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
register : result
- name : Assert SNI verification succeeds on old python
assert :
that :
2017-11-27 23:58:08 +01:00
- result is successful
2016-12-13 23:26:47 +01:00
- 'sni_host in result.content'
2016-04-05 02:35:47 +02:00
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
2017-01-06 18:03:10 +01:00
- name : Uninstall ndg-httpsclient
2016-04-05 02:35:47 +02:00
pip :
name : "{{ item }}"
state : absent
with_items :
- ndg-httpsclient
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : uninstall OS packages that are needed for SNI on old python
package :
name : "{{ item }}"
state : absent
2017-01-06 18:03:10 +01:00
with_items : "{{ uri_os_packages[ansible_os_family].step1 | default([]) }}"
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : install OS packages that are needed for building cryptography
package :
name : "{{ item }}"
with_items : "{{ uri_os_packages[ansible_os_family].step2 | default([]) }}"
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : install urllib3 and pyopenssl via pip
pip :
name : "{{ item }}"
state : latest
2019-01-25 04:25:06 +01:00
extra_args : "-c {{ remote_constraints }}"
2017-01-06 18:03:10 +01:00
with_items :
- urllib3
- PyOpenSSL
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : Verify SNI verification succeeds on old python with pip urllib3 contrib
uri :
url : 'https://{{ sni_host }}'
return_content : true
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
register : result
- name : Assert SNI verification succeeds on old python with pip urllib3 contrib
assert :
that :
2017-11-27 23:58:08 +01:00
- result is successful
2017-01-06 18:03:10 +01:00
- 'sni_host in result.content'
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
- name : Uninstall urllib3 and PyOpenSSL
pip :
name : "{{ item }}"
state : absent
with_items :
- urllib3
- PyOpenSSL
2016-04-05 02:35:47 +02:00
when : not ansible_python.has_sslcontext and not is_ubuntu_precise|bool
2016-04-18 19:02:57 +02:00
- name : validate the status_codes are correct
uri :
2016-05-11 05:43:07 +02:00
url : "https://{{ httpbin_host }}/status/202"
2016-04-18 19:02:57 +02:00
status_code : 202
method : POST
body : foo
2017-01-13 19:19:38 +01:00
- name : Validate body_format json does not override content-type in 2.3 or newer
uri :
url : "https://{{ httpbin_host }}/post"
method : POST
body :
foo : bar
body_format : json
headers :
'Content-Type' : 'text/json'
return_content : true
register : result
failed_when : result.json.headers['Content-Type'] != 'text/json'
2017-04-07 18:54:37 +02:00
2018-05-17 19:18:18 +02:00
- name : Validate body_format form-urlencoded using dicts works
uri :
url : https://{{ httpbin_host }}/post
method : POST
body :
user : foo
password : bar!#@ |&82$M
submit : Sign in
body_format : form-urlencoded
return_content : yes
register : result
- name : Assert form-urlencoded dict input
assert :
that :
- result is successful
- result.json.headers['Content-Type'] == 'application/x-www-form-urlencoded'
- result.json.form.password == 'bar!#@ |&82$M'
- name : Validate body_format form-urlencoded using lists works
uri :
url : https://{{ httpbin_host }}/post
method : POST
body :
- [ user, foo ]
- [ password, bar!#@ |&82$M ]
- [ submit, Sign in ]
body_format : form-urlencoded
return_content : yes
register : result
- name : Assert form-urlencoded list input
assert :
that :
- result is successful
- result.json.headers['Content-Type'] == 'application/x-www-form-urlencoded'
- result.json.form.password == 'bar!#@ |&82$M'
- name : Validate body_format form-urlencoded of invalid input fails
uri :
url : https://{{ httpbin_host }}/post
method : POST
body :
- foo
- bar : baz
body_format : form-urlencoded
return_content : yes
register : result
ignore_errors : yes
- name : Assert invalid input fails
assert :
that :
- result is failure
- "'failed to parse body as form_urlencoded: too many values to unpack' in result.msg"
2019-02-28 21:55:18 +01:00
- name : Validate invalid method
uri :
url : https://{{ httpbin_host }}/anything
method : UNKNOWN
register : result
ignore_errors : yes
- name : Assert invalid method fails
assert :
that :
- result is failure
- result.status == 405
- "'METHOD NOT ALLOWED' in result.msg"
2017-04-07 18:54:37 +02:00
- name : Test client cert auth, no certs
uri :
url : "https://ansible.http.tests/ssl_client_verify"
status_code : 200
return_content : true
register : result
failed_when : result.content != "ansible.http.tests:NONE"
when : has_httptester
- name : Test client cert auth, with certs
uri :
url : "https://ansible.http.tests/ssl_client_verify"
2019-01-25 04:25:06 +01:00
client_cert : "{{ remote_tmp_dir }}/client.pem"
client_key : "{{ remote_tmp_dir }}/client.key"
2017-04-07 18:54:37 +02:00
return_content : true
register : result
failed_when : result.content != "ansible.http.tests:SUCCESS"
when : has_httptester
- name : Test client cert auth, with no validation
uri :
url : "https://fail.ansible.http.tests/ssl_client_verify"
2019-01-25 04:25:06 +01:00
client_cert : "{{ remote_tmp_dir }}/client.pem"
client_key : "{{ remote_tmp_dir }}/client.key"
2017-04-07 18:54:37 +02:00
return_content : true
validate_certs : no
register : result
failed_when : result.content != "ansible.http.tests:SUCCESS"
when : has_httptester
- name : Test client cert auth, with validation and ssl mismatch
uri :
url : "https://fail.ansible.http.tests/ssl_client_verify"
2019-01-25 04:25:06 +01:00
client_cert : "{{ remote_tmp_dir }}/client.pem"
client_key : "{{ remote_tmp_dir }}/client.key"
2017-04-07 18:54:37 +02:00
return_content : true
validate_certs : yes
register : result
2017-11-27 23:58:08 +01:00
failed_when : result is not failed
2017-04-07 18:54:37 +02:00
when : has_httptester
2018-01-03 16:52:56 +01:00
2018-04-10 16:26:51 +02:00
- uri :
url : https://{{ httpbin_host }}/response-headers?Set-Cookie=Foo%3Dbar&Set-Cookie=Baz%3Dqux
register : result
- assert :
that :
- result['set_cookie'] == 'Foo=bar, Baz=qux'
# Python sorts cookies in order of most specific (ie. longest) path first
# items with the same path are reversed from response order
- result['cookies_string'] == 'Baz=qux; Foo=bar'
2018-01-03 16:52:56 +01:00
- name : Write out netrc template
template :
src : netrc.j2
2019-01-25 04:25:06 +01:00
dest : "{{ remote_tmp_dir }}/netrc"
2018-01-03 16:52:56 +01:00
- name : Test netrc with port
uri :
url : "https://{{ httpbin_host }}:443/basic-auth/user/passwd"
environment :
2019-01-25 04:25:06 +01:00
NETRC : "{{ remote_tmp_dir }}/netrc"
2018-03-29 20:54:42 +02:00
2018-05-31 18:43:00 +02:00
- name : Test JSON POST with src
uri :
url : "https://{{ httpbin_host}}/post"
src : pass0.json
method : POST
return_content : true
body_format : json
register : result
- name : Validate POST with src works
assert :
that :
- result.json.json[0] == 'JSON Test Pattern pass1'
2019-01-25 04:25:06 +01:00
- name : Copy file pass0.json to remote
copy :
src : "{{ role_path }}/files/pass0.json"
dest : "{{ remote_tmp_dir }}/pass0.json"
2018-05-31 18:43:00 +02:00
- name : Test JSON POST with src and remote_src=True
uri :
url : "https://{{ httpbin_host}}/post"
2019-01-25 04:25:06 +01:00
src : "{{ remote_tmp_dir }}/pass0.json"
2018-05-31 18:43:00 +02:00
remote_src : true
method : POST
return_content : true
body_format : json
register : result
- name : Validate POST with src and remote_src=True works
assert :
that :
- result.json.json[0] == 'JSON Test Pattern pass1'
2019-03-22 15:32:43 +01:00
- name : Create a testing file
copy :
content : "content"
dest : "{{ output_dir }}/output"
- name : Download a file from non existing location
uri :
url : http://does/not/exist
dest : "{{ output_dir }}/output"
ignore_errors : yes
- name : Save testing file's output
command : "cat {{ output_dir }}/output"
register : file_out
- name : Test the testing file was not overwritten
assert :
that :
- "'content' in file_out.stdout"
- name : Clean up
file :
dest : "{{ output_dir }}/output"
state : absent
2018-03-29 20:54:42 +02:00
- name : Test follow_redirects=none
uri: check unexpected failure doesn't occur when file cannot be saved (#45824)
* uri: fix TypeError when file can't be saved
Fix the following exception (and others):
Traceback (most recent call last):
File "/home/lilou/debug_dir/__main__.py", line 604, in <module>
main()
File "/home/lilou/debug_dir/__main__.py", line 554, in main
write_file(module, url, dest, content, resp)
File "/home/lilou/debug_dir/__main__.py", line 320, in write_file
module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest), **resp)
TypeError: fail_json() got multiple values for keyword argument 'msg'
I would rather remove **resp from returned values but this module is
flagged as stableinterface.
* Static imports are more straight forward and preferred unless dynamic inclusion is required.
2019-02-04 16:29:05 +01:00
import_tasks : redirect-none.yml
2018-03-29 20:54:42 +02:00
- name : Test follow_redirects=safe
uri: check unexpected failure doesn't occur when file cannot be saved (#45824)
* uri: fix TypeError when file can't be saved
Fix the following exception (and others):
Traceback (most recent call last):
File "/home/lilou/debug_dir/__main__.py", line 604, in <module>
main()
File "/home/lilou/debug_dir/__main__.py", line 554, in main
write_file(module, url, dest, content, resp)
File "/home/lilou/debug_dir/__main__.py", line 320, in write_file
module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest), **resp)
TypeError: fail_json() got multiple values for keyword argument 'msg'
I would rather remove **resp from returned values but this module is
flagged as stableinterface.
* Static imports are more straight forward and preferred unless dynamic inclusion is required.
2019-02-04 16:29:05 +01:00
import_tasks : redirect-safe.yml
2018-03-29 20:54:42 +02:00
- name : Test follow_redirects=urllib2
uri: check unexpected failure doesn't occur when file cannot be saved (#45824)
* uri: fix TypeError when file can't be saved
Fix the following exception (and others):
Traceback (most recent call last):
File "/home/lilou/debug_dir/__main__.py", line 604, in <module>
main()
File "/home/lilou/debug_dir/__main__.py", line 554, in main
write_file(module, url, dest, content, resp)
File "/home/lilou/debug_dir/__main__.py", line 320, in write_file
module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest), **resp)
TypeError: fail_json() got multiple values for keyword argument 'msg'
I would rather remove **resp from returned values but this module is
flagged as stableinterface.
* Static imports are more straight forward and preferred unless dynamic inclusion is required.
2019-02-04 16:29:05 +01:00
import_tasks : redirect-urllib2.yml
2018-03-29 20:54:42 +02:00
- name : Test follow_redirects=all
uri: check unexpected failure doesn't occur when file cannot be saved (#45824)
* uri: fix TypeError when file can't be saved
Fix the following exception (and others):
Traceback (most recent call last):
File "/home/lilou/debug_dir/__main__.py", line 604, in <module>
main()
File "/home/lilou/debug_dir/__main__.py", line 554, in main
write_file(module, url, dest, content, resp)
File "/home/lilou/debug_dir/__main__.py", line 320, in write_file
module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest), **resp)
TypeError: fail_json() got multiple values for keyword argument 'msg'
I would rather remove **resp from returned values but this module is
flagged as stableinterface.
* Static imports are more straight forward and preferred unless dynamic inclusion is required.
2019-02-04 16:29:05 +01:00
import_tasks : redirect-all.yml
- name : Check unexpected failures
import_tasks : unexpected-failures.yml