From 8b020428f606c558174aabb8d3d0a0f6137bf710 Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Wed, 29 Jan 2025 19:18:50 -0300 Subject: [PATCH] fix webhook method null and add test case --- handlers/webhook.go | 7 ++++--- handlers/webhook_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/handlers/webhook.go b/handlers/webhook.go index d549578b8..8bff269f4 100644 --- a/handlers/webhook.go +++ b/handlers/webhook.go @@ -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) diff --git a/handlers/webhook_test.go b/handlers/webhook_test.go index 1d073a775..2b4088f7e 100644 --- a/handlers/webhook_test.go +++ b/handlers/webhook_test.go @@ -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)