From 9bb1ee30bf0de2aa62805daf112a1538a386093b Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 6 Apr 2018 20:17:14 +0200 Subject: [PATCH] Required changes to support redirects on HTTP 307/308 (#36809) * Required changes to support redirects on HTTP 307/308 This ensures HTTP 307 and 308 will redirect the request to the new location without modification. * Fix the unused newheaders reference * Be more compliant * Add integration tests for follow_redirects=all * Improve other tests for new behaviour * Make follow_redirects values more strict --- lib/ansible/module_utils/urls.py | 80 ++++++++++++----- .../targets/uri/tasks/redirect-all.yml | 86 ++++++++++--------- .../targets/uri/tasks/redirect-none.yml | 15 ++++ .../targets/uri/tasks/redirect-safe.yml | 60 +++++++------ .../targets/uri/tasks/redirect-urllib2.yml | 26 ++++++ 5 files changed, 177 insertions(+), 90 deletions(-) diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 10b39ae41ca..4e2f0ecf182 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -63,6 +63,8 @@ except ImportError: import urllib2 as urllib_request from urllib2 import AbstractHTTPHandler +urllib_request.HTTPRedirectHandler.http_error_308 = urllib_request.HTTPRedirectHandler.http_error_307 + try: from ansible.module_utils.six.moves.urllib.parse import urlparse, urlunparse HAS_URLPARSE = True @@ -444,11 +446,11 @@ class RequestWithMethod(urllib_request.Request): Originally contained in library/net_infrastructure/dnsmadeeasy ''' - def __init__(self, url, method, data=None, headers=None): + def __init__(self, url, method, data=None, headers=None, origin_req_host=None, unverifiable=True): if headers is None: headers = {} self._method = method.upper() - urllib_request.Request.__init__(self, url, data, headers) + urllib_request.Request.__init__(self, url, data, headers, origin_req_host, unverifiable) def get_method(self): if self._method: @@ -476,37 +478,69 @@ def RedirectHandlerFactory(follow_redirects=None, validate_certs=True): if handler: urllib_request._opener.add_handler(handler) + # Preserve urllib2 compatibility if follow_redirects == 'urllib2': return urllib_request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl) + + # Handle disabled redirects elif follow_redirects in ['no', 'none', False]: raise urllib_error.HTTPError(newurl, code, msg, hdrs, fp) - do_redirect = False - if follow_redirects in ['all', 'yes', True]: - do_redirect = (code >= 300 and code < 400) + method = req.get_method() + # Handle non-redirect HTTP status or invalid follow_redirects + if follow_redirects in ['all', 'yes', True]: + if code < 300 or code >= 400: + raise urllib_error.HTTPError(req.get_full_url(), code, msg, hdrs, fp) elif follow_redirects == 'safe': - m = req.get_method() - do_redirect = (code >= 300 and code < 400 and m in ('GET', 'HEAD')) - - if do_redirect: - # be conciliant with URIs containing a space - newurl = newurl.replace(' ', '%20') - newheaders = dict((k, v) for k, v in req.headers.items() - if k.lower() not in ("content-length", "content-type")) - try: - # Python 2-3.3 - origin_req_host = req.get_origin_req_host() - except AttributeError: - # Python 3.4+ - origin_req_host = req.origin_req_host - return urllib_request.Request(newurl, - headers=newheaders, - origin_req_host=origin_req_host, - unverifiable=True) + if code < 300 or code >= 400 or method not in ('GET', 'HEAD'): + raise urllib_error.HTTPError(req.get_full_url(), code, msg, hdrs, fp) else: raise urllib_error.HTTPError(req.get_full_url(), code, msg, hdrs, fp) + try: + # Python 2-3.3 + data = req.get_data() + origin_req_host = req.get_origin_req_host() + except AttributeError: + # Python 3.4+ + data = req.data + origin_req_host = req.origin_req_host + + # Be conciliant with URIs containing a space + newurl = newurl.replace(' ', '%20') + + # Suport redirect with payload and original headers + if code in (307, 308): + # Preserve payload and headers + headers = req.headers + else: + # Do not preserve payload and filter headers + data = None + headers = dict((k, v) for k, v in req.headers.items() + if k.lower() not in ("content-length", "content-type", "transfer-encoding")) + + # http://tools.ietf.org/html/rfc7231#section-6.4.4 + if code == 303 and method != 'HEAD': + method = 'GET' + + # Do what the browsers do, despite standards... + # First, turn 302s into GETs. + if code == 302 and method != 'HEAD': + method = 'GET' + + # Second, if a POST is responded to with a 301, turn it into a GET. + if code == 301 and method == 'POST': + method = 'GET' + + return RequestWithMethod(newurl, + method=method, + headers=headers, + data=data, + origin_req_host=origin_req_host, + unverifiable=True, + ) + return RedirectHandler diff --git a/test/integration/targets/uri/tasks/redirect-all.yml b/test/integration/targets/uri/tasks/redirect-all.yml index 58e76426ad7..334bc7bf27c 100644 --- a/test/integration/targets/uri/tasks/redirect-all.yml +++ b/test/integration/targets/uri/tasks/redirect-all.yml @@ -1,4 +1,3 @@ -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 301 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=301&url=http://{{ httpbin_host }}/anything @@ -9,8 +8,8 @@ - assert: that: - - http_301_head.json.data == '' - - http_301_head.json.method == 'GET' + - http_301_head is successful + - http_301_head.json is not defined - http_301_head.redirected == true - http_301_head.status == 200 - http_301_head.url == 'http://{{ httpbin_host }}/anything' @@ -25,8 +24,10 @@ - assert: that: + - http_301_get is successful - http_301_get.json.data == '' - http_301_get.json.method == 'GET' + - http_301_get.json.url == 'http://{{ httpbin_host }}/anything' - http_301_get.redirected == true - http_301_get.status == 200 - http_301_get.url == 'http://{{ httpbin_host }}/anything' @@ -44,13 +45,14 @@ - assert: that: + - http_301_post is successful - http_301_post.json.data == '' - http_301_post.json.method == 'GET' + - http_301_post.json.url == 'http://{{ httpbin_host }}/anything' - http_301_post.redirected == true - http_301_post.status == 200 - http_301_post.url == 'http://{{ httpbin_host }}/anything' -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 302 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=302&url=http://{{ httpbin_host }}/anything @@ -61,8 +63,8 @@ - assert: that: - - http_302_head.json.data == '' - - http_302_head.json.method == 'GET' + - http_302_head is successful + - http_302_head.json is not defined - http_302_head.redirected == true - http_302_head.status == 200 - http_302_head.url == 'http://{{ httpbin_host }}/anything' @@ -77,8 +79,10 @@ - assert: that: + - http_302_get is successful - http_302_get.json.data == '' - http_302_get.json.method == 'GET' + - http_302_get.json.url == 'http://{{ httpbin_host }}/anything' - http_302_get.redirected == true - http_302_get.status == 200 - http_302_get.url == 'http://{{ httpbin_host }}/anything' @@ -96,13 +100,14 @@ - assert: that: + - http_302_post is successful - http_302_post.json.data == '' - http_302_post.json.method == 'GET' + - http_302_post.json.url == 'http://{{ httpbin_host }}/anything' - http_302_post.redirected == true - http_302_post.status == 200 - http_302_post.url == 'http://{{ httpbin_host }}/anything' -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 303 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=303&url=http://{{ httpbin_host }}/anything @@ -113,8 +118,8 @@ - assert: that: - - http_303_head.json.data == '' - - http_303_head.json.method == 'GET' + - http_303_head is successful + - http_303_head.json is not defined - http_303_head.redirected == true - http_303_head.status == 200 - http_303_head.url == 'http://{{ httpbin_host }}/anything' @@ -129,8 +134,10 @@ - assert: that: + - http_303_get is successful - http_303_get.json.data == '' - http_303_get.json.method == 'GET' + - http_303_get.json.url == 'http://{{ httpbin_host }}/anything' - http_303_get.redirected == true - http_303_get.status == 200 - http_303_get.url == 'http://{{ httpbin_host }}/anything' @@ -148,13 +155,14 @@ - assert: that: + - http_303_post is successful - http_303_post.json.data == '' - http_303_post.json.method == 'GET' + - http_303_post.json.url == 'http://{{ httpbin_host }}/anything' - http_303_post.redirected == true - http_303_post.status == 200 - http_303_post.url == 'http://{{ httpbin_host }}/anything' -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 307 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=307&url=http://{{ httpbin_host }}/anything @@ -165,8 +173,8 @@ - assert: that: - - http_307_head.json.data == '' - - http_307_head.json.method == 'GET' + - http_307_head is successful + - http_307_head.json is not defined - http_307_head.redirected == true - http_307_head.status == 200 - http_307_head.url == 'http://{{ httpbin_host }}/anything' @@ -181,13 +189,14 @@ - assert: that: + - http_307_get is successful - http_307_get.json.data == '' - http_307_get.json.method == 'GET' + - http_307_get.json.url == 'http://{{ httpbin_host }}/anything' - http_307_get.redirected == true - http_307_get.status == 200 - http_307_get.url == 'http://{{ httpbin_host }}/anything' -# FIXME: The HTTP POST turns into an HTTP GET. This is fixed in https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 307 using POST uri: url: http://{{ httpbin_host }}/redirect-to?status_code=307&url=http://{{ httpbin_host }}/anything @@ -200,51 +209,48 @@ - assert: that: - - http_307_post.json.data == '' - - http_307_post.json.method == 'GET' + - http_307_post is successful + - http_307_post.json.json.foo == 'bar' + - http_307_post.json.method == 'POST' + - http_307_post.json.url == 'http://{{ httpbin_host }}/anything' - http_307_post.redirected == true - http_307_post.status == 200 - http_307_post.url == 'http://{{ httpbin_host }}/anything' -# FIXME: This is fixed in https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 308 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything follow_redirects: all return_content: yes - method: GET - ignore_errors: yes + method: HEAD register: http_308_head - assert: that: - - http_308_head.json is not defined - - http_308_head.location == 'http://{{ httpbin_host }}/anything' - - "http_308_head.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" - - http_308_head.redirected == false - - http_308_head.status == 308 - - http_308_head.url == 'http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything' + - http_308_head is successful + - http_308_head.json is undefined + - http_308_head.redirected == true + - http_308_head.status == 200 + - http_308_head.url == 'http://{{ httpbin_host }}/anything' -# FIXME: This is fixed in https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 308 using GET uri: url: http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything follow_redirects: all return_content: yes method: GET - ignore_errors: yes register: http_308_get - assert: that: - - http_308_get.json is not defined - - http_308_get.location == 'http://{{ httpbin_host }}/anything' - - "http_308_get.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" - - http_308_get.redirected == false - - http_308_get.status == 308 - - http_308_get.url == 'http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything' + - http_308_get is successful + - http_308_get.json.data == '' + - http_308_get.json.method == 'GET' + - http_308_get.json.url == 'http://{{ httpbin_host }}/anything' + - http_308_get.redirected == true + - http_308_get.status == 200 + - http_308_get.url == 'http://{{ httpbin_host }}/anything' -# FIXME: This is fixed in https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 308 using POST uri: url: http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything @@ -253,14 +259,14 @@ method: POST body: '{ "foo": "bar" }' body_format: json - ignore_errors: yes register: http_308_post - assert: that: - - http_308_post.json is not defined - - http_308_post.location == 'http://{{ httpbin_host }}/anything' - - "http_308_post.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" - - http_308_post.redirected == false - - http_308_post.status == 308 - - http_308_post.url == 'http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything' + - http_308_post is successful + - http_308_post.json.json.foo == 'bar' + - http_308_post.json.method == 'POST' + - http_308_post.json.url == 'http://{{ httpbin_host }}/anything' + - http_308_post.redirected == true + - http_308_post.status == 200 + - http_308_post.url == 'http://{{ httpbin_host }}/anything' diff --git a/test/integration/targets/uri/tasks/redirect-none.yml b/test/integration/targets/uri/tasks/redirect-none.yml index e85b9b6f48d..dd2d3a4470f 100644 --- a/test/integration/targets/uri/tasks/redirect-none.yml +++ b/test/integration/targets/uri/tasks/redirect-none.yml @@ -9,6 +9,7 @@ - assert: that: + - http_301_head is failure - http_301_head.json is not defined - http_301_head.location == 'http://{{ httpbin_host }}/anything' - "http_301_head.msg == 'Status code was 301 and not [200]: HTTP Error 301: MOVED PERMANENTLY'" @@ -27,6 +28,7 @@ - assert: that: + - http_301_get is failure - http_301_get.json is not defined - http_301_get.location == 'http://{{ httpbin_host }}/anything' - "http_301_get.msg == 'Status code was 301 and not [200]: HTTP Error 301: MOVED PERMANENTLY'" @@ -47,6 +49,7 @@ - assert: that: + - http_301_post is failure - http_301_post.json is not defined - http_301_post.location == 'http://{{ httpbin_host }}/anything' - "http_301_post.msg == 'Status code was 301 and not [200]: HTTP Error 301: MOVED PERMANENTLY'" @@ -65,6 +68,7 @@ - assert: that: + - http_302_head is failure - http_302_head.json is not defined - http_302_head.location == 'http://{{ httpbin_host }}/anything' - "http_302_head.msg == 'Status code was 302 and not [200]: HTTP Error 302: FOUND'" @@ -83,6 +87,7 @@ - assert: that: + - http_302_get is failure - http_302_get.json is not defined - http_302_get.location == 'http://{{ httpbin_host }}/anything' - "http_302_get.msg == 'Status code was 302 and not [200]: HTTP Error 302: FOUND'" @@ -103,6 +108,7 @@ - assert: that: + - http_302_post is failure - http_302_post.json is not defined - http_302_post.location == 'http://{{ httpbin_host }}/anything' - "http_302_post.msg == 'Status code was 302 and not [200]: HTTP Error 302: FOUND'" @@ -121,6 +127,7 @@ - assert: that: + - http_303_head is failure - http_303_head.json is not defined - http_303_head.location == 'http://{{ httpbin_host }}/anything' - "http_303_head.msg == 'Status code was 303 and not [200]: HTTP Error 303: SEE OTHER'" @@ -139,6 +146,7 @@ - assert: that: + - http_303_get is failure - http_303_get.json is not defined - http_303_get.location == 'http://{{ httpbin_host }}/anything' - "http_303_get.msg == 'Status code was 303 and not [200]: HTTP Error 303: SEE OTHER'" @@ -159,6 +167,7 @@ - assert: that: + - http_303_post is failure - http_303_post.json is not defined - http_303_post.location == 'http://{{ httpbin_host }}/anything' - "http_303_post.msg == 'Status code was 303 and not [200]: HTTP Error 303: SEE OTHER'" @@ -177,6 +186,7 @@ - assert: that: + - http_307_head is failure - http_307_head.json is not defined - http_307_head.location == 'http://{{ httpbin_host }}/anything' - "http_307_head.msg == 'Status code was 307 and not [200]: HTTP Error 307: TEMPORARY REDIRECT'" @@ -195,6 +205,7 @@ - assert: that: + - http_307_get is failure - http_307_get.json is not defined - http_307_get.location == 'http://{{ httpbin_host }}/anything' - "http_307_get.msg == 'Status code was 307 and not [200]: HTTP Error 307: TEMPORARY REDIRECT'" @@ -215,6 +226,7 @@ - assert: that: + - http_307_post is failure - http_307_post.json is not defined - http_307_post.location == 'http://{{ httpbin_host }}/anything' - "http_307_post.msg == 'Status code was 307 and not [200]: HTTP Error 307: TEMPORARY REDIRECT'" @@ -234,6 +246,7 @@ - assert: that: + - http_308_head is failure - http_308_head.json is not defined - http_308_head.location == 'http://{{ httpbin_host }}/anything' - "http_308_head.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" @@ -253,6 +266,7 @@ - assert: that: + - http_308_get is failure - http_308_get.json is not defined - http_308_get.location == 'http://{{ httpbin_host }}/anything' - "http_308_get.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" @@ -273,6 +287,7 @@ - assert: that: + - http_308_post is failure - http_308_post.json is not defined - http_308_post.location == 'http://{{ httpbin_host }}/anything' - "http_308_post.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" diff --git a/test/integration/targets/uri/tasks/redirect-safe.yml b/test/integration/targets/uri/tasks/redirect-safe.yml index 2071487b22b..09ac1271ec7 100644 --- a/test/integration/targets/uri/tasks/redirect-safe.yml +++ b/test/integration/targets/uri/tasks/redirect-safe.yml @@ -1,4 +1,3 @@ -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 301 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=301&url=http://{{ httpbin_host }}/anything @@ -9,8 +8,8 @@ - assert: that: - - http_301_head.json.data == '' - - http_301_head.json.method == 'GET' + - http_301_head is successful + - http_301_head.json is not defined - http_301_head.redirected == true - http_301_head.status == 200 - http_301_head.url == 'http://{{ httpbin_host }}/anything' @@ -25,8 +24,10 @@ - assert: that: + - http_301_get is successful - http_301_get.json.data == '' - http_301_get.json.method == 'GET' + - http_301_get.json.url == 'http://{{ httpbin_host }}/anything' - http_301_get.redirected == true - http_301_get.status == 200 - http_301_get.url == 'http://{{ httpbin_host }}/anything' @@ -44,6 +45,7 @@ - assert: that: + - http_301_post is failure - http_301_post.json is not defined - http_301_post.location == 'http://{{ httpbin_host }}/anything' - "http_301_post.msg == 'Status code was 301 and not [200]: HTTP Error 301: MOVED PERMANENTLY'" @@ -51,7 +53,6 @@ - http_301_post.status == 301 - http_301_post.url == 'http://{{ httpbin_host }}/redirect-to?status_code=301&url=http://{{ httpbin_host }}/anything' -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 302 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=302&url=http://{{ httpbin_host }}/anything @@ -62,8 +63,8 @@ - assert: that: - - http_302_head.json.data == '' - - http_302_head.json.method == 'GET' + - http_302_head is successful + - http_302_head.json is not defined - http_302_head.redirected == true - http_302_head.status == 200 - http_302_head.url == 'http://{{ httpbin_host }}/anything' @@ -78,8 +79,10 @@ - assert: that: + - http_302_get is successful - http_302_get.json.data == '' - http_302_get.json.method == 'GET' + - http_302_get.json.url == 'http://{{ httpbin_host }}/anything' - http_302_get.redirected == true - http_302_get.status == 200 - http_302_get.url == 'http://{{ httpbin_host }}/anything' @@ -97,6 +100,7 @@ - assert: that: + - http_302_post is failure - http_302_post.json is not defined - http_302_post.location == 'http://{{ httpbin_host }}/anything' - "http_302_post.msg == 'Status code was 302 and not [200]: HTTP Error 302: FOUND'" @@ -104,7 +108,6 @@ - http_302_post.status == 302 - http_302_post.url == 'http://{{ httpbin_host }}/redirect-to?status_code=302&url=http://{{ httpbin_host }}/anything' -# NOTE: The HTTP HEAD turns into an HTTP GET - name: Test HTTP 303 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=303&url=http://{{ httpbin_host }}/anything @@ -115,8 +118,8 @@ - assert: that: - - http_303_head.json.data == '' - - http_303_head.json.method == 'GET' + - http_303_head is successful + - http_303_head.json is not defined - http_303_head.redirected == true - http_303_head.status == 200 - http_303_head.url == 'http://{{ httpbin_host }}/anything' @@ -131,8 +134,10 @@ - assert: that: + - http_303_get is successful - http_303_get.json.data == '' - http_303_get.json.method == 'GET' + - http_303_get.json.url == 'http://{{ httpbin_host }}/anything' - http_303_get.redirected == true - http_303_get.status == 200 - http_303_get.url == 'http://{{ httpbin_host }}/anything' @@ -150,6 +155,7 @@ - assert: that: + - http_303_post is failure - http_303_post.json is not defined - http_303_post.location == 'http://{{ httpbin_host }}/anything' - "http_303_post.msg == 'Status code was 303 and not [200]: HTTP Error 303: SEE OTHER'" @@ -167,8 +173,8 @@ - assert: that: - - http_307_head.json.data == '' - - http_307_head.json.method == 'GET' + - http_307_head is successful + - http_307_head.json is not defined - http_307_head.redirected == true - http_307_head.status == 200 - http_307_head.url == 'http://{{ httpbin_host }}/anything' @@ -183,8 +189,10 @@ - assert: that: + - http_307_get is successful - http_307_get.json.data == '' - http_307_get.json.method == 'GET' + - http_307_get.json.url == 'http://{{ httpbin_host }}/anything' - http_307_get.redirected == true - http_307_get.status == 200 - http_307_get.url == 'http://{{ httpbin_host }}/anything' @@ -202,6 +210,7 @@ - assert: that: + - http_307_post is failure - http_307_post.json is not defined - http_307_post.location == 'http://{{ httpbin_host }}/anything' - "http_307_post.msg == 'Status code was 307 and not [200]: HTTP Error 307: TEMPORARY REDIRECT'" @@ -209,43 +218,39 @@ - http_307_post.status == 307 - http_307_post.url == 'http://{{ httpbin_host }}/redirect-to?status_code=307&url=http://{{ httpbin_host }}/anything' -# NOTE: In my opinion this should work, see https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 308 using HEAD uri: url: http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything follow_redirects: safe return_content: yes - method: GET - ignore_errors: yes + method: HEAD register: http_308_head - assert: that: + - http_308_head is successful - http_308_head.json is not defined - - http_308_head.location == 'http://{{ httpbin_host }}/anything' - - "http_308_head.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" - - http_308_head.redirected == false - - http_308_head.status == 308 - - http_308_head.url == 'http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything' + - http_308_head.redirected == true + - http_308_head.status == 200 + - http_308_head.url == 'http://{{ httpbin_host }}/anything' -# NOTE: In my opinion this should work, see https://github.com/ansible/ansible/pull/36809 - name: Test HTTP 308 using GET uri: url: http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything follow_redirects: safe return_content: yes method: GET - ignore_errors: yes register: http_308_get - assert: that: - - http_308_get.json is not defined - - http_308_get.location == 'http://{{ httpbin_host }}/anything' - - "http_308_get.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" - - http_308_get.redirected == false - - http_308_get.status == 308 - - http_308_get.url == 'http://{{ httpbin_host }}/redirect-to?status_code=308&url=http://{{ httpbin_host }}/anything' + - http_308_get is successful + - http_308_get.json.data == '' + - http_308_get.json.method == 'GET' + - http_308_get.json.url == 'http://{{ httpbin_host }}/anything' + - http_308_get.redirected == true + - http_308_get.status == 200 + - http_308_get.url == 'http://{{ httpbin_host }}/anything' - name: Test HTTP 308 using POST uri: @@ -260,6 +265,7 @@ - assert: that: + - http_308_post is failure - http_308_post.json is not defined - http_308_post.location == 'http://{{ httpbin_host }}/anything' - "http_308_post.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" diff --git a/test/integration/targets/uri/tasks/redirect-urllib2.yml b/test/integration/targets/uri/tasks/redirect-urllib2.yml index ddcff0e11a7..c9eb909ea42 100644 --- a/test/integration/targets/uri/tasks/redirect-urllib2.yml +++ b/test/integration/targets/uri/tasks/redirect-urllib2.yml @@ -9,8 +9,10 @@ - assert: that: + - http_301_head is successful - http_301_head.json.data == '' - http_301_head.json.method == 'GET' + - http_301_head.json.url == 'http://{{ httpbin_host }}/anything' - http_301_head.redirected == true - http_301_head.status == 200 - http_301_head.url == 'http://{{ httpbin_host }}/anything' @@ -25,8 +27,10 @@ - assert: that: + - http_301_get is successful - http_301_get.json.data == '' - http_301_get.json.method == 'GET' + - http_301_get.json.url == 'http://{{ httpbin_host }}/anything' - http_301_get.redirected == true - http_301_get.status == 200 - http_301_get.url == 'http://{{ httpbin_host }}/anything' @@ -44,8 +48,10 @@ - assert: that: + - http_301_post is successful - http_301_post.json.data == '' - http_301_post.json.method == 'GET' + - http_301_post.json.url == 'http://{{ httpbin_host }}/anything' - http_301_post.redirected == true - http_301_post.status == 200 - http_301_post.url == 'http://{{ httpbin_host }}/anything' @@ -61,8 +67,10 @@ - assert: that: + - http_302_head is successful - http_302_head.json.data == '' - http_302_head.json.method == 'GET' + - http_302_head.json.url == 'http://{{ httpbin_host }}/anything' - http_302_head.redirected == true - http_302_head.status == 200 - http_302_head.url == 'http://{{ httpbin_host }}/anything' @@ -77,8 +85,10 @@ - assert: that: + - http_302_get is successful - http_302_get.json.data == '' - http_302_get.json.method == 'GET' + - http_302_get.json.url == 'http://{{ httpbin_host }}/anything' - http_302_get.redirected == true - http_302_get.status == 200 - http_302_get.url == 'http://{{ httpbin_host }}/anything' @@ -96,8 +106,10 @@ - assert: that: + - http_302_post is successful - http_302_post.json.data == '' - http_302_post.json.method == 'GET' + - http_302_post.json.url == 'http://{{ httpbin_host }}/anything' - http_302_post.redirected == true - http_302_post.status == 200 - http_302_post.url == 'http://{{ httpbin_host }}/anything' @@ -113,8 +125,10 @@ - assert: that: + - http_303_head is successful - http_303_head.json.data == '' - http_303_head.json.method == 'GET' + - http_303_head.json.url == 'http://{{ httpbin_host }}/anything' - http_303_head.redirected == true - http_303_head.status == 200 - http_303_head.url == 'http://{{ httpbin_host }}/anything' @@ -129,8 +143,10 @@ - assert: that: + - http_303_get is successful - http_303_get.json.data == '' - http_303_get.json.method == 'GET' + - http_303_get.json.url == 'http://{{ httpbin_host }}/anything' - http_303_get.redirected == true - http_303_get.status == 200 - http_303_get.url == 'http://{{ httpbin_host }}/anything' @@ -148,8 +164,10 @@ - assert: that: + - http_303_post is successful - http_303_post.json.data == '' - http_303_post.json.method == 'GET' + - http_303_post.json.url == 'http://{{ httpbin_host }}/anything' - http_303_post.redirected == true - http_303_post.status == 200 - http_303_post.url == 'http://{{ httpbin_host }}/anything' @@ -165,8 +183,10 @@ - assert: that: + - http_307_head is successful - http_307_head.json.data == '' - http_307_head.json.method == 'GET' + - http_307_head.json.url == 'http://{{ httpbin_host }}/anything' - http_307_head.redirected == true - http_307_head.status == 200 - http_307_head.url == 'http://{{ httpbin_host }}/anything' @@ -181,8 +201,10 @@ - assert: that: + - http_307_get is successful - http_307_get.json.data == '' - http_307_get.json.method == 'GET' + - http_307_get.json.url == 'http://{{ httpbin_host }}/anything' - http_307_get.redirected == true - http_307_get.status == 200 - http_307_get.url == 'http://{{ httpbin_host }}/anything' @@ -201,6 +223,7 @@ - assert: that: + - http_307_post is failure - http_307_post.json is not defined - http_307_post.location == 'http://{{ httpbin_host }}/anything' - "http_307_post.msg == 'Status code was 307 and not [200]: HTTP Error 307: TEMPORARY REDIRECT'" @@ -220,6 +243,7 @@ - assert: that: + - http_308_head is failure - http_308_head.json is not defined - http_308_head.location == 'http://{{ httpbin_host }}/anything' - "http_308_head.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" @@ -239,6 +263,7 @@ - assert: that: + - http_308_get is failure - http_308_get.json is not defined - http_308_get.location == 'http://{{ httpbin_host }}/anything' - "http_308_get.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'" @@ -260,6 +285,7 @@ - assert: that: + - http_308_post is failure - http_308_post.json is not defined - http_308_post.location == 'http://{{ httpbin_host }}/anything' - "http_308_post.msg == 'Status code was 308 and not [200]: HTTP Error 308: UNKNOWN'"