Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling of DNS resolution errors #23

Merged
merged 4 commits into from
Feb 26, 2024
Merged
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
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
Loading