From df1c00cb004e7b79f95368075104859539d15b9d Mon Sep 17 00:00:00 2001 From: Mark Wragg Date: Sun, 17 Mar 2024 16:45:21 +0000 Subject: [PATCH 1/3] Added functionality to ensure the room name is automatically URL encoded if not already per issue #4 --- HipChatPS/Public/Send-HipChat.ps1 | 7 +++++-- Tests/Send-HipChat.Tests.ps1 | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/HipChatPS/Public/Send-HipChat.ps1 b/HipChatPS/Public/Send-HipChat.ps1 index 6b4fa93..aadb0e9 100644 --- a/HipChatPS/Public/Send-HipChat.ps1 +++ b/HipChatPS/Public/Send-HipChat.ps1 @@ -37,11 +37,11 @@ [Parameter(Mandatory = $True)] [string]$apitoken, - #The id or URL encoded name of the HipChat room you want to send the message to. + #The id or name of the HipChat room you want to send the message to. If not URL encoded, will be URL encoded for you. [Parameter(Mandatory = $True, ParameterSetName = 'Room')] [string]$room, - #The id or URL encoded name of the HipChat room you want to send the message to. + #The id or name of the HipChat room you want to send the message to. If not URL encoded, will be URL encoded for you. [Parameter(Mandatory = $True, ParameterSetName = 'User')] [string]$user, @@ -58,6 +58,9 @@ "notify" = [string]$notify } + $roomDecoded = [System.Web.HttpUtility]::UrlDecode($room) + $room = [System.Web.HttpUtility]::UrlEncode($roomDecoded) + $uri = "$uri/v2/room/$room/notification?auth_token=$apitoken" $Body = ConvertTo-Json $messageObj $Post = [System.Text.Encoding]::UTF8.GetBytes($Body) diff --git a/Tests/Send-HipChat.Tests.ps1 b/Tests/Send-HipChat.Tests.ps1 index ac33f18..e67be69 100644 --- a/Tests/Send-HipChat.Tests.ps1 +++ b/Tests/Send-HipChat.Tests.ps1 @@ -30,6 +30,28 @@ Describe 'Send-HipChat' { {send-hipchat @params} | Should Throw } + + It "should handle a URL encoded room" { + + $params = @{ + message = "Pester test message" + room = "Test%20Room" + apitoken = "c6cS2qXSv1zRyUUXpPsu3bebVF43wx8bvPQK5vg6" + } + + Send-HipChat @params | Should Be $true + } + + It "should handle a non-url encoded room name that needs encoding" { + + $params = @{ + message = "Pester test message" + room = "Test Room" + apitoken = "c6cS2qXSv1zRyUUXpPsu3bebVF43wx8bvPQK5vg6" + } + + Send-HipChat @params | Should Be $true + } } Describe "Send-HipChat timeouts" { @@ -48,7 +70,7 @@ Describe "Send-HipChat timeouts" { } send-hipchat @params | Should be $false - + Assert-MockCalled Invoke-WebRequest -Exactly 4 -Scope It } } From d7cae1d4e557eb3804bb1e4b653db4c8afb3a110 Mon Sep 17 00:00:00 2001 From: Mark Wragg Date: Sun, 17 Mar 2024 16:48:03 +0000 Subject: [PATCH 2/3] Added try catch to handle [system.web.httputility] not available --- HipChatPS/Public/Send-HipChat.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/HipChatPS/Public/Send-HipChat.ps1 b/HipChatPS/Public/Send-HipChat.ps1 index aadb0e9..75de1a4 100644 --- a/HipChatPS/Public/Send-HipChat.ps1 +++ b/HipChatPS/Public/Send-HipChat.ps1 @@ -58,8 +58,13 @@ "notify" = [string]$notify } - $roomDecoded = [System.Web.HttpUtility]::UrlDecode($room) - $room = [System.Web.HttpUtility]::UrlEncode($roomDecoded) + try { + $roomDecoded = [System.Web.HttpUtility]::UrlDecode($room) + $room = [System.Web.HttpUtility]::UrlEncode($roomDecoded) + } + catch { + Write-Warning "Could not automatically URL encode room name. Please ensure it is encoded. Error: $($_)" + } $uri = "$uri/v2/room/$room/notification?auth_token=$apitoken" $Body = ConvertTo-Json $messageObj From 1a64b41d920772ae1d511e4b34065a9efae481af Mon Sep 17 00:00:00 2001 From: Mark Wragg Date: Sun, 17 Mar 2024 16:49:04 +0000 Subject: [PATCH 3/3] Ensured $room is unchanged unless decode/encode has worked --- HipChatPS/Public/Send-HipChat.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HipChatPS/Public/Send-HipChat.ps1 b/HipChatPS/Public/Send-HipChat.ps1 index 75de1a4..c64d07d 100644 --- a/HipChatPS/Public/Send-HipChat.ps1 +++ b/HipChatPS/Public/Send-HipChat.ps1 @@ -60,7 +60,8 @@ try { $roomDecoded = [System.Web.HttpUtility]::UrlDecode($room) - $room = [System.Web.HttpUtility]::UrlEncode($roomDecoded) + $roomEncoded = [System.Web.HttpUtility]::UrlEncode($roomDecoded) + $room = $roomEncoded } catch { Write-Warning "Could not automatically URL encode room name. Please ensure it is encoded. Error: $($_)"