Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1260,21 +1260,24 @@ internal virtual void FillRequestStream(HttpRequestMessage request)

foreach (var entry in WebSession.ContentHeaders)
{
if (SkipHeaderValidation)
{
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
}
else
if (!string.IsNullOrWhiteSpace(entry.Value))
{
try
if (SkipHeaderValidation)
Copy link
Copy Markdown
Collaborator

@iSazonov iSazonov Dec 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If our intention is to follow Core (that is to send headers with empty value) the check should be here: Update.

Also please add a test.

{
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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'"
Expand Down