Skip to content

Commit

Permalink
fix: don't append slash to webhook url (argoproj-labs#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmt committed May 6, 2020
1 parent 2c858e9 commit af3ae28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion notifiers/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func (w webhookNotifier) Send(notification Notification, recipient string) error
urlPath = webhookNotification.Path
}
}
url := strings.TrimRight(webhookSettings.URL, "/") + "/" + strings.TrimLeft(urlPath, "/")
url := webhookSettings.URL
if urlPath != "" {
url = strings.TrimRight(webhookSettings.URL, "/") + "/" + strings.TrimLeft(urlPath, "/")
}
req, err := http.NewRequest(method, url, bytes.NewBufferString(body))
if err != nil {
return err
Expand Down
30 changes: 30 additions & 0 deletions notifiers/webook_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notifiers

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -45,3 +46,32 @@ func TestWebhook_FailedToSendNotConfigured(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "not configured")
}

func TestWebhook_SubPath(t *testing.T) {
var receivedPath string
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
receivedPath = request.URL.Path
}))
defer server.Close()

notifier := NewWebhookNotifier(WebhookOptions{{
Name: "test",
URL: fmt.Sprintf("%s/subpath1", server.URL),
}})

err := notifier.Send(Notification{
Webhook: map[string]WebhookNotification{
"test": {Body: "hello world", Method: http.MethodPost},
},
}, "test")
assert.NoError(t, err)
assert.Equal(t, "/subpath1", receivedPath)

err = notifier.Send(Notification{
Webhook: map[string]WebhookNotification{
"test": {Body: "hello world", Method: http.MethodPost, Path: "/subpath2"},
},
}, "test")
assert.NoError(t, err)
assert.Equal(t, "/subpath1/subpath2", receivedPath)
}

0 comments on commit af3ae28

Please sign in to comment.