47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"cmroubao/backend-api/internal/usecase"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestWriteUsecaseErrorUsesPreflightStatusAndCode(t *testing.T) {
|
||
|
|
testCases := []struct {
|
||
|
|
code string
|
||
|
|
status int
|
||
|
|
}{
|
||
|
|
{"ERP_NOT_CONFIGURED", http.StatusUnprocessableEntity},
|
||
|
|
{"ERP_LOGIN_REJECTED", http.StatusUnprocessableEntity},
|
||
|
|
{"ERP_RESPONSE_INVALID", http.StatusBadGateway},
|
||
|
|
{"OCR_SERVICE_INVALID", http.StatusServiceUnavailable},
|
||
|
|
{"ERP_UNAVAILABLE", http.StatusServiceUnavailable},
|
||
|
|
}
|
||
|
|
for _, testCase := range testCases {
|
||
|
|
t.Run(testCase.code, func(t *testing.T) {
|
||
|
|
gin.SetMode(gin.TestMode)
|
||
|
|
router := gin.New()
|
||
|
|
router.GET("/test", func(ctx *gin.Context) {
|
||
|
|
writeUsecaseError(ctx, &usecase.Error{
|
||
|
|
Kind: usecase.ErrorKindUnavailable,
|
||
|
|
Code: testCase.code,
|
||
|
|
Message: "private upstream response is hidden",
|
||
|
|
Fields: map[string]string{},
|
||
|
|
})
|
||
|
|
})
|
||
|
|
response := httptest.NewRecorder()
|
||
|
|
router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/test", nil))
|
||
|
|
if response.Code != testCase.status ||
|
||
|
|
!strings.Contains(response.Body.String(), testCase.code) ||
|
||
|
|
strings.Contains(response.Body.String(), "private upstream") {
|
||
|
|
t.Fatalf("response = %d / %s", response.Code, response.Body)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|