mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Ignore headers with null values in web cmdlets (#11424)
This commit is contained in:
+9
-2
@@ -663,8 +663,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
foreach (string key in Headers.Keys)
|
||||
{
|
||||
// add the header value (or overwrite it if already present)
|
||||
WebSession.Headers[key] = Headers[key].ToString();
|
||||
var value = Headers[key];
|
||||
|
||||
// null is not valid value for header.
|
||||
// We silently ignore header if value is null.
|
||||
if (!(value is null))
|
||||
{
|
||||
// add the header value (or overwrite it if already present)
|
||||
WebSession.Headers[key] = value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3496,4 +3496,19 @@ Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI", "RequireAdmi
|
||||
$result = Invoke-RestMethod $uri
|
||||
$result.Hello | Should -Be "world"
|
||||
}
|
||||
|
||||
It "Web cmdlets ignore headers with null value" {
|
||||
$query = @{
|
||||
body = "hello"
|
||||
contenttype = 'text/plain'
|
||||
}
|
||||
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
|
||||
|
||||
# Core throws if a header has null value.
|
||||
# We ignore such headers so no exception is expected.
|
||||
{ Invoke-WebRequest -Uri $uri -Headers @{ "Location" = $null } } | Should -Not -Throw
|
||||
{ Invoke-WebRequest -Uri $uri -ContentType $null } | Should -Not -Throw
|
||||
{ Invoke-RestMethod -Uri $uri -Headers @{ "Location" = $null } } | Should -Not -Throw
|
||||
{ Invoke-RestMethod -Uri $uri -ContentType $null } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user