Files
zt/tests/custom_assertions.go
zeekay 241625e7d9 rename: ziti → zt across all paths
- Repo names: ziti-* → zt-*
- Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt
- Docker images: hanzozt/ziti-* → hanzozt/zt-*
- C/Java/JS symbols: ziti_* → zt_*
- File and directory names updated
2026-02-18 18:34:19 -08:00

69 lines
1.6 KiB
Go

package tests
import (
"errors"
"fmt"
"github.com/hanzozt/edge-api/rest_util"
"github.com/hanzozt/zt/v2/zt/util"
"github.com/stretchr/testify/require"
)
type CustomAssertions struct {
*require.Assertions
}
func NewCustomAssertions(t require.TestingT) *CustomAssertions {
return &CustomAssertions{require.New(t)}
}
func (c *CustomAssertions) ApiError(err error) {
restApiError := &util.RestApiError{}
if errors.As(err, restApiError) {
return
}
c.Fail(fmt.Sprintf("expected error to be an api error, got %T", err))
}
func (c *CustomAssertions) ApiErrorWithCode(err error, code string) {
// if not wrapped
if apiErrorPayload, ok := err.(rest_util.ApiErrorPayload); ok {
if apiErrorPayload == nil {
c.Fail("expected ApiErrorPayload to not be nil")
}
payload := apiErrorPayload.GetPayload()
if payload == nil {
c.Fail("expected RestAPIError to have payload, got nil")
return
}
if payload.Error == nil {
c.Fail("expected RestApiError payload to have an error, got nil")
return
}
if payload.Error.Code != code {
c.Fail(fmt.Sprintf("expected RestApiError payload to have code %s, got %s", code, payload.Error.Code))
return
}
//success
return
}
//if wrapped
restApiFormattedError := &rest_util.APIFormattedError{}
if errors.As(err, &restApiFormattedError) {
if restApiFormattedError.Code != code {
c.Fail(fmt.Sprintf("expected RestApiError payload to have code %s, got %s", code, restApiFormattedError.Code))
return
}
//success
return
}
c.Fail(fmt.Sprintf("expected error to be an RestApiError, got %T", err))
}