parent
b68265c365
commit
f93084ba9f
@ -0,0 +1,121 @@ |
||||
package kratos |
||||
|
||||
import ( |
||||
"context" |
||||
"log" |
||||
"net/url" |
||||
"os" |
||||
"testing" |
||||
"time" |
||||
|
||||
xlog "github.com/go-kratos/kratos/v2/log" |
||||
"github.com/go-kratos/kratos/v2/registry" |
||||
"github.com/go-kratos/kratos/v2/transport" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestID(t *testing.T) { |
||||
o := &options{} |
||||
v := "123" |
||||
ID(v)(o) |
||||
assert.Equal(t, v, o.id) |
||||
} |
||||
|
||||
func TestName(t *testing.T) { |
||||
o := &options{} |
||||
v := "abc" |
||||
Name(v)(o) |
||||
assert.Equal(t, v, o.name) |
||||
} |
||||
|
||||
func TestVersion(t *testing.T) { |
||||
o := &options{} |
||||
v := "123" |
||||
Version(v)(o) |
||||
assert.Equal(t, v, o.version) |
||||
} |
||||
|
||||
func TestMetadata(t *testing.T) { |
||||
o := &options{} |
||||
v := map[string]string{ |
||||
"a": "1", |
||||
"b": "2", |
||||
} |
||||
Metadata(v)(o) |
||||
assert.Equal(t, v, o.metadata) |
||||
} |
||||
|
||||
func TestEndpoint(t *testing.T) { |
||||
o := &options{} |
||||
v := []*url.URL{ |
||||
{Host: "example.com"}, |
||||
{Host: "foo.com"}, |
||||
} |
||||
Endpoint(v...)(o) |
||||
assert.Equal(t, v, o.endpoints) |
||||
} |
||||
|
||||
func TestContext(t *testing.T) { |
||||
o := &options{} |
||||
v := context.WithValue(context.TODO(), "a", "b") |
||||
Context(v)(o) |
||||
assert.Equal(t, v, o.ctx) |
||||
} |
||||
|
||||
func TestLogger(t *testing.T) { |
||||
o := &options{} |
||||
v := xlog.NewStdLogger(log.Writer()) |
||||
Logger(v)(o) |
||||
assert.Equal(t, v, o.logger) |
||||
} |
||||
|
||||
type mockServer struct{} |
||||
|
||||
func (m *mockServer) Start(ctx context.Context) error { return nil } |
||||
func (m *mockServer) Stop(ctx context.Context) error { return nil } |
||||
|
||||
func TestServer(t *testing.T) { |
||||
o := &options{} |
||||
v := []transport.Server{ |
||||
&mockServer{}, &mockServer{}, |
||||
} |
||||
Server(v...)(o) |
||||
assert.Equal(t, v, o.servers) |
||||
} |
||||
|
||||
type mockSignal struct{} |
||||
|
||||
func (m *mockSignal) String() string { return "sig" } |
||||
func (m *mockSignal) Signal() {} |
||||
|
||||
func TestSignal(t *testing.T) { |
||||
o := &options{} |
||||
v := []os.Signal{ |
||||
&mockSignal{}, &mockSignal{}, |
||||
} |
||||
Signal(v...)(o) |
||||
assert.Equal(t, v, o.sigs) |
||||
} |
||||
|
||||
type mockRegistrar struct{} |
||||
|
||||
func (m *mockRegistrar) Register(ctx context.Context, service *registry.ServiceInstance) error { |
||||
return nil |
||||
} |
||||
func (m *mockRegistrar) Deregister(ctx context.Context, service *registry.ServiceInstance) error { |
||||
return nil |
||||
} |
||||
|
||||
func TestRegistrar(t *testing.T) { |
||||
o := &options{} |
||||
v := &mockRegistrar{} |
||||
Registrar(v)(o) |
||||
assert.Equal(t, v, o.registrar) |
||||
} |
||||
|
||||
func TestRegistrarTimeout(t *testing.T) { |
||||
o := &options{} |
||||
v := time.Duration(123) |
||||
RegistrarTimeout(v)(o) |
||||
assert.Equal(t, v, o.registrarTimeout) |
||||
} |
@ -0,0 +1,76 @@ |
||||
package grpc |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/middleware" |
||||
"github.com/go-kratos/kratos/v2/registry" |
||||
"github.com/stretchr/testify/assert" |
||||
"google.golang.org/grpc" |
||||
) |
||||
|
||||
func TestWithEndpoint(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := "abc" |
||||
WithEndpoint(v)(o) |
||||
assert.Equal(t, v, o.endpoint) |
||||
} |
||||
|
||||
func TestWithMiddleware(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := []middleware.Middleware{ |
||||
func(middleware.Handler) middleware.Handler { return nil }, |
||||
} |
||||
WithMiddleware(v...)(o) |
||||
assert.Equal(t, v, o.middleware) |
||||
} |
||||
|
||||
type mockRegistry struct{} |
||||
|
||||
func (m *mockRegistry) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { |
||||
return nil, nil |
||||
} |
||||
func (m *mockRegistry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { |
||||
return nil, nil |
||||
} |
||||
|
||||
func TestWithDiscovery(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := &mockRegistry{} |
||||
WithDiscovery(v)(o) |
||||
assert.Equal(t, v, o.discovery) |
||||
} |
||||
|
||||
func TestWithTLSConfig(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := &tls.Config{} |
||||
WithTLSConfig(v)(o) |
||||
assert.Equal(t, v, o.tlsConf) |
||||
} |
||||
|
||||
func TestWithUnaryInterceptor(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := []grpc.UnaryClientInterceptor{ |
||||
func(ctx context.Context, method string, req, reply interface{}, |
||||
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
||||
return nil |
||||
}, |
||||
func(ctx context.Context, method string, req, reply interface{}, |
||||
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
||||
return nil |
||||
}, |
||||
} |
||||
WithUnaryInterceptor(v...)(o) |
||||
assert.Equal(t, v, o.ints) |
||||
} |
||||
|
||||
func TestWithOptions(t *testing.T) { |
||||
o := &clientOptions{} |
||||
v := []grpc.DialOption{ |
||||
grpc.EmptyDialOption{}, |
||||
} |
||||
WithOptions(v...)(o) |
||||
assert.Equal(t, v, o.grpcOpts) |
||||
} |
@ -0,0 +1,46 @@ |
||||
package grpc |
||||
|
||||
import ( |
||||
"github.com/stretchr/testify/assert" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/transport" |
||||
) |
||||
|
||||
func TestTransport_Kind(t *testing.T) { |
||||
o := &Transport{} |
||||
assert.Equal(t, transport.KindGRPC, o.Kind()) |
||||
} |
||||
|
||||
func TestTransport_Endpoint(t *testing.T) { |
||||
v := "hello" |
||||
o := &Transport{endpoint: v} |
||||
assert.Equal(t, v, o.Endpoint()) |
||||
} |
||||
|
||||
func TestTransport_Operation(t *testing.T) { |
||||
v := "hello" |
||||
o := &Transport{operation: v} |
||||
assert.Equal(t, v, o.Operation()) |
||||
} |
||||
|
||||
func TestTransport_RequestHeader(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("a", "1") |
||||
o := &Transport{reqHeader: v} |
||||
assert.Equal(t, "1", o.RequestHeader().Get("a")) |
||||
} |
||||
|
||||
func TestTransport_ReplyHeader(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("a", "1") |
||||
o := &Transport{replyHeader: v} |
||||
assert.Equal(t, "1", o.ReplyHeader().Get("a")) |
||||
} |
||||
|
||||
func TestHeaderCarrier_Keys(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("abb", "1") |
||||
v.Set("bcc", "2") |
||||
assert.ElementsMatch(t, []string{"abb", "bcc"}, v.Keys()) |
||||
} |
@ -0,0 +1,59 @@ |
||||
package http |
||||
|
||||
import ( |
||||
"net/http" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/transport" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestTransport_Kind(t *testing.T) { |
||||
o := &Transport{} |
||||
assert.Equal(t, transport.KindHTTP, o.Kind()) |
||||
} |
||||
|
||||
func TestTransport_Endpoint(t *testing.T) { |
||||
v := "hello" |
||||
o := &Transport{endpoint: v} |
||||
assert.Equal(t, v, o.Endpoint()) |
||||
} |
||||
|
||||
func TestTransport_Operation(t *testing.T) { |
||||
v := "hello" |
||||
o := &Transport{operation: v} |
||||
assert.Equal(t, v, o.Operation()) |
||||
} |
||||
|
||||
func TestTransport_Request(t *testing.T) { |
||||
v := &http.Request{} |
||||
o := &Transport{request: v} |
||||
assert.Same(t, v, o.Request()) |
||||
} |
||||
|
||||
func TestTransport_RequestHeader(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("a", "1") |
||||
o := &Transport{reqHeader: v} |
||||
assert.Equal(t, "1", o.RequestHeader().Get("a")) |
||||
} |
||||
|
||||
func TestTransport_ReplyHeader(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("a", "1") |
||||
o := &Transport{replyHeader: v} |
||||
assert.Equal(t, "1", o.ReplyHeader().Get("a")) |
||||
} |
||||
|
||||
func TestTransport_PathTemplate(t *testing.T) { |
||||
v := "template" |
||||
o := &Transport{pathTemplate: v} |
||||
assert.Equal(t, v, o.PathTemplate()) |
||||
} |
||||
|
||||
func TestHeaderCarrier_Keys(t *testing.T) { |
||||
v := headerCarrier{} |
||||
v.Set("abb", "1") |
||||
v.Set("bcc", "2") |
||||
assert.ElementsMatch(t, []string{"Abb", "Bcc"}, v.Keys()) |
||||
} |
Loading…
Reference in new issue