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 @@ -1241,26 +1241,29 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
}

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