Skip to content

Commit

Permalink
Fix handling of Urls that are just IPs (#22)
Browse files Browse the repository at this point in the history
* Handle hostnames that are plain IPs

---------

Co-authored-by: Chris Hunt <github@automatedops.com>
  • Loading branch information
cdhunt and Chris Hunt authored Feb 23, 2024
1 parent 2488e86 commit e39698e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/httpunitPS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class TestPlan {
$planUrl = [uri]$this.URL
$hostName = $planUrl.DnsSafeHost

$addressList = [Net.Dns]::GetHostEntry($hostName) |
Select-Object -ExpandProperty AddressList |
Where-Object AddressFamily -eq 'InterNetwork' |
Select-Object -ExpandProperty IPAddressToString

$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
}
if (!$All) {
return $addressList | Select-Object -First 1
}
Expand Down Expand Up @@ -135,10 +138,10 @@ class TestCase {

$result.Label = '{0} ({1})' -f $result.Label, $testName

$socket = [Net.Sockets.Socket]::new([Net.Sockets.AddressFamily]::InterNetwork, [Net.Sockets.SocketType ]::Stream, [Net.Sockets.ProtocolType]::Tcp )
$socket = [Net.Sockets.Socket]::new([Net.Sockets.AddressFamily]::InterNetwork, [Net.Sockets.SocketType]::Stream, [Net.Sockets.ProtocolType]::Tcp )

try {
$socket.Connect($testName, $testPort);
$socket.Connect($testName, $testPort)
$socket.Shutdown([Net.Sockets.SocketShutdown]::Both)
} catch {
$result.Connected = $false
Expand Down Expand Up @@ -174,7 +177,10 @@ class TestCase {
}

$client = [Net.Http.HttpClient]::new($handler)
$client.DefaultRequestHeaders.Host = $this.URL.Host
if ($this.URL.Host -ne $this.IP) {
$client.DefaultRequestHeaders.Host = $this.URL.Host
}

$client.Timeout = $this.Plan.Timeout


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

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

$result.Connected | Should -Be $true
$result.Gotcode | Should -Be $true
}
}

Context 'By Config' {
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.1.0",
"version": "1.1.1",
"cloudBuild": {
"buildNumber": {
"enabled": true
Expand Down

0 comments on commit e39698e

Please sign in to comment.