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 @@ -1277,9 +1277,15 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
}
}

// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ $redirectTests = @(

Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
BeforeAll {
$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$WebListener = Start-WebListener
$NotFoundQuery = @{
statuscode = 404
Expand All @@ -380,6 +382,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
}
}

AfterAll {
$ProgressPreference = $oldProgress
}

# Validate the output of Invoke-WebRequest
#
function ValidateResponse {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 = @{
Expand All @@ -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'
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'
Expand Down