Files
cmroubao/backend-api/internal/transport/httpapi/preflight_error_test.go
T

48 lines
1.4 KiB
Go
Raw Normal View History

2026-07-29 10:58:28 +08:00
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},
{"FREIGHT_SYNC_TIMEOUT", http.StatusGatewayTimeout},
2026-07-29 10:58:28 +08:00
}
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)
}
})
}
}