Fix WebCmdlets when -Body is specified but ContentType is not (#23952)

This commit is contained in:
CarloToso
2024-07-01 15:35:23 -07:00
committed by GitHub
parent ab43c19390
commit 86a7262a71
2 changed files with 48 additions and 3 deletions
@@ -1575,9 +1575,8 @@ namespace Microsoft.PowerShell.Commands
ArgumentNullException.ThrowIfNull(content);
Encoding encoding = null;
string contentType = WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType];
if (contentType is not null)
if (WebSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out string contentType) && contentType is not null)
{
// If Content-Type contains the encoding format (as CharSet), use this encoding format
// to encode the Body of the WebRequest sent to the server. Default Encoding format
@@ -692,8 +692,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method -ContentType $contentType"
$commandNoContentType = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method"
It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
@@ -705,6 +706,29 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate that the response Content.data field is the same as what we sent.
$jsonContent.data | Should -Be $body
}
It "Invoke-WebRequest -Uri $uri -Method $method -Body [body data]" {
$result = ExecuteWebCommand -command $commandNoContentType
ValidateResponse -response $result
# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.url | Should -Match $uri
if ($method -eq "POST")
{
$jsonContent.headers.'Content-Type' | Should -Match "application/x-www-form-urlencoded"
# Validate that the response Content.form field is the same as what we sent.
[string]$jsonContent.form | Should -Be ([string][PSCustomObject]@{$body.Split("=")[0] = [System.Object[]]})
$jsonContent.data | Should -BeNullOrEmpty
}
else
{
# Validate that the response Content.data field is the same as what we sent.
$jsonContent.data | Should -Be $body
}
}
}
}
@@ -2739,6 +2763,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method -ContentType $contentType"
$commandNoContentType = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method"
It "Invoke-RestMethod -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
@@ -2751,6 +2776,27 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate that the response Content.data field is the same as what we sent.
$result.Output.data | Should -Be $body
}
It "Invoke-RestMethod -Uri $uri -Method $method -Body [body data]" {
$result = ExecuteWebCommand -command $commandNoContentType
# Validate response
$result.Output.url | Should -Match $uri
if ($method -eq "POST")
{
$result.Output.headers.'Content-Type' | Should -Match "application/x-www-form-urlencoded"
# Validate that the response Content.form field is the same as what we sent.
[string]$result.Output.form | Should -Be ([string][PSCustomObject]@{$body.Split("=")[0] = [System.Object[]]})
$result.Output.data | Should -BeNullOrEmpty
}
else
{
# Validate that the response Content.data field is the same as what we sent.
$result.Output.data | Should -Be $body
}
}
}
}