fix confirmed deployment risks
This commit is contained in:
@@ -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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user