Set request headers when request body is empty in Web Cmdlets (#10034)

This commit is contained in:
Mark Kraus 2019-07-02 12:01:06 -07:00 committed by Travis Plunk
parent 6d8dd92759
commit 2285ece613
2 changed files with 60 additions and 15 deletions

View file

@ -1241,26 +1241,29 @@ namespace Microsoft.PowerShell.Commands
}
// Add the content headers
if (request.Content != null)
if (request.Content == null)
{
request.Content = new StringContent(string.Empty);
request.Content.Headers.Clear();
}
foreach (var entry in WebSession.ContentHeaders)
{
foreach (var entry in WebSession.ContentHeaders)
if (SkipHeaderValidation)
{
if (SkipHeaderValidation)
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
}
else
{
try
{
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
request.Content.Headers.Add(entry.Key, entry.Value);
}
else
catch (FormatException ex)
{
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);
}
var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex);
ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
ThrowTerminatingError(er);
}
}
}

View file

@ -993,6 +993,28 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.data | Should -Match 'bar'
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
It "Verifies Invoke-WebRequest applies -ContentType when no -Body is present" {
$contentType = 'application/json'
$uri = Get-WebListenerUrl -Test 'Get'
$response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType
$result = $response.Content | ConvertFrom-Json
$result.data | Should -BeNullOrEmpty
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
It "Verifies Invoke-WebRequest applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Get'
$response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation
$result = $response.Content | ConvertFrom-Json
$result.data | Should -BeNullOrEmpty
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
}
#region charset encoding tests
@ -2388,6 +2410,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.data | Should -Match 'bar'
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
It "Verifies Invoke-RestMethod applies -ContentType when no -Body is present" {
$contentType = 'application/json'
$uri = Get-WebListenerUrl -Test 'Get'
$result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType
$result.data | Should -BeNullOrEmpty
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
It "Verifies Invoke-RestMethod applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Get'
$result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation
$result.data | Should -BeNullOrEmpty
$result.headers.'Content-Type' | Should -BeExactly $contentType
}
}
Context "HTTPS Tests" {