Set request headers when request body is empty in Web Cmdlets (#10034)
This commit is contained in:
parent
6d8dd92759
commit
2285ece613
|
@ -1241,8 +1241,12 @@ namespace Microsoft.PowerShell.Commands
|
|||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
if (SkipHeaderValidation)
|
||||
|
@ -1264,7 +1268,6 @@ namespace Microsoft.PowerShell.Commands
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the status code is one of the supported redirection codes.
|
||||
private static bool IsRedirectCode(HttpStatusCode code)
|
||||
|
|
|
@ -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
|
||||
|
@ -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" {
|
||||
|
|
Loading…
Reference in a new issue