test(middleware/) add test (#1379)

* test(middleware/) add test
pull/1384/head
yuemoxi 3 years ago committed by GitHub
parent 10ecd91fe9
commit 12f17a3e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      middleware/metrics/metrics_test.go
  2. 19
      middleware/recovery/recovery_test.go
  3. 1
      middleware/tracing/metadata.go
  4. 89
      middleware/tracing/metadata_test.go

@ -0,0 +1,18 @@
package metrics
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
)
func TestMetrics(t *testing.T) {
next := func(ctx context.Context, req interface{}) (interface{}, error) {
return req.(string) + "https://go-kratos.dev", nil
}
_, err := Server()(next)(context.Background(), "test:")
assert.Equal(t, err, nil)
_, err = Client()(next)(context.Background(), "test:")
assert.Equal(t, err, nil)
}

@ -2,7 +2,11 @@ package recovery
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
) )
func TestOnce(t *testing.T) { func TestOnce(t *testing.T) {
@ -15,6 +19,19 @@ func TestOnce(t *testing.T) {
next := func(ctx context.Context, req interface{}) (interface{}, error) { next := func(ctx context.Context, req interface{}) (interface{}, error) {
panic("panic reason") panic("panic reason")
} }
_, e := Recovery()(next)(context.Background(), "panic") _, e := Recovery(WithLogger(log.DefaultLogger))(next)(context.Background(), "panic")
t.Logf("succ and reason is %v", e) 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")
}
}

@ -2,7 +2,6 @@ package tracing
import ( import (
"context" "context"
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/metadata" "github.com/go-kratos/kratos/v2/metadata"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"

@ -0,0 +1,89 @@
package tracing
import (
"context"
"testing"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/metadata"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/propagation"
)
func TestMetadata_Inject(t *testing.T) {
type args struct {
appName string
carrier propagation.TextMapCarrier
}
tests := []struct {
name string
args args
want string
}{
{
name: "https://go-kratos.dev",
args: args{"https://go-kratos.dev", propagation.HeaderCarrier{}},
want: "https://go-kratos.dev",
},
{
name: "https://github.com/go-kratos/kratos",
args: args{"https://github.com/go-kratos/kratos", propagation.HeaderCarrier{"mode": []string{"test"}}},
want: "https://github.com/go-kratos/kratos",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := kratos.New(kratos.Name(tt.args.appName))
ctx := kratos.NewContext(context.Background(), a)
var m = new(Metadata)
m.Inject(ctx, tt.args.carrier)
if res := tt.args.carrier.Get(serviceHeader); tt.want != res {
t.Errorf("Get(serviceHeader) :%s want: %s", res, tt.want)
}
})
}
}
func TestMetadata_Extract(t *testing.T) {
type args struct {
parent context.Context
carrier propagation.TextMapCarrier
}
tests := []struct {
name string
args args
want string
}{
{
name: "https://go-kratos.dev",
args: args{
parent: context.Background(),
carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://go-kratos.dev"}},
},
want: "https://go-kratos.dev",
},
{
name: "https://github.com/go-kratos/kratos",
args: args{
parent: metadata.NewServerContext(context.Background(), metadata.Metadata{}),
carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://github.com/go-kratos/kratos"}},
},
want: "https://github.com/go-kratos/kratos",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := Metadata{}
ctx := b.Extract(tt.args.parent, tt.args.carrier)
md, ok := metadata.FromServerContext(ctx)
assert.Equal(t, ok, true)
assert.Equal(t, md.Get(serviceHeader), tt.want)
})
}
}
func TestFields(t *testing.T) {
b := Metadata{}
assert.Equal(t, b.Fields(), []string{"x-md-service-name"})
}
Loading…
Cancel
Save