Fix web cmdlets so that an empty Get does not include a content-length header (#16587)

This commit is contained in:
Steve Lee
2022-01-03 11:20:33 -08:00
committed by GitHub
parent bf8a13908d
commit 7a2b44523b
2 changed files with 44 additions and 2 deletions
@@ -1277,9 +1277,15 @@ namespace Microsoft.PowerShell.Commands
}
}
// Add the content headers
if (request.Content == null)
// For other methods like Put where empty content has meaning, we need to fill in the content
if (request.Content is null)
{
// If this is a Get request and there is no content, then don't fill in the content as empty content gets rejected by some web services per RFC7230
if ((IsStandardMethodSet() && request.Method == HttpMethod.Get && ContentType is null) || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET"))
{
return;
}
request.Content = new StringContent(string.Empty);
request.Content.Headers.Clear();
}
@@ -370,6 +370,8 @@ $redirectTests = @(
Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
BeforeAll {
$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$WebListener = Start-WebListener
$NotFoundQuery = @{
statuscode = 404
@@ -380,6 +382,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
}
}
AfterAll {
$ProgressPreference = $oldProgress
}
# Validate the output of Invoke-WebRequest
#
function ValidateResponse {
@@ -456,6 +462,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate response
ValidateResponse -response $result
$result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty
}
It "Validate Invoke-WebRequest -DisableKeepAlive" {
@@ -705,6 +713,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
($result.Output.Content | ConvertFrom-Json).args.testparam | Should -Be "testvalue"
}
It 'Validate Invoke-WebRequest empty body CustomMethod GET' {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET"
$result = ExecuteWebCommand -command $command
$result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty
}
It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET and -NoProxy" {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'} -NoProxy"
@@ -2061,6 +2076,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
BeforeAll {
$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$WebListener = Start-WebListener
$NotFoundQuery = @{
@@ -2072,6 +2090,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
}
}
AfterAll {
$ProgressPreference = $oldProgress
}
#User-Agent changes on different platforms, so tests should only be run if on the correct platform
It "Invoke-RestMethod returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) {
$uri = Get-WebListenerUrl -Test 'Get'
@@ -2111,6 +2133,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate response
$result.Error | Should -BeNullOrEmpty
$result.Output.headers.'Content-Length' | Should -Be 0
}
It "Invoke-RestMethod returns headers dictionary" {
@@ -2350,6 +2373,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output.args.testparam | Should -Be "testvalue"
}
It 'Validate Invoke-RestMethod empty body CustomMethod GET' {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET"
$result = ExecuteWebCommand -command $command
$result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty
}
It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET and -NoProxy" {
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'} -NoProxy"
@@ -3528,9 +3558,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature", "RequireAdminOnWindows" {
BeforeAll {
$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$WebListener = Start-WebListener
}
AfterAll {
$ProgressPreference = $oldProgress
}
Context "InFile parameter negative tests" {
BeforeAll {
$uri = Get-WebListenerUrl -Test 'Post'