From e39698e3da9b05e946703f65211f74da411826d0 Mon Sep 17 00:00:00 2001 From: Chris Hunt <114173+cdhunt@users.noreply.github.com> Date: Fri, 23 Feb 2024 12:24:49 -0500 Subject: [PATCH] Fix handling of Urls that are just IPs (#22) * Handle hostnames that are plain IPs --------- Co-authored-by: Chris Hunt --- src/httpunitPS.psm1 | 22 ++++++++++++++-------- test/httpunitps.Tests.ps1 | 7 +++++++ version.json | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/httpunitPS.psm1 b/src/httpunitPS.psm1 index e7570c8..c6084ef 100644 --- a/src/httpunitPS.psm1 +++ b/src/httpunitPS.psm1 @@ -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 } @@ -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 @@ -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 diff --git a/test/httpunitps.Tests.ps1 b/test/httpunitps.Tests.ps1 index 09d6762..f91064c 100644 --- a/test/httpunitps.Tests.ps1 +++ b/test/httpunitps.Tests.ps1 @@ -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' { diff --git a/version.json b/version.json index b98a313..595d5ef 100644 --- a/version.json +++ b/version.json @@ -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