Files
PowerShell-PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/JsonObject.Tests.ps1
T
Max Maximov 8be31098b1 Invoke-RestMethod cmdlet error if the response is neither JSON nor XML (#2862)
* This change fixes the Invoke-RestMethod cmdlet behavior if the input is neither xml nor json.
* test description
* fixed the failed tests
* removed some duplication
* refactoring: eliminated some double castings
* added JsonObject unit tests; enhanced JSON deserialization exception message
* refactored JsonObject.Tests.ps1
* entitled test cases
* formatting
2016-12-15 00:06:26 -08:00

47 lines
1.3 KiB
PowerShell

Describe 'Unit tests for JsonObject' -tags "CI" {
function ShouldThrow
{
param (
[Parameter(ValueFromPipeline = $true)]
$InputObject,
[Parameter(Position = 0)]
$ExpectedException
)
try
{
& $InputObject
throw "Should throw exception"
}
catch
{
$_.FullyQualifiedErrorId | should be $ExpectedException
}
}
$validStrings = @(
@{ name = "empty"; str = "" }
@{ name = "spaces"; str = " " }
@{ name = "object"; str = "{a:1}" }
)
It 'no error for valid string - <name>' -TestCase $validStrings {
param ($str)
$errRecord = $null
[Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord)
$errRecord | Should BeNullOrEmpty
}
$invalidStrings = @(
@{ name = "plain text"; str = "plaintext" }
@{ name = "part"; str = '{"a" :' }
)
It 'throw ArgumentException for invalid string - <name>' -TestCase $invalidStrings {
param ($str)
$errRecord = $null
{ [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord) } | ShouldThrow 'ArgumentException'
}
}