From c38617a7368df22731fceee201bb5649f2181c0a Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 9 Apr 2018 21:25:08 -0500 Subject: [PATCH] fix for when status codes are provided as a comma separated list (#38080) * fix for when status codes are provided as an array of strings * convert status codes to int, additional tests --- lib/ansible/modules/windows/win_uri.ps1 | 13 +++++++- .../targets/win_uri/tasks/test.yml | 33 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/windows/win_uri.ps1 b/lib/ansible/modules/windows/win_uri.ps1 index c259ca5fcf..7d04055d61 100644 --- a/lib/ansible/modules/windows/win_uri.ps1 +++ b/lib/ansible/modules/windows/win_uri.ps1 @@ -56,6 +56,17 @@ if ($use_basic_parsing) { Add-DeprecationWarning -obj $result -message "Since Ansible 2.5, use_basic_parsing does not change any behaviour, this option will be removed" -version 2.7 } +if ($status_code) { + $status_code = foreach ($code in $status_code) { + try { + [int]$code + } + catch [System.InvalidCastException] { + Fail-Json -obj $result -message "Failed to convert '$code' to an integer. Status codes must be provided in numeric format." + } + } +} + $client = [System.Net.WebRequest]::Create($url) $client.Method = $method $client.Timeout = $timeout * 1000 @@ -265,7 +276,7 @@ if ($return_content -or $dest) { } if ($status_code -notcontains $response.StatusCode) { - Fail-Json -obj $result -message "Status code of request '$($response.StatusCode)' is not in list of valid status codes $status_code." + Fail-Json -obj $result -message "Status code of request '$([int]$response.StatusCode)' is not in list of valid status codes $status_code : '$($response.StatusCode)'." } Exit-Json -obj $result diff --git a/test/integration/targets/win_uri/tasks/test.yml b/test/integration/targets/win_uri/tasks/test.yml index 176e303e10..55789479cb 100644 --- a/test/integration/targets/win_uri/tasks/test.yml +++ b/test/integration/targets/win_uri/tasks/test.yml @@ -252,6 +252,7 @@ url: http://{{httpbin_host}}/status/202 status_code: - 202 + - 418 method: POST body: foo register: status_code_check @@ -359,3 +360,35 @@ - post_request_with_custom_headers.json.headers['Content-Type'] == "application/json" - post_request_with_custom_headers.json.headers['Test-Header'] == 'hello' - post_request_with_custom_headers.json.headers['Another-Header'] == 'world' + +- name: validate status codes as list of strings + win_uri: + url: https://httpbin.org/status/202 + status_code: + - '202' + - '418' + method: POST + body: foo + return_content: yes + register: request_status_code_string + +- name: assert status codes as list of strings + assert: + that: + - not request_status_code_string.changed + - request_status_code_string.status_code == 202 + +- name: validate status codes as comma separated list + win_uri: + url: https://httpbin.org/status/202 + status_code: 202, 418 + method: POST + body: foo + return_content: yes + register: request_status_code_comma + +- name: assert status codes as comma separated list + assert: + that: + - not request_status_code_comma.changed + - request_status_code_comma.status_code == 202 \ No newline at end of file