Skip to content

Commit

Permalink
Improve error handling of DNS resolution errors (#23)
Browse files Browse the repository at this point in the history
* Unknown host errors should end up in the Result property

---------

Co-authored-by: Chris Hunt <github@automatedops.com>
  • Loading branch information
cdhunt and Chris Hunt committed Feb 26, 2024
1 parent e39698e commit 1f835f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
31 changes: 24 additions & 7 deletions src/httpunitPS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ class TestPlan {
$addressList = $null
$isIp = [ipaddress]::TryParse($hostName, [ref]$addressList)
if (!$isip) {
$addressList = [Net.Dns]::GetHostEntry($hostName) |
Select-Object -ExpandProperty AddressList |
Where-Object AddressFamily -eq 'InterNetwork' |
Select-Object -ExpandProperty IPAddressToString
try {
$addressList = [Net.Dns]::GetHostEntry($hostName) |
Select-Object -ExpandProperty AddressList |
Where-Object AddressFamily -eq 'InterNetwork' |
Select-Object -ExpandProperty IPAddressToString
} catch {
Write-Verbose "Cannot resolve hostname '$hostName'."
}

}
if (!$All) {
return $addressList | Select-Object -First 1
Expand Down Expand Up @@ -66,13 +71,18 @@ class TestPlan {
$planUrl = [uri]$this.URL

foreach ($item in $this.ExpandIpList()) {

Write-Debug ('Adding test case for "{0}"' -f $item)
$case = [TestCase]@{
URL = $planUrl
IP = $item
Plan = $this
ExpectCode = [System.Net.HttpStatusCode]$this.Code
}
if (![string]::IsNullOrEmpty($item)) {
$case.IP = $item
} else {
Write-Debug ('No IP for "{0}".' -f $planUrl)
}

if (![string]::IsNullOrEmpty($this.Text)) {
Write-Debug ('Adding simple string matching test case. "{0}"' -f $this.Text)
Expand Down Expand Up @@ -183,8 +193,12 @@ class TestCase {

$client.Timeout = $this.Plan.Timeout

if ($null -ne $this.IP) {
$testUri = $this.URL.OriginalString -replace $this.URL.Host, $this.IP.ToString()
} else {
$testUri = $this.URL
}

$testUri = $this.URL.OriginalString -replace $this.URL.Host, $this.IP.ToString()
$content = [Net.Http.HttpRequestMessage]::new($this.Plan.Method, [Uri]$testUri)

if ($this.Plan.InsecureSkipVerify) {
Expand Down Expand Up @@ -266,7 +280,10 @@ class TestCase {
$result.TimeTotal = (Get-Date) - $time

if ($this.URL.Scheme -eq 'https') {
$result.ServerCertificate = Get-SSLCertificate -ComputerName $this.URL.DnsSafeHost -Port $this.URL.Port
$getSSL = Get-SSLCertificate -ComputerName $this.URL.DnsSafeHost -Port $this.URL.Port -ErrorAction SilentlyContinue
if ($getSSL -is [System.Security.Cryptography.X509Certificates.X509Certificate2]) {
$result.ServerCertificate = $getSSL
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions test/httpunitps.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ Describe 'Invoke-HttpUnit' {
$result.Result.Exception.Message | Should -Match 'Exception calling "Connect"'
}

It 'Should report "No such host is known"' {
$result = Invoke-HttpUnit -Url "https://asfldjfalsjfalsdkjf.com" -Quiet

$result.Connected | Should -Be $false
$result.Result.CategoryInfo.Category | Should -Be 'ConnectionError'
}

It 'Should test a raw IP' {
$result = Invoke-HttpUnit -Url https://93.184.216.34 -Code 404 -Quiet -SkipVerify

Expand Down Expand Up @@ -171,7 +178,9 @@ Describe 'Invoke-HttpUnit' {
$result[0].GotRegex | Should -Be $False
$result[0].GotHeaders | Should -Be $False
$result[0].InvalidCert | Should -Be $False
$result[0].TimeTotal | Should -BeGreaterThan ([timespan]::new(1))
#$result[0].TimeTotal | Should -BeGreaterThan ([timespan]::new(1))
# This returns no time in GitHubActions on PS 5.1.
# I can't reproduce on 5.1.

$result[1].Label | Should -Match "https://example.com/"
$result[1].Result | Should -BeNullOrEmpty
Expand All @@ -181,7 +190,7 @@ Describe 'Invoke-HttpUnit' {
$result[1].GotRegex | Should -Be $False
$result[1].GotHeaders | Should -Be $False
$result[1].InvalidCert | Should -Be $False
$result[1].TimeTotal | Should -BeGreaterThan ([timespan]::new(1))
#$result[1].TimeTotal | Should -BeGreaterThan ([timespan]::new(1))
}
}
}
Expand Down

0 comments on commit 1f835f4

Please sign in to comment.