From 2285ece613972de369171bd6fd4977df2d3e0427 Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Tue, 2 Jul 2019 12:01:06 -0700 Subject: [PATCH] Set request headers when request body is empty in Web Cmdlets (#10034) --- .../Common/WebRequestPSCmdlet.Common.cs | 33 ++++++++------- .../WebCmdlets.Tests.ps1 | 42 +++++++++++++++++++ 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index d082020bb..6cdb5ecfb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1241,26 +1241,29 @@ namespace Microsoft.PowerShell.Commands } // Add the content headers - if (request.Content != null) + if (request.Content == null) + { + request.Content = new StringContent(string.Empty); + request.Content.Headers.Clear(); + } + + foreach (var entry in WebSession.ContentHeaders) { - foreach (var entry in WebSession.ContentHeaders) + if (SkipHeaderValidation) { - if (SkipHeaderValidation) + request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); + } + else + { + try { - request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); + request.Content.Headers.Add(entry.Key, entry.Value); } - else + catch (FormatException ex) { - try - { - request.Content.Headers.Add(entry.Key, entry.Value); - } - catch (FormatException ex) - { - var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); - ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); - ThrowTerminatingError(er); - } + var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); + ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); + ThrowTerminatingError(er); } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 304732528..c20cf7d71 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -993,6 +993,28 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.data | Should -Match 'bar' $result.headers.'Content-Type' | Should -BeExactly $contentType } + + It "Verifies Invoke-WebRequest applies -ContentType when no -Body is present" { + $contentType = 'application/json' + $uri = Get-WebListenerUrl -Test 'Get' + + $response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType + $result = $response.Content | ConvertFrom-Json + + $result.data | Should -BeNullOrEmpty + $result.headers.'Content-Type' | Should -BeExactly $contentType + } + + It "Verifies Invoke-WebRequest applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" { + $contentType = 'foo' + $uri = Get-WebListenerUrl -Test 'Get' + + $response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation + $result = $response.Content | ConvertFrom-Json + + $result.data | Should -BeNullOrEmpty + $result.headers.'Content-Type' | Should -BeExactly $contentType + } } #region charset encoding tests @@ -2388,6 +2410,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.data | Should -Match 'bar' $result.headers.'Content-Type' | Should -BeExactly $contentType } + + It "Verifies Invoke-RestMethod applies -ContentType when no -Body is present" { + $contentType = 'application/json' + $uri = Get-WebListenerUrl -Test 'Get' + + $result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType + + $result.data | Should -BeNullOrEmpty + $result.headers.'Content-Type' | Should -BeExactly $contentType + } + + It "Verifies Invoke-RestMethod applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" { + $contentType = 'foo' + $uri = Get-WebListenerUrl -Test 'Get' + + $result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation + + $result.data | Should -BeNullOrEmpty + $result.headers.'Content-Type' | Should -BeExactly $contentType + } } Context "HTTPS Tests" {