mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Make web cmdlets skip processing the content headers with a null or empty value for backward compatibility (#11421)
This commit is contained in:
+14
-11
@@ -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'"
|
||||
|
||||
Reference in New Issue
Block a user