Fix using xml -Body in webcmdlets without an encoding (#19281)

This commit is contained in:
CarloToso
2023-03-07 09:35:36 +05:00
committed by GitHub
parent 1925cf67af
commit 6824d489bb
2 changed files with 18 additions and 1 deletions
@@ -1528,7 +1528,7 @@ namespace Microsoft.PowerShell.Commands
byte[] bytes = null;
XmlDocument doc = xmlNode as XmlDocument;
if (doc?.FirstChild is XmlDeclaration decl)
if (doc?.FirstChild is XmlDeclaration decl && !string.IsNullOrEmpty(decl.Encoding))
{
Encoding encoding = Encoding.GetEncoding(decl.Encoding);
bytes = StreamHelper.EncodeToBytes(doc.OuterXml, encoding);
@@ -588,6 +588,15 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$object.Data | Should -BeExactly 'проверка'
}
It "Invoke-WebRequest supports sending XML requests without encoding" {
$uri = Get-WebListenerUrl -Test POST
$body = '<?xml version="1.0"?><foo />'
$result = Invoke-WebRequest -Uri $uri -body ([xml]$body) -ContentType 'text/xml' -method 'POST'
$object = $result.Content | ConvertFrom-Json
$object.Data | Should -BeExactly $body
}
It "Invoke-WebRequest supports request that returns page containing CodPage 936 data." {
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
$command = "Invoke-WebRequest -Uri '$uri'"
@@ -2402,6 +2411,14 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output.Data | Should -BeExactly 'проверка'
}
It "Invoke-RestMethod supports sending XML requests without encoding" {
$uri = Get-WebListenerUrl -Test POST
$body = '<?xml version="1.0"?><foo />'
$result = Invoke-RestMethod -Uri $uri -body ([xml]$body) -ContentType 'text/xml' -method 'POST'
$result.Data | Should -BeExactly $body
}
It "Invoke-RestMethod supports request that returns page containing Code Page 936 data." {
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
$command = "Invoke-RestMethod -Uri '$uri'"