- name: Test HTTP 301 using HEAD
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=301&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: HEAD
  register: http_301_head

- assert:
    that:
    - 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 == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 301 using GET
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=301&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: GET
  register: http_301_get

- assert:
    that:
    - http_301_get is successful
    - http_301_get.json.data == ''
    - http_301_get.json.method == 'GET'
    - http_301_get.json.url == 'https://{{ httpbin_host }}/anything'
    - http_301_get.redirected == true
    - http_301_get.status == 200
    - http_301_get.url == 'https://{{ httpbin_host }}/anything'

# NOTE: The HTTP POST turns into an HTTP GET
- name: Test HTTP 301 using POST
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=301&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: POST
    body: '{ "foo": "bar" }'
    body_format: json
  register: http_301_post

- assert:
    that:
    - http_301_post is successful
    - http_301_post.json.data == ''
    - http_301_post.json.method == 'GET'
    - http_301_post.json.url == 'https://{{ httpbin_host }}/anything'
    - http_301_post.redirected == true
    - http_301_post.status == 200
    - http_301_post.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 302 using HEAD
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=302&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: HEAD
  register: http_302_head

- assert:
    that:
    - 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 == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 302 using GET
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=302&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: GET
  register: http_302_get

- assert:
    that:
    - http_302_get is successful
    - http_302_get.json.data == ''
    - http_302_get.json.method == 'GET'
    - http_302_get.json.url == 'https://{{ httpbin_host }}/anything'
    - http_302_get.redirected == true
    - http_302_get.status == 200
    - http_302_get.url == 'https://{{ httpbin_host }}/anything'

# NOTE: The HTTP POST turns into an HTTP GET
- name: Test HTTP 302 using POST
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=302&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: POST
    body: '{ "foo": "bar" }'
    body_format: json
  register: http_302_post

- assert:
    that:
    - http_302_post is successful
    - http_302_post.json.data == ''
    - http_302_post.json.method == 'GET'
    - http_302_post.json.url == 'https://{{ httpbin_host }}/anything'
    - http_302_post.redirected == true
    - http_302_post.status == 200
    - http_302_post.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 303 using HEAD
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=303&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: HEAD
  register: http_303_head

- assert:
    that:
    - 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 == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 303 using GET
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=303&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: GET
  register: http_303_get

- assert:
    that:
    - http_303_get is successful
    - http_303_get.json.data == ''
    - http_303_get.json.method == 'GET'
    - http_303_get.json.url == 'https://{{ httpbin_host }}/anything'
    - http_303_get.redirected == true
    - http_303_get.status == 200
    - http_303_get.url == 'https://{{ httpbin_host }}/anything'

# NOTE: The HTTP POST turns into an HTTP GET
- name: Test HTTP 303 using POST
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=303&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: POST
    body: '{ "foo": "bar" }'
    body_format: json
  register: http_303_post

- assert:
    that:
    - http_303_post is successful
    - http_303_post.json.data == ''
    - http_303_post.json.method == 'GET'
    - http_303_post.json.url == 'https://{{ httpbin_host }}/anything'
    - http_303_post.redirected == true
    - http_303_post.status == 200
    - http_303_post.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 307 using HEAD
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=307&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: HEAD
  register: http_307_head

- assert:
    that:
    - 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 == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 307 using GET
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=307&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: GET
  register: http_307_get

- assert:
    that:
    - http_307_get is successful
    - http_307_get.json.data == ''
    - http_307_get.json.method == 'GET'
    - http_307_get.json.url == 'https://{{ httpbin_host }}/anything'
    - http_307_get.redirected == true
    - http_307_get.status == 200
    - http_307_get.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 307 using POST
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=307&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: POST
    body: '{ "foo": "bar" }'
    body_format: json
  register: http_307_post

- assert:
    that:
    - http_307_post is successful
    - http_307_post.json.json.foo == 'bar'
    - http_307_post.json.method == 'POST'
    - http_307_post.json.url == 'https://{{ httpbin_host }}/anything'
    - http_307_post.redirected == true
    - http_307_post.status == 200
    - http_307_post.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 308 using HEAD
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=308&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: HEAD
  register: http_308_head

- assert:
    that:
    - 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 == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 308 using GET
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=308&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: GET
  register: http_308_get

- assert:
    that:
    - http_308_get is successful
    - http_308_get.json.data == ''
    - http_308_get.json.method == 'GET'
    - http_308_get.json.url == 'https://{{ httpbin_host }}/anything'
    - http_308_get.redirected == true
    - http_308_get.status == 200
    - http_308_get.url == 'https://{{ httpbin_host }}/anything'

- name: Test HTTP 308 using POST
  uri:
    url: https://{{ httpbin_host }}/redirect-to?status_code=308&url=https://{{ httpbin_host }}/anything
    follow_redirects: all
    return_content: yes
    method: POST
    body: '{ "foo": "bar" }'
    body_format: json
  register: http_308_post

- assert:
    that:
    - http_308_post is successful
    - http_308_post.json.json.foo == 'bar'
    - http_308_post.json.method == 'POST'
    - http_308_post.json.url == 'https://{{ httpbin_host }}/anything'
    - http_308_post.redirected == true
    - http_308_post.status == 200
    - http_308_post.url == 'https://{{ httpbin_host }}/anything'
