From 886a88a96725d8066f4bb418c4d07058126e6c47 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 3 Apr 2018 08:30:09 +1000 Subject: [PATCH] win_uri: backport fix custom header override (#37889) * append request headers instead of replacing (#37845) (cherry picked from commit 2c44061a044ba5ae3f0648ea8d6f7ae5801e0922) * Added changelog fragment --- .../win_uri-fix-header-override.yaml | 3 +++ lib/ansible/modules/windows/win_uri.ps1 | 2 +- .../targets/win_uri/tasks/test.yml | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/win_uri-fix-header-override.yaml diff --git a/changelogs/fragments/win_uri-fix-header-override.yaml b/changelogs/fragments/win_uri-fix-header-override.yaml new file mode 100644 index 0000000000..edbb288cbf --- /dev/null +++ b/changelogs/fragments/win_uri-fix-header-override.yaml @@ -0,0 +1,3 @@ +bugfixes: +- win_uri - do not override existing header when using the ``headers`` key. + https://github.com/ansible/ansible/pull/37845 diff --git a/lib/ansible/modules/windows/win_uri.ps1 b/lib/ansible/modules/windows/win_uri.ps1 index f3a2343801..c259ca5fcf 100644 --- a/lib/ansible/modules/windows/win_uri.ps1 +++ b/lib/ansible/modules/windows/win_uri.ps1 @@ -125,7 +125,7 @@ if ($headers) { default { $req_headers.Add($header.Name, $header.Value) } } } - $client.Headers = $req_headers + $client.Headers.Add($req_headers) } if ($client_cert) { diff --git a/test/integration/targets/win_uri/tasks/test.yml b/test/integration/targets/win_uri/tasks/test.yml index 206648d743..176e303e10 100644 --- a/test/integration/targets/win_uri/tasks/test.yml +++ b/test/integration/targets/win_uri/tasks/test.yml @@ -338,3 +338,24 @@ - invalid_path.content is defined - invalid_path.method == 'GET' - invalid_path.connection is defined + +- name: post request with custom headers + win_uri: + url: http://{{httpbin_host}}/post + method: POST + headers: + Test-Header: hello + Another-Header: world + content_type: application/json + body: '{"foo": "bar"}' + return_content: yes + register: post_request_with_custom_headers + +- name: assert post with custom headers + assert: + that: + - not post_request_with_custom_headers.changed + - post_request_with_custom_headers.status_code == 200 + - 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'