From ed6f724a5de6ee91d90995a865e1d5587ea02e86 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Mon, 11 Mar 2024 09:42:58 +0100 Subject: [PATCH] add test for webhook default method https://github.com/go-gitea/gitea/pull/29690 --- services/webhook/deliver_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/services/webhook/deliver_test.go b/services/webhook/deliver_test.go index 3324c7f0e3..bc06e43e03 100644 --- a/services/webhook/deliver_test.go +++ b/services/webhook/deliver_test.go @@ -233,7 +233,8 @@ func TestWebhookDeliverSpecificTypes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) type hookCase struct { - gotBody chan []byte + gotBody chan []byte + expectedMethod string } cases := map[string]hookCase{ @@ -256,7 +257,8 @@ func TestWebhookDeliverSpecificTypes(t *testing.T) { gotBody: make(chan []byte, 1), }, webhook_module.MATRIX: { - gotBody: make(chan []byte, 1), + gotBody: make(chan []byte, 1), + expectedMethod: "PUT", }, webhook_module.WECHATWORK: { gotBody: make(chan []byte, 1), @@ -271,6 +273,13 @@ func TestWebhookDeliverSpecificTypes(t *testing.T) { typ := strings.Split(r.URL.Path, "/")[1] // take first segment (after skipping leading slash) hc := cases[typ] + + if hc.expectedMethod != "" { + assert.Equal(t, hc.expectedMethod, r.Method, r.URL.Path) + } else { + assert.Equal(t, "POST", r.Method, r.URL.Path) + } + require.NotNil(t, hc.gotBody, r.URL.Path) body, err := io.ReadAll(r.Body) assert.NoError(t, err) @@ -293,8 +302,8 @@ func TestWebhookDeliverSpecificTypes(t *testing.T) { IsActive: true, Type: typ, URL: s.URL + "/" + typ, - HTTPMethod: "POST", - ContentType: 0, // set to 0 so that falling back to default request fails with "invalid content type" + HTTPMethod: "", // should fallback to POST, when left unset by the specific hook + ContentType: 0, // set to 0 so that falling back to default request fails with "invalid content type" Meta: "{}", } assert.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, hook))