FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Set request headers when request body is empty in Web Cmdlets (#10034) · PowerShell/PowerShell@2285ece · GitHub

Commit 2285ece

Browse files
authored andcommitted
Set request headers when request body is empty in Web Cmdlets (#10034)
1 parent 6d8dd92 commit 2285ece

2 files changed

Lines changed: 60 additions & 15 deletions

File tree

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,26 +1241,29 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
12411241
}
12421242

12431243
// Add the content headers
1244-
if (request.Content != null)
1244+
if (request.Content == null)
1245+
{
1246+
request.Content = new StringContent(string.Empty);
1247+
request.Content.Headers.Clear();
1248+
}
1249+
1250+
foreach (var entry in WebSession.ContentHeaders)
12451251
{
1246-
foreach (var entry in WebSession.ContentHeaders)
1252+
if (SkipHeaderValidation)
12471253
{
1248-
if (SkipHeaderValidation)
1254+
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
1255+
}
1256+
else
1257+
{
1258+
try
12491259
{
1250-
request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value);
1260+
request.Content.Headers.Add(entry.Key, entry.Value);
12511261
}
1252-
else
1262+
catch (FormatException ex)
12531263
{
1254-
try
1255-
{
1256-
request.Content.Headers.Add(entry.Key, entry.Value);
1257-
}
1258-
catch (FormatException ex)
1259-
{
1260-
var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex);
1261-
ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
1262-
ThrowTerminatingError(er);
1263-
}
1264+
var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex);
1265+
ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
1266+
ThrowTerminatingError(er);
12641267
}
12651268
}
12661269
}

‎test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,28 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
993993
$result.data | Should -Match 'bar'
994994
$result.headers.'Content-Type' | Should -BeExactly $contentType
995995
}
996+
997+
It "Verifies Invoke-WebRequest applies -ContentType when no -Body is present" {
998+
$contentType = 'application/json'
999+
$uri = Get-WebListenerUrl -Test 'Get'
1000+
1001+
$response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType
1002+
$result = $response.Content | ConvertFrom-Json
1003+
1004+
$result.data | Should -BeNullOrEmpty
1005+
$result.headers.'Content-Type' | Should -BeExactly $contentType
1006+
}
1007+
1008+
It "Verifies Invoke-WebRequest applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" {
1009+
$contentType = 'foo'
1010+
$uri = Get-WebListenerUrl -Test 'Get'
1011+
1012+
$response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation
1013+
$result = $response.Content | ConvertFrom-Json
1014+
1015+
$result.data | Should -BeNullOrEmpty
1016+
$result.headers.'Content-Type' | Should -BeExactly $contentType
1017+
}
9961018
}
9971019

9981020
#region charset encoding tests
@@ -2388,6 +2410,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
23882410
$result.data | Should -Match 'bar'
23892411
$result.headers.'Content-Type' | Should -BeExactly $contentType
23902412
}
2413+
2414+
It "Verifies Invoke-RestMethod applies -ContentType when no -Body is present" {
2415+
$contentType = 'application/json'
2416+
$uri = Get-WebListenerUrl -Test 'Get'
2417+
2418+
$result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType
2419+
2420+
$result.data | Should -BeNullOrEmpty
2421+
$result.headers.'Content-Type' | Should -BeExactly $contentType
2422+
}
2423+
2424+
It "Verifies Invoke-RestMethod applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" {
2425+
$contentType = 'foo'
2426+
$uri = Get-WebListenerUrl -Test 'Get'
2427+
2428+
$result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation
2429+
2430+
$result.data | Should -BeNullOrEmpty
2431+
$result.headers.'Content-Type' | Should -BeExactly $contentType
2432+
}
23912433
}
23922434

23932435
Context "HTTPS Tests" {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL