Adjustements of tests for WebCmdlets XML declaration or HTML meta encoding value usage

WebCmdlets XML declaration or HTML meta encoding value usage tests ensure to set the correct request response encoding.
Reading a UTF-8 XML declaration or HTML meta encoding value asking for UTF-16 does not makes sense.
The encoding declaration will be made unreadable after the encoding change.
This commit adapts the tests to test against latin1 instead of UTF-16. Latin1 is a UTF-8 compatible charset.
Additionally, two tests were added to ensure an incompatible charset is not used.
This commit is contained in:
BAILLY-GRANDVAUX Clément 2024-08-08 17:26:11 +02:00
parent d7a4a093b3
commit 23bc6b71e5
No known key found for this signature in database
GPG Key ID: 2F410B4342FE24D8

View File

@ -1374,10 +1374,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects charset meta value when the ContentType header does not define it." {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset="Unicode"></head></html>'
body = '<html><head><meta charset="latin1"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1388,10 +1388,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." {
$query = @{
contenttype = 'text/html'
body = "<html>`n <head>`n <meta`n charset=`"Unicode`"`n >`n </head>`n</html>"
body = "<html>`n <head>`n <meta`n charset=`"latin1`"`n >`n </head>`n</html>"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1402,10 +1402,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects charset meta value when the attribute value is unquoted." {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset = Unicode></head></html>'
body = '<html><head><meta charset = latin1></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1416,10 +1416,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects http-equiv charset meta value when the ContentType header does not define it." {
$query = @{
contenttype = 'text/html'
body = "<html><head>`n<meta http-equiv=`"content-type`" content=`"text/html; charset=Unicode`">`n</head>`n</html>"
body = "<html><head>`n<meta http-equiv=`"content-type`" content=`"text/html; charset=latin1`">`n</head>`n</html>"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1430,10 +1430,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects http-equiv charset meta value newlines are encountered in the element." {
$query = @{
contenttype = 'text/html'
body = "<html><head>`n<meta`n http-equiv=`"content-type`"`n content=`"text/html; charset=Unicode`">`n</head>`n</html>"
body = "<html><head>`n<meta`n http-equiv=`"content-type`"`n content=`"text/html; charset=latin1`">`n</head>`n</html>"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1458,12 +1458,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest honors non-utf8 charsets in the Content-Type header" {
$query = @{
contenttype = 'text/html; charset=utf-16'
contenttype = 'text/html; charset=latin1'
body = '<html><head><meta charset="utf-32"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
# NOTE: meta charset should be ignored
$expectedEncoding = [System.Text.Encoding]::GetEncoding('utf-16')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -1498,6 +1498,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$response.Output.Encoding.EncodingName | Should -Be $expectedEncoding.EncodingName
$response.Output | Should -BeOfType Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject
}
It "Verifies Invoke-WebRequest defaults to UTF-8 when an UTF-8/ASCII incompatible charset is declared" {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset="utf-16"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::UTF8
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
$response.Output.Encoding.EncodingName | Should -Be $expectedEncoding.EncodingName
$response.Output | Should -BeOfType Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject
}
}
#endregion charset encoding tests
@ -3671,10 +3685,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-RestMethod detects charset meta value when the ContentType header does not define it." {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset="Unicode"></head></html>'
body = '<html><head><meta charset="latin1"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3684,10 +3698,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." {
$query = @{
contenttype = 'text/html'
body = "<html>`n <head>`n <meta`n charset=`"Unicode`"`n >`n </head>`n</html>"
body = "<html>`n <head>`n <meta`n charset=`"latin1`"`n >`n </head>`n</html>"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3697,10 +3711,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-RestMethod detects charset meta value when the attribute value is unquoted." {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset = Unicode></head></html>'
body = '<html><head><meta charset = latin1></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3710,10 +3724,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-RestMethod detects http-equiv charset meta value when the ContentType header does not define it." {
$query = @{
contenttype = 'text/html'
body = "<html><head>`n<meta http-equiv=`"content-type`" content=`"text/html; charset=Unicode`">`n</head>`n</html>"
body = "<html><head>`n<meta http-equiv=`"content-type`" content=`"text/html; charset=latin1`">`n</head>`n</html>"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3723,10 +3737,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-RestMethod detects http-equiv charset meta value newlines are encountered in the element." {
$query = @{
contenttype = 'text/html'
body = "<html><head>`n<meta`n http-equiv=`"content-type`"`n content=`"text/html; charset=Unicode`">`n</head>`n</html>`n"
body = "<html><head>`n<meta`n http-equiv=`"content-type`"`n content=`"text/html; charset=latin1`">`n</head>`n</html>`n"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3749,12 +3763,12 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
It "Verifies Invoke-RestMethod honors non-utf8 charsets in the Content-Type header" {
$query = @{
contenttype = 'text/html; charset=utf-16'
contenttype = 'text/html; charset=latin1'
body = '<html><head><meta charset="utf-32"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
# NOTE: meta charset should be ignored
$expectedEncoding = [System.Text.Encoding]::GetEncoding('utf-16')
$expectedEncoding = [System.Text.Encoding]::GetEncoding('latin1')
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
@ -3786,6 +3800,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$response.Error | Should -BeNullOrEmpty
$response.Encoding.EncodingName | Should -Be $expectedEncoding.EncodingName
}
It "Verifies Invoke-RestMethod defaults to UTF-8 when an UTF-8/ASCII incompatible charset is declared" {
$query = @{
contenttype = 'text/html'
body = '<html><head><meta charset="utf-16"></head></html>'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::UTF8
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
$response.Error | Should -BeNullOrEmpty
$response.Encoding.EncodingName | Should -Be $expectedEncoding.EncodingName
}
}
#endregion charset encoding tests