You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.2 KiB
94 lines
2.2 KiB
package logging
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/middleware"
|
|
"github.com/go-kratos/kratos/v2/transport"
|
|
)
|
|
|
|
var (
|
|
_ transport.Transporter = &Transport{}
|
|
)
|
|
|
|
type Transport struct {
|
|
kind transport.Kind
|
|
endpoint string
|
|
operation string
|
|
}
|
|
|
|
func (tr *Transport) Kind() transport.Kind {
|
|
return tr.kind
|
|
}
|
|
func (tr *Transport) Endpoint() string {
|
|
return tr.endpoint
|
|
}
|
|
func (tr *Transport) Operation() string {
|
|
return tr.operation
|
|
}
|
|
func (tr *Transport) RequestHeader() transport.Header {
|
|
return nil
|
|
}
|
|
func (tr *Transport) ReplyHeader() transport.Header {
|
|
return nil
|
|
}
|
|
|
|
func TestHTTP(t *testing.T) {
|
|
var err = errors.New("reply.error")
|
|
var bf = bytes.NewBuffer(nil)
|
|
var logger = log.NewStdLogger(bf)
|
|
|
|
tests := []struct {
|
|
name string
|
|
kind func(logger log.Logger) middleware.Middleware
|
|
err error
|
|
ctx context.Context
|
|
}{
|
|
{"http-server@fail",
|
|
Server,
|
|
err,
|
|
func() context.Context {
|
|
return transport.NewServerContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
|
|
}(),
|
|
},
|
|
{"http-server@succ",
|
|
Server,
|
|
nil,
|
|
func() context.Context {
|
|
return transport.NewServerContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
|
|
}(),
|
|
},
|
|
{"http-client@succ",
|
|
Client,
|
|
nil,
|
|
func() context.Context {
|
|
return transport.NewClientContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
|
|
|
|
}(),
|
|
},
|
|
{"http-client@fail",
|
|
Client,
|
|
err,
|
|
func() context.Context {
|
|
return transport.NewClientContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
|
|
}(),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
bf.Reset()
|
|
next := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
return "reply", test.err
|
|
}
|
|
next = test.kind(logger)(next)
|
|
v, e := next(test.ctx, "req.args")
|
|
t.Logf("[%s]reply: %v, error: %v", test.name, v, e)
|
|
t.Logf("[%s]log:%s", test.name, bf.String())
|
|
})
|
|
}
|
|
}
|
|
|