test: improve pkg package coverage

- Add HTTP status error functions tests (internal/pkg/errors)
- Add ReadRequestBodyWithPrealloc tests (internal/pkg/httputil)
- Add HTTPStatusToGoogleStatus tests (internal/pkg/googleapi)

Coverage improvements:
- pkg/errors: 77.6%
- pkg/httputil: 91.7%
- pkg/googleapi: 79.5%
This commit is contained in:
Your Name
2026-05-29 16:24:23 +08:00
parent 6351271f2d
commit ed399edb5f
3 changed files with 190 additions and 0 deletions

View File

@@ -181,3 +181,52 @@ func TestToHTTP_MetadataDeepCopy(t *testing.T) {
appErr.Metadata["k"] = "changed-again"
require.Equal(t, "v", body.Metadata["k"])
}
func TestHTTPStatusErrorFunctions(t *testing.T) {
tests := []struct {
name string
createFn func(string, string) *ApplicationError
isFn func(error) bool
code int
}{
{"BadRequest", BadRequest, IsBadRequest, http.StatusBadRequest},
{"TooManyRequests", TooManyRequests, IsTooManyRequests, http.StatusTooManyRequests},
{"Unauthorized", Unauthorized, IsUnauthorized, http.StatusUnauthorized},
{"Forbidden", Forbidden, IsForbidden, http.StatusForbidden},
{"NotFound", NotFound, IsNotFound, http.StatusNotFound},
{"Conflict", Conflict, IsConflict, http.StatusConflict},
{"InternalServer", InternalServer, IsInternalServer, http.StatusInternalServerError},
{"ServiceUnavailable", ServiceUnavailable, IsServiceUnavailable, http.StatusServiceUnavailable},
{"GatewayTimeout", GatewayTimeout, IsGatewayTimeout, http.StatusGatewayTimeout},
{"ClientClosed", ClientClosed, IsClientClosed, 499},
}
for _, tt := range tests {
t.Run(tt.name+"_create", func(t *testing.T) {
err := tt.createFn("REASON", "message")
require.Equal(t, int32(tt.code), err.Code)
require.Equal(t, "REASON", err.Reason)
require.Equal(t, "message", err.Message)
})
t.Run(tt.name+"_is_true", func(t *testing.T) {
err := New(tt.code, "ANY", "test")
require.True(t, tt.isFn(err))
})
t.Run(tt.name+"_is_false", func(t *testing.T) {
err := New(tt.code+1, "ANY", "test")
require.False(t, tt.isFn(err))
})
t.Run(tt.name+"_is_nil", func(t *testing.T) {
require.False(t, tt.isFn(nil))
})
}
}
func TestToHTTP_NonApplicationError(t *testing.T) {
code, body := ToHTTP(stderrors.New("plain error"))
require.Equal(t, http.StatusInternalServerError, code)
require.Equal(t, int32(UnknownCode), body.Code)
}