mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-19 00:37:28 +00:00
fix: support numeric status code mapping in ResetStatusCode
This commit is contained in:
57
service/error_test.go
Normal file
57
service/error_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestResetStatusCode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
statusCode int
|
||||
statusCodeConfig string
|
||||
expectedCode int
|
||||
}{
|
||||
{
|
||||
name: "map string value",
|
||||
statusCode: 429,
|
||||
statusCodeConfig: `{"429":"503"}`,
|
||||
expectedCode: 503,
|
||||
},
|
||||
{
|
||||
name: "map int value",
|
||||
statusCode: 429,
|
||||
statusCodeConfig: `{"429":503}`,
|
||||
expectedCode: 503,
|
||||
},
|
||||
{
|
||||
name: "skip invalid string value",
|
||||
statusCode: 429,
|
||||
statusCodeConfig: `{"429":"bad-code"}`,
|
||||
expectedCode: 429,
|
||||
},
|
||||
{
|
||||
name: "skip status code 200",
|
||||
statusCode: 200,
|
||||
statusCodeConfig: `{"200":503}`,
|
||||
expectedCode: 200,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
newAPIError := &types.NewAPIError{
|
||||
StatusCode: tc.statusCode,
|
||||
}
|
||||
ResetStatusCode(newAPIError, tc.statusCodeConfig)
|
||||
require.Equal(t, tc.expectedCode, newAPIError.StatusCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user