Skip to content

Commit

Permalink
fix webhook method null and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Jan 29, 2025
1 parent d874137 commit 8b02042
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions handlers/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func SendWebhooksExternal(r *http.Request, configWebhook interface{}) error {
return fmt.Errorf("invalid url %s", err)
}

method := webhook["method"].(string)
if method == "" {
method = "POST"
method := "POST"
methodVal, ok := webhook["method"]
if ok && methodVal != nil {
method = methodVal.(string)
}

req, _ := http.NewRequest(method, webhook["url"].(string), r.Body)
Expand Down
15 changes: 15 additions & 0 deletions handlers/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ func TestSendWebhooksExternal_NoHeaders(t *testing.T) {
assert.NoError(t, err)
}

func TestSendWebhooksExternal_NoMethod(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data": "success"}`))
}))

req := httptest.NewRequest(http.MethodPost, "https://foo.bar/webhook", nil)
webhookConfig := map[string]interface{}{
"url": ts.URL,
}

err := SendWebhooksExternal(req, webhookConfig)
assert.NoError(t, err)
}

func TestSendWebhooks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit 8b02042

Please sign in to comment.