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

Added functionality to automatically URL encode the room name if required #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions HipChatPS/Public/Send-HipChat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -58,6 +58,15 @@
"notify" = [string]$notify
}

try {
$roomDecoded = [System.Web.HttpUtility]::UrlDecode($room)
$roomEncoded = [System.Web.HttpUtility]::UrlEncode($roomDecoded)
$room = $roomEncoded
}
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
$Post = [System.Text.Encoding]::UTF8.GetBytes($Body)
Expand Down
24 changes: 23 additions & 1 deletion Tests/Send-HipChat.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -48,7 +70,7 @@ Describe "Send-HipChat timeouts" {
}

send-hipchat @params | Should be $false

Assert-MockCalled Invoke-WebRequest -Exactly 4 -Scope It
}
}