Ignore headers with null values in web cmdlets (#11424)

This commit is contained in:
Ilya
2020-01-07 12:25:30 -08:00
committed by Travis Plunk
parent 8cb75f7e02
commit e8d472b65b
2 changed files with 24 additions and 2 deletions
@@ -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
}
}