test: option tests (#1292)

* test options

* more tests
pull/1293/head
Windfarer 3 years ago committed by GitHub
parent b68265c365
commit f93084ba9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app_test.go
  2. 1
      examples/go.sum
  3. 121
      options_test.go
  4. 76
      transport/grpc/client_test.go
  5. 69
      transport/grpc/server_test.go
  6. 46
      transport/grpc/transport_test.go
  7. 27
      transport/http/client_test.go
  8. 64
      transport/http/server_test.go
  9. 59
      transport/http/transport_test.go

@ -6,6 +6,7 @@ import (
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/stretchr/testify/assert"
)
func TestApp(t *testing.T) {
@ -23,3 +24,30 @@ func TestApp(t *testing.T) {
t.Fatal(err)
}
}
func TestApp_ID(t *testing.T) {
v := "123"
o := New(ID(v))
assert.Equal(t, v, o.ID())
}
func TestApp_Name(t *testing.T) {
v := "123"
o := New(Name(v))
assert.Equal(t, v, o.Name())
}
func TestApp_Version(t *testing.T) {
v := "123"
o := New(Version(v))
assert.Equal(t, v, o.Version())
}
func TestApp_Metadata(t *testing.T) {
v := map[string]string{
"a": "1",
"b": "2",
}
o := New(Metadata(v))
assert.Equal(t, v, o.Metadata())
}

@ -445,6 +445,7 @@ github.com/longXboy/grpc-gateway/v2 v2.0.0-20210707031540-bd2d73d86cee/go.mod h1
github.com/lyft/protoc-gen-star v0.5.1/go.mod h1:9toiA3cC7z5uVbODF7kEQ91Xn7XNFkVUl+SrEe+ZORU=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

@ -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)
}

@ -2,9 +2,14 @@ package grpc
import (
"context"
"crypto/tls"
"github.com/go-kratos/kratos/v2/middleware"
"google.golang.org/grpc"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testKey struct{}
@ -41,3 +46,67 @@ func testClient(t *testing.T, srv *Server) {
}
conn.Close()
}
func TestNetwork(t *testing.T) {
o := &Server{}
v := "abc"
Network(v)(o)
assert.Equal(t, v, o.network)
}
func TestAddress(t *testing.T) {
o := &Server{}
v := "abc"
Address(v)(o)
assert.Equal(t, v, o.address)
}
func TestTimeout(t *testing.T) {
o := &Server{}
v := time.Duration(123)
Timeout(v)(o)
assert.Equal(t, v, o.timeout)
}
func TestMiddleware(t *testing.T) {
o := &clientOptions{}
v := []middleware.Middleware{
func(middleware.Handler) middleware.Handler { return nil },
}
WithMiddleware(v...)(o)
assert.Equal(t, v, o.middleware)
}
func TestLogger(t *testing.T) {
//todo
}
func TestTLSConfig(t *testing.T) {
o := &Server{}
v := &tls.Config{}
TLSConfig(v)(o)
assert.Equal(t, v, o.tlsConf)
}
func TestUnaryInterceptor(t *testing.T) {
o := &Server{}
v := []grpc.UnaryServerInterceptor{
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
return nil, nil
},
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
return nil, nil
},
}
UnaryInterceptor(v...)(o)
assert.Equal(t, v, o.ints)
}
func TestOptions(t *testing.T) {
o := &Server{}
v := []grpc.ServerOption{
grpc.EmptyServerOption{},
}
Options(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())
}

@ -10,10 +10,9 @@ import (
"time"
"github.com/go-kratos/kratos/v2/errors"
"github.com/stretchr/testify/assert"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/registry"
"github.com/stretchr/testify/assert"
)
type mockRoundTripper struct {
@ -52,6 +51,12 @@ func TestWithUserAgent(t *testing.T) {
}
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)
}
func TestWithEndpoint(t *testing.T) {
@ -63,14 +68,26 @@ func TestWithEndpoint(t *testing.T) {
}
func TestWithRequestEncoder(t *testing.T) {
o := &clientOptions{}
v := func(ctx context.Context, contentType string, in interface{}) (body []byte, err error) {
return nil, nil
}
WithRequestEncoder(v)(o)
assert.NotNil(t, o.encoder)
}
func TestWithResponseDecoder(t *testing.T) {
o := &clientOptions{}
v := func(ctx context.Context, res *nethttp.Response, out interface{}) error { return nil }
WithResponseDecoder(v)(o)
assert.NotNil(t, o.decoder)
}
func TestWithErrorDecoder(t *testing.T) {
o := &clientOptions{}
v := func(ctx context.Context, res *nethttp.Response) error { return nil }
WithErrorDecoder(v)(o)
assert.NotNil(t, o.errorDecoder)
}
type mockDiscovery struct {

@ -2,9 +2,11 @@ package http
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"io/ioutil"
"net/http"
"strings"
@ -153,3 +155,65 @@ func BenchmarkServer(b *testing.B) {
}
srv.Stop(ctx)
}
func TestNetwork(t *testing.T) {
o := &Server{}
v := "abc"
Network(v)(o)
assert.Equal(t, v, o.network)
}
func TestAddress(t *testing.T) {
o := &Server{}
v := "abc"
Address(v)(o)
assert.Equal(t, v, o.address)
}
func TestTimeout(t *testing.T) {
o := &Server{}
v := time.Duration(123)
Timeout(v)(o)
assert.Equal(t, v, o.timeout)
}
func TestLogger(t *testing.T) {
//todo
}
func TestMiddleware(t *testing.T) {
o := &clientOptions{}
v := []middleware.Middleware{
func(middleware.Handler) middleware.Handler { return nil },
}
WithMiddleware(v...)(o)
assert.Equal(t, v, o.middleware)
}
func TestRequestDecoder(t *testing.T) {
o := &Server{}
v := func(*http.Request, interface{}) error { return nil }
RequestDecoder(v)(o)
assert.NotNil(t, o.dec)
}
func TestResponseEncoder(t *testing.T) {
o := &Server{}
v := func(http.ResponseWriter, *http.Request, interface{}) error { return nil }
ResponseEncoder(v)(o)
assert.NotNil(t, o.enc)
}
func TestErrorEncoder(t *testing.T) {
o := &Server{}
v := func(http.ResponseWriter, *http.Request, error) {}
ErrorEncoder(v)(o)
assert.NotNil(t, o.ene)
}
func TestTLSConfig(t *testing.T) {
o := &Server{}
v := &tls.Config{}
TLSConfig(v)(o)
assert.Equal(t, v, o.tlsConf)
}

@ -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…
Cancel
Save