fix confirmed deployment risks
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Security Scan / backend-security (push) Has been cancelled
Security Scan / frontend-security (push) Has been cancelled

This commit is contained in:
2026-04-25 09:24:17 +08:00
parent 75d03e4713
commit 649eb23091
10 changed files with 258 additions and 19 deletions

View File

@@ -97,3 +97,64 @@ func TestWebhookConstants(t *testing.T) {
assert.Equal(t, 200, webhookLogTruncateLen)
})
}
func TestWriteUnavailableResponse(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
providerKey string
wantCode int
wantContentType string
wantBody string
checkJSON bool
wantJSONCode string
wantJSONMessage string
}{
{
name: "wxpay returns JSON failure to trigger retry",
providerKey: "wxpay",
wantCode: http.StatusServiceUnavailable,
wantContentType: "application/json",
checkJSON: true,
wantJSONCode: "FAIL",
wantJSONMessage: "provider unavailable",
},
{
name: "stripe returns 503 with empty body",
providerKey: "stripe",
wantCode: http.StatusServiceUnavailable,
wantContentType: "text/plain",
wantBody: "",
},
{
name: "easypay returns 503 plain text",
providerKey: "easypay",
wantCode: http.StatusServiceUnavailable,
wantContentType: "text/plain",
wantBody: "provider unavailable",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
writeUnavailableResponse(c, tt.providerKey)
assert.Equal(t, tt.wantCode, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), tt.wantContentType)
if tt.checkJSON {
var resp map[string]string
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
assert.Equal(t, tt.wantJSONCode, resp["code"])
assert.Equal(t, tt.wantJSONMessage, resp["message"])
} else {
assert.Equal(t, tt.wantBody, w.Body.String())
}
})
}
}