mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 08:01:10 +00:00
73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestWebhookTestEndpointUsesPayloadTemplate(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
var received map[string]string
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if err := json.NewDecoder(r.Body).Decode(&received); err != nil {
|
|
t.Errorf("decode webhook body: %v", err)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer target.Close()
|
|
|
|
body := webhookTestRequestBody(t, target.URL, `{"title":{{title}},"desp":{{message}}}`)
|
|
recorder := httptest.NewRecorder()
|
|
ctx, _ := gin.CreateTestContext(recorder)
|
|
ctx.Request = httptest.NewRequest(http.MethodPost, "/settings/notifications/webhook/test", strings.NewReader(body))
|
|
ctx.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
(&Server{}).handleTestWebhookNotification(ctx)
|
|
|
|
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), `"ok":true`) {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if received["title"] == "" || received["desp"] != "这是一条 Webhook 测试通知" {
|
|
t.Fatalf("payload = %#v", received)
|
|
}
|
|
}
|
|
|
|
func TestWebhookTestEndpointRejectsInvalidPayloadTemplate(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
var calls atomic.Int32
|
|
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
|
calls.Add(1)
|
|
}))
|
|
defer target.Close()
|
|
|
|
body := webhookTestRequestBody(t, target.URL, `{"title":{{unknown}}}`)
|
|
recorder := httptest.NewRecorder()
|
|
ctx, _ := gin.CreateTestContext(recorder)
|
|
ctx.Request = httptest.NewRequest(http.MethodPost, "/settings/notifications/webhook/test", strings.NewReader(body))
|
|
ctx.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
(&Server{}).handleTestWebhookNotification(ctx)
|
|
|
|
if recorder.Code != http.StatusBadRequest || calls.Load() != 0 {
|
|
t.Fatalf("status=%d calls=%d body=%s", recorder.Code, calls.Load(), recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func webhookTestRequestBody(t *testing.T, targetURL, payloadTemplate string) string {
|
|
t.Helper()
|
|
body, err := json.Marshal(map[string]any{
|
|
"enabled": true, "urls": []string{targetURL}, "timeout_ms": 1000,
|
|
"retry_max": 0, "text_template": "{{text}}", "payload_template": payloadTemplate,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(body)
|
|
}
|