42 lines
965 B
PowerShell
42 lines
965 B
PowerShell
|
function Test-HaveStatusCode {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Tests that the $result has status code in $expect
|
||
|
|
||
|
.DESCRIPTION
|
||
|
Intended to be used in conjunction with Test-Should function.
|
||
|
|
||
|
.PARAMETER result
|
||
|
Result of Invoke-WebRequest containing Html to test against.
|
||
|
|
||
|
.PARAMETER expect
|
||
|
Status code to expect in $result
|
||
|
|
||
|
.EXAMPLE
|
||
|
$passedTest = (Test-Should -Result $webRequest -Predicate ${function:Test-HaveStatusCode} 200)
|
||
|
|
||
|
.NOTES
|
||
|
Returns a bool of success and/or error message.
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([System.Boolean])]
|
||
|
[OutputType([System.String])]
|
||
|
param(
|
||
|
$result,
|
||
|
$expect
|
||
|
)
|
||
|
|
||
|
$returnedCode = $result.StatusCode -as [int]
|
||
|
|
||
|
try {
|
||
|
if (-not($returnedCode -eq $expect)) {
|
||
|
$false
|
||
|
"returned wrong status code: $returnedCode. Expected: $expect"
|
||
|
} else {
|
||
|
$true
|
||
|
}
|
||
|
} catch {
|
||
|
$false
|
||
|
"Error occured inside test! Exception: $_"
|
||
|
}
|
||
|
}
|