mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Take into account ContentType from Headers in WebCmdlets (#19227)
This commit is contained in:
+5
-3
@@ -1487,14 +1487,16 @@ namespace Microsoft.PowerShell.Commands
|
||||
ArgumentNullException.ThrowIfNull(content);
|
||||
|
||||
Encoding encoding = null;
|
||||
if (ContentType is not null)
|
||||
string contentType = WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType];
|
||||
|
||||
if (contentType is not null)
|
||||
{
|
||||
// If Content-Type contains the encoding format (as CharSet), use this encoding format
|
||||
// to encode the Body of the WebRequest sent to the server. Default Encoding format
|
||||
// would be used if Charset is not supplied in the Content-Type property.
|
||||
try
|
||||
{
|
||||
MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType);
|
||||
MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType);
|
||||
if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet))
|
||||
{
|
||||
encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet);
|
||||
@@ -1505,7 +1507,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
if (!SkipHeaderValidation)
|
||||
{
|
||||
ValidationMetadataException outerEx = new(WebCmdletStrings.ContentTypeException, ex);
|
||||
ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
|
||||
ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, contentType);
|
||||
ThrowTerminatingError(er);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -568,6 +568,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||
$Result.Output.Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
|
||||
}
|
||||
|
||||
It "Invoke-WebRequest -ContentType overwrites Content-Type from -Headers." {
|
||||
$uri = Get-WebListenerUrl -Test 'POST'
|
||||
$command = "Invoke-WebRequest -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'"
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Output.BaseResponse.RequestMessage.Content.Headers.ContentType.MediaType | Should -BeExactly 'application/json'
|
||||
}
|
||||
|
||||
It "Invoke-WebRequest supports sending request as UTF-8." {
|
||||
$uri = Get-WebListenerUrl -Test 'POST'
|
||||
# Body must contain non-ASCII characters
|
||||
@@ -576,7 +583,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||
$result = ExecuteWebCommand -command $command
|
||||
ValidateResponse -response $result
|
||||
|
||||
$Result.Output.Encoding.BodyName | Should -BeExactly 'utf-8'
|
||||
$result.Output.Encoding.BodyName | Should -BeExactly 'utf-8'
|
||||
$object = $Result.Output.Content | ConvertFrom-Json
|
||||
$object.Data | Should -BeExactly 'проверка'
|
||||
}
|
||||
@@ -2338,6 +2345,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||
$Result.Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
|
||||
}
|
||||
|
||||
It "Invoke-RestMethod -ContentType overwrites Content-Type from -Headers." {
|
||||
$uri = Get-WebListenerUrl -Test 'POST'
|
||||
$command = "Invoke-RestMethod -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'"
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Output.headers."Content-Type" | Should -BeExactly 'application/json'
|
||||
}
|
||||
|
||||
It "Invoke-RestMethod supports sending requests as UTF8" {
|
||||
$uri = Get-WebListenerUrl -Test POST
|
||||
# Body must contain non-ASCII characters
|
||||
|
||||
Reference in New Issue
Block a user