test(transport) add test (#1325)
* test * test client and server * return * add args * calloption * testpull/1331/head
parent
ef6b347d6b
commit
a128566cef
@ -0,0 +1,37 @@ |
||||
package direct |
||||
|
||||
import ( |
||||
"github.com/stretchr/testify/assert" |
||||
"google.golang.org/grpc/resolver" |
||||
"google.golang.org/grpc/serviceconfig" |
||||
"testing" |
||||
) |
||||
|
||||
func TestDirectBuilder_Scheme(t *testing.T) { |
||||
b := NewBuilder() |
||||
assert.Equal(t, "direct", b.Scheme()) |
||||
} |
||||
|
||||
type mockConn struct { |
||||
} |
||||
|
||||
func (m *mockConn) UpdateState(resolver.State) error { |
||||
return nil |
||||
} |
||||
|
||||
func (m *mockConn) ReportError(error) {} |
||||
|
||||
func (m *mockConn) NewAddress(addresses []resolver.Address) {} |
||||
|
||||
func (m *mockConn) NewServiceConfig(serviceConfig string) {} |
||||
|
||||
func (m *mockConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult { |
||||
return nil |
||||
} |
||||
|
||||
func TestDirectBuilder_Build(t *testing.T) { |
||||
b := NewBuilder() |
||||
r, err := b.Build(resolver.Target{}, &mockConn{}, resolver.BuildOptions{}) |
||||
assert.NoError(t, err) |
||||
r.ResolveNow(resolver.ResolveNowOptions{}) |
||||
} |
@ -0,0 +1 @@ |
||||
package direct |
@ -0,0 +1,81 @@ |
||||
package discovery |
||||
|
||||
import ( |
||||
"context" |
||||
"github.com/go-kratos/kratos/v2/log" |
||||
"github.com/go-kratos/kratos/v2/registry" |
||||
"github.com/stretchr/testify/assert" |
||||
"google.golang.org/grpc/resolver" |
||||
"google.golang.org/grpc/serviceconfig" |
||||
"testing" |
||||
"time" |
||||
) |
||||
|
||||
type mockLogger struct { |
||||
level log.Level |
||||
key string |
||||
val string |
||||
} |
||||
|
||||
func (l *mockLogger) Log(level log.Level, keyvals ...interface{}) error { |
||||
l.level = level |
||||
l.key = keyvals[0].(string) |
||||
l.val = keyvals[1].(string) |
||||
return nil |
||||
} |
||||
|
||||
func TestWithLogger(t *testing.T) { |
||||
b := &builder{} |
||||
WithLogger(&mockLogger{})(b) |
||||
} |
||||
|
||||
func TestWithInsecure(t *testing.T) { |
||||
b := &builder{} |
||||
WithInsecure(true)(b) |
||||
assert.True(t, b.insecure) |
||||
} |
||||
|
||||
func TestWithTimeout(t *testing.T) { |
||||
o := &builder{} |
||||
v := time.Duration(123) |
||||
WithTimeout(v)(o) |
||||
assert.Equal(t, v, o.timeout) |
||||
} |
||||
|
||||
type mockDiscovery struct { |
||||
} |
||||
|
||||
func (m *mockDiscovery) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { |
||||
return nil, nil |
||||
} |
||||
func (m *mockDiscovery) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { |
||||
return &testWatch{}, nil |
||||
} |
||||
|
||||
func TestBuilder_Scheme(t *testing.T) { |
||||
b := NewBuilder(&mockDiscovery{}) |
||||
assert.Equal(t, "discovery", b.Scheme()) |
||||
} |
||||
|
||||
type mockConn struct { |
||||
} |
||||
|
||||
func (m *mockConn) UpdateState(resolver.State) error { |
||||
return nil |
||||
} |
||||
|
||||
func (m *mockConn) ReportError(error) {} |
||||
|
||||
func (m *mockConn) NewAddress(addresses []resolver.Address) {} |
||||
|
||||
func (m *mockConn) NewServiceConfig(serviceConfig string) {} |
||||
|
||||
func (m *mockConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult { |
||||
return nil |
||||
} |
||||
|
||||
func TestBuilder_Build(t *testing.T) { |
||||
b := NewBuilder(&mockDiscovery{}) |
||||
_, err := b.Build(resolver.Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "gprc://authority/endpoint"}, &mockConn{}, resolver.BuildOptions{}) |
||||
assert.NoError(t, err) |
||||
} |
@ -0,0 +1,68 @@ |
||||
package http |
||||
|
||||
import ( |
||||
"net/http" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestEmptyCallOptions(t *testing.T) { |
||||
assert.NoError(t, EmptyCallOption{}.before(&callInfo{})) |
||||
EmptyCallOption{}.after(&callInfo{}, &csAttempt{}) |
||||
} |
||||
|
||||
func TestContentType(t *testing.T) { |
||||
assert.Equal(t, "aaa", ContentType("aaa").(ContentTypeCallOption).ContentType) |
||||
} |
||||
|
||||
func TestContentTypeCallOption_before(t *testing.T) { |
||||
c := &callInfo{} |
||||
err := ContentType("aaa").before(c) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, "aaa", c.contentType) |
||||
} |
||||
|
||||
func TestDefaultCallInfo(t *testing.T) { |
||||
path := "hi" |
||||
rv := defaultCallInfo(path) |
||||
assert.Equal(t, path, rv.pathTemplate) |
||||
assert.Equal(t, path, rv.operation) |
||||
assert.Equal(t, "application/json", rv.contentType) |
||||
} |
||||
|
||||
func TestOperation(t *testing.T) { |
||||
assert.Equal(t, "aaa", Operation("aaa").(OperationCallOption).Operation) |
||||
} |
||||
|
||||
func TestOperationCallOption_before(t *testing.T) { |
||||
c := &callInfo{} |
||||
err := Operation("aaa").before(c) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, "aaa", c.operation) |
||||
} |
||||
|
||||
func TestPathTemplate(t *testing.T) { |
||||
assert.Equal(t, "aaa", PathTemplate("aaa").(PathTemplateCallOption).Pattern) |
||||
} |
||||
|
||||
func TestPathTemplateCallOption_before(t *testing.T) { |
||||
c := &callInfo{} |
||||
err := PathTemplate("aaa").before(c) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, "aaa", c.pathTemplate) |
||||
} |
||||
|
||||
func TestHeader(t *testing.T) { |
||||
h := http.Header{"A": []string{"123"}} |
||||
assert.Equal(t, "123", Header(&h).(HeaderCallOption).header.Get("A")) |
||||
} |
||||
|
||||
func TestHeaderCallOption_after(t *testing.T) { |
||||
h := http.Header{"A": []string{"123"}} |
||||
c := &callInfo{} |
||||
cs := &csAttempt{res: &http.Response{Header: h}} |
||||
o := Header(&h) |
||||
o.after(c, cs) |
||||
assert.Equal(t, &h, o.(HeaderCallOption).header) |
||||
} |
@ -0,0 +1 @@ |
||||
package http |
Loading…
Reference in new issue