1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-11-06 08:59:18 +02:00
Files
kratos/middleware/recovery/recovery_test.go
darkweak c9fbb27b5b tests(coverage): Increase middleware tests coverage (#2165)
* tests(coverage): Increase tests coverage

* Lint fix
2022-07-08 19:53:21 +08:00

43 lines
993 B
Go

package recovery
import (
"context"
"fmt"
"testing"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
)
func TestOnce(t *testing.T) {
defer func() {
if recover() != nil {
t.Error("fail")
}
}()
next := func(ctx context.Context, req interface{}) (interface{}, error) {
panic("panic reason")
}
_, e := Recovery()(next)(context.Background(), "panic")
t.Logf("succ and reason is %v", e)
}
func TestNotPanic(t *testing.T) {
next := func(ctx context.Context, req interface{}) (interface{}, error) {
return req.(string) + "https://go-kratos.dev", nil
}
_, e := Recovery(WithHandler(func(ctx context.Context, req, err interface{}) error {
return errors.InternalServer("RECOVERY", fmt.Sprintf("panic triggered: %v", err))
}))(next)(context.Background(), "notPanic")
if e != nil {
t.Errorf("e isn't nil")
}
}
// Deprecated: Remove this test with WithLogger method.
func TestWithLogger(t *testing.T) {
_ = WithLogger(log.DefaultLogger)
}