Adjust PUT method behavior to POST one for default content type in WebCmdlets (#19152)

This commit is contained in:
CarloToso
2023-03-02 23:35:31 +05:00
committed by GitHub
parent 8d8f45e3df
commit 8b9544b614
2 changed files with 16 additions and 7 deletions
@@ -1109,7 +1109,7 @@ namespace Microsoft.PowerShell.Commands
{
WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType;
}
else if (request.Method == HttpMethod.Post)
else if (request.Method == HttpMethod.Post || request.Method == HttpMethod.Put)
{
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
WebSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out string contentType);
@@ -759,9 +759,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
($result.Output.Content | ConvertFrom-Json).method | Should -Be "TEST"
}
It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" {
$uri = Get-WebListenerUrl -Test 'Post'
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
It "Validate Invoke-WebRequest default ContentType for CustomMethod <method>" -TestCases @(
@{method = "POST"}
@{method = "PUT"}
) {
param($method)
$uri = Get-WebListenerUrl -Test $method
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod $method -Body 'testparam=testvalue'"
$result = ExecuteWebCommand -command $command
$jsonResult = $result.Output.Content | ConvertFrom-Json
$jsonResult.form.testparam | Should -Be "testvalue"
@@ -2559,9 +2564,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output.method | Should -Be "TEST"
}
It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" {
$uri = Get-WebListenerUrl -Test 'Post'
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
It "Validate Invoke-RestMethod default ContentType for CustomMethod <method>" -TestCases @(
@{method = "POST"}
@{method = "PUT"}
) {
param($method)
$uri = Get-WebListenerUrl -Test $method
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod $method -Body 'testparam=testvalue'"
$result = ExecuteWebCommand -command $command
$result.Output.form.testparam | Should -Be "testvalue"
$result.Output.Headers.'Content-Type' | Should -Be "application/x-www-form-urlencoded"