Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
markekraus marked this conversation as resolved.
Show resolved Hide resolved
{
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
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