Public/Http.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<#
.SYNOPSIS
    Test a Web Service.
.DESCRIPTION
    Test that a Web Service is reachable and optionally returns specific content.
.PARAMETER Target
    Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent.
.PARAMETER Property
    Specifies a property of the WebResponseObject object to test.
.PARAMETER Should
    A Script Block defining a Pester Assertion.
.EXAMPLE
    Http http://localhost StatusCode { Should Be 200 }
.EXAMPLE
    Http http://localhost RawContent { Should Match 'X-Powered-By: ASP.NET' }
.EXAMPLE
    Http http://localhost RawContent { Should Not Match 'X-Powered-By: Cobal' }
.NOTES
    Assertions: Be, BeExactly, Match, MatchExactly
#>


function Http {
    [CmdletBinding()]
    param(        
        [Parameter(Mandatory, Position=1)]
        [Alias("Uri")]
        [string]$Target,
        
        [Parameter(Mandatory, Position=2)]
        [ValidateSet("BaseResponse", "Content", "Headers", "RawContent", "RawContentLength", "RawContentStream", "StatusCode", "StatusDescription")]
        [string]$Property,

        [Parameter(Mandatory, Position=3)]
        [scriptblock]$Should
    )    
    
    $params = Get-PoshspecParam -TestName Http -TestExpression {Invoke-WebRequest -Uri '$Target' -UseBasicParsing -ErrorAction SilentlyContinue} @PSBoundParameters
    
    Invoke-PoshspecExpression @params
}