mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add EscapeHandling parameter in ConvertTo-Json cmdlet to allow escape the HTML (<, >, &, ', ") and control characters (e.g. newline).
66 lines
3.1 KiB
PowerShell
66 lines
3.1 KiB
PowerShell
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
Describe 'ConvertTo-Json' -tags "CI" {
|
|
BeforeAll {
|
|
$newline = [System.Environment]::NewLine
|
|
}
|
|
|
|
It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' {
|
|
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
|
|
$EgJObject.Add("TestValue1", "123456")
|
|
$EgJObject.Add("TestValue2", "78910")
|
|
$EgJObject.Add("TestValue3", "99999")
|
|
$dict = @{}
|
|
$dict.Add('JObject', $EgJObject)
|
|
$dict.Add('StrObject', 'This is a string Object')
|
|
$properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'}
|
|
$object = New-Object -TypeName psobject -Property $properties
|
|
$jsonFormat = ConvertTo-Json -InputObject $object
|
|
$jsonFormat | Should -Match '"TestValue1": 123456'
|
|
$jsonFormat | Should -Match '"TestValue2": 78910'
|
|
$jsonFormat | Should -Match '"TestValue3": 99999'
|
|
}
|
|
|
|
It "StopProcessing should succeed" {
|
|
$ps = [PowerShell]::Create()
|
|
$null = $ps.AddScript({
|
|
$obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''}
|
|
$obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj
|
|
1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose
|
|
# the conversion is expected to take some time, this throw is in case it doesn't
|
|
throw "Should not have thrown exception"
|
|
})
|
|
$null = $ps.BeginInvoke()
|
|
# wait for verbose message from ConvertTo-Json to ensure cmdlet is processing
|
|
Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 }
|
|
$null = $ps.BeginStop($null, $null)
|
|
# wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester
|
|
Start-Sleep -Milliseconds 100
|
|
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"
|
|
$ps.Dispose()
|
|
}
|
|
|
|
It "The result string is packed in an array symbols when AsArray parameter is used." {
|
|
$output = 1 | ConvertTo-Json -AsArray
|
|
$output | Should -BeLike "``[*1*]"
|
|
|
|
$output = 1,2 | ConvertTo-Json -AsArray
|
|
$output | Should -BeLike "``[*1*2*]"
|
|
}
|
|
|
|
It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." {
|
|
$output = 1 | ConvertTo-Json
|
|
$output | Should -BeExactly '1'
|
|
}
|
|
|
|
It "The result string should <Name>." -TestCases @(
|
|
@{name = "be not escaped by default."; params = @{}; expected = "{$newline ""abc"": ""'def'""$newline}" }
|
|
@{name = "be not escaped with '-EscapeHandling Default'."; params = @{EscapeHandling = 'Default'}; expected = "{$newline ""abc"": ""'def'""$newline}" }
|
|
@{name = "be escaped with '-EscapeHandling EscapeHtml'."; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$newline ""abc"": ""\u0027def\u0027""$newline}" }
|
|
) {
|
|
param ($name, $params ,$expected)
|
|
|
|
@{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected
|
|
}
|
|
}
|