Make web cmdlets skip processing the content headers with a null or empty value for backward compatibility (#11421)

This commit is contained in:
spongemike2
2020-01-06 12:11:55 -08:00
committed by Dongbo Wang
parent 7658af4532
commit 1376d310bb
2 changed files with 34 additions and 11 deletions
@@ -1260,21 +1260,24 @@ namespace Microsoft.PowerShell.Commands
foreach (var entry in WebSession.ContentHeaders)
{
if (SkipHeaderValidation)
if (!string.IsNullOrWhiteSpace(entry.Value))
{
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
}
else
{
try
if (SkipHeaderValidation)
{
request.Content.Headers.Add(entry.Key, entry.Value);
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
}
catch (FormatException ex)
else
{
var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex);
ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
ThrowTerminatingError(er);
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);
}
}
}
}
@@ -447,6 +447,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$jsonContent.headers.Host | Should -Be $Uri.Authority
}
It "Invoke-WebRequest with blank ContentType succeeds" {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -ContentType ''"
$result = ExecuteWebCommand -command $command
# Validate response
ValidateResponse -response $result
}
It "Validate Invoke-WebRequest -DisableKeepAlive" {
# Operation options
$uri = Get-WebListenerUrl -Test 'Get'
@@ -1995,6 +2005,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output.headers.'User-Agent' | Should -MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
}
It "Invoke-RestMethod with blank ContentType succeeds" {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -ContentType ''"
$result = ExecuteWebCommand -command $command
# Validate response
$result.Error | Should -BeNullOrEmpty
}
It "Invoke-RestMethod returns headers dictionary" {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri'"