Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/powershell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- src/internal/Export-HelpToMd.ps1
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
contents: read
Expand Down
26 changes: 20 additions & 6 deletions src/httpunitPS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class TestPlan {
[string] $Text
[string] $Regex
[hashtable] $Headers
[hashtable] $Content

[bool] $InsecureSkipVerify = $false
[X509Certificate] $ClientCertificate
[timespan] $Timeout = [timespan]::new(0, 0, 3)
Expand Down Expand Up @@ -89,9 +91,18 @@ class TestPlan {
$case.ExpectText = $this.Text
}

if ($null -ne $this.Headers) {
Write-Debug ('Adding headers test case. Checking for "{0}" headers' -f $this.Headers.Count)
$case.ExpectHeaders = $this.Headers
if ($null -ne $this.Headers -or $null -ne $this.Content.Headers) {
$case.ExpectHeaders = @{}

if ($null -ne $this.Headers) {
Write-Debug ('Adding headers test case. Checking for "{0}" headers' -f $this.Headers.Count)
$case.ExpectHeaders += $this.Headers
}

if ($null -ne $this.Content.Headers) {
Write-Debug ('Adding content headers test case. Checking for "{0}" headers' -f $this.Content.Headers.Count)
$case.ExpectHeaders += $this.Content.Headers
}
}

$cases.Add($case)
Expand Down Expand Up @@ -240,13 +251,16 @@ class TestCase {
Write-Debug ('TestHttp: Headers=@({0})' -f ($this.ExpectHeaders.Keys -join ', '))
$headerMatchErrors = @()

# check against response.Headers and response.Content.Headers
$allResponseHeaders = $response.Headers + $response.Content.Headers

foreach ($keyExpected in $this.ExpectHeaders.Keys) {

$expectedValue = $this.ExpectHeaders[$keyExpected]

if ($response.Headers.Key -contains $keyExpected) {
if ($allResponseHeaders -contains $keyExpected) {

$foundValue = $response.Headers.Where({ $_.Key -eq $keyExpected }).Value
$foundValue = $allResponseHeaders.Where({ $_.Key -eq $keyExpected }).Value

if ($foundValue -like $expectedValue) {
continue
Expand All @@ -261,7 +275,7 @@ class TestCase {
if ($headerMatchErrors.Count -gt 0) {
$errorMessage = $headerMatchErrors -join "; "
$exception = [Exception]::new(("Response headers do not match: {0}" -f $errorMessage))
$result.Result = [System.Management.Automation.ErrorRecord]::new($exception, "3", "InvalidResult", $response.Headers)
$result.Result = [System.Management.Automation.ErrorRecord]::new($exception, "3", "InvalidResult", $allResponseHeaders)
} else {
$result.GotHeaders = $true
}
Expand Down
5 changes: 5 additions & 0 deletions test/testconfig1.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
code = 200
timeout = "0:0:10"
headers = @{Server = "gws" }
content = @{
headers = @{
'Content-Type' = 'text/html'
}
}
}
)
}
4 changes: 3 additions & 1 deletion test/testconfig1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
code = 200
timeout = "0:0:10"
[plan.headers]
Server = "gws"
Server = "gws"
[plan.content.headers]
Content-Type = "text/html"