From 9ab49ba233fa73799b8f2c8f4201b88845e8c04e Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 23 Feb 2024 11:46:18 -0500 Subject: [PATCH 1/2] Handle hostnames that are plain IPs --- src/httpunitPS.psm1 | 17 ++++++++++------- test/httpunitps.Tests.ps1 | 7 +++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/httpunitPS.psm1 b/src/httpunitPS.psm1 index e7570c8..d966861 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 diff --git a/test/httpunitps.Tests.ps1 b/test/httpunitps.Tests.ps1 index 09d6762..666ea61 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 -Quiet -SkipVerify + + $result.Result | Should -BeNullOrEmpty + $result.Connected | Should -Be $true + } } Context 'By Config' { From 8281eb625c9109d58d62d6725f756d941b893cdc Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 23 Feb 2024 12:13:17 -0500 Subject: [PATCH 2/2] Fix the header when using IP --- src/httpunitPS.psm1 | 5 ++++- test/httpunitps.Tests.ps1 | 4 ++-- version.json | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/httpunitPS.psm1 b/src/httpunitPS.psm1 index d966861..c6084ef 100644 --- a/src/httpunitPS.psm1 +++ b/src/httpunitPS.psm1 @@ -177,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 666ea61..f91064c 100644 --- a/test/httpunitps.Tests.ps1 +++ b/test/httpunitps.Tests.ps1 @@ -72,10 +72,10 @@ Describe 'Invoke-HttpUnit' { } It 'Should test a raw IP' { - $result = Invoke-HttpUnit -Url https://93.184.216.34 -Quiet -SkipVerify + $result = Invoke-HttpUnit -Url https://93.184.216.34 -Code 404 -Quiet -SkipVerify - $result.Result | Should -BeNullOrEmpty $result.Connected | Should -Be $true + $result.Gotcode | Should -Be $true } } 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