Skip to content

Instantly share code, notes, and snippets.

@DanielSmon
Last active September 25, 2019 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielSmon/adb0dd3c0c9c269662c2c53e227bd5f3 to your computer and use it in GitHub Desktop.
Save DanielSmon/adb0dd3c0c9c269662c2c53e227bd5f3 to your computer and use it in GitHub Desktop.
Function Test-Redirect($uri, $expectedStatusCode, $expectedRedirect) {
try {
$result = Invoke-WebRequest -MaximumRedirection 0 -Uri $uri -SkipHeaderValidation
Write-Host -ForegroundColor Red "Failed " -NoNewline
Write-Host $uri -NoNewline
Write-Host " Expected:" $expectedStatusCode $expectedRedirect " " -NoNewline
Write-Host "Received:" $result.StatusCode -BackgroundColor Yellow
} catch {
$_Exception
$location = $_.Exception.Response.Headers.Location
$statusCode = $_.Exception.Response.StatusCode
if($location -eq $expectedRedirect -and $statusCode -eq $expectedStatusCode)
{
Write-Host -ForegroundColor Green "Passed " -NoNewline
Write-Host $uri -NoNewline
Write-Host " Redirects with" $statusCode.value__ "to" $location " "
} else {
Write-Host -ForegroundColor Red "Failed " -NoNewline
Write-Host $uri -NoNewline
Write-Host " Expected:" $expectedStatusCode $expectedRedirect " " -NoNewline
Write-Host "Received:" $statusCode.value__ $location -BackgroundColor Yellow
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment