Skip to content

Commit

Permalink
feat: improve test tool
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Nov 29, 2022
1 parent 3dee271 commit 8d1a9d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
27 changes: 6 additions & 21 deletions pkg/core/tests/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
)

type GRPCTestCase struct {
Addr string
Timeout time.Duration
Method string
Args interface{}
Args proto.Message

ExpectError error
ExpectMetadata metadata.MD
ExpectReply interface{}
ExpectError error
ExpectReply proto.Message
}

// RunGRPCTestCase runs a test case against the given handler.
Expand All @@ -54,24 +52,11 @@ func RunGRPCTestCase(gtc GRPCTestCase) {
assert.Nil(ginkgoT, err)

reply := reflect.New(reflect.TypeOf(gtc.ExpectReply).Elem())

metadata := metadata.New(nil)

err = clientConn.Invoke(ctx, gtc.Method, gtc.Args, reply.Interface(),
grpc.Header(&metadata))
err = clientConn.Invoke(ctx, gtc.Method, gtc.Args, reply.Interface())
assert.Equal(ginkgoT, gtc.ExpectError, err)

replyData, err := proto.Marshal(reply.Interface().(proto.Message))
assert.Nil(ginkgoT, err)

expectReplyData, err := proto.Marshal(gtc.ExpectReply.(proto.Message))
assert.Nil(ginkgoT, err)

if gtc.ExpectMetadata != nil {
assert.EqualValues(ginkgoT, gtc.ExpectMetadata, metadata)
}

assert.Equal(ginkgoT, string(expectReplyData), string(replyData))
assert.True(ginkgoT, proto.Equal(gtc.ExpectReply, reply.Interface().(proto.Message)),
"expected: %s\nactually: %s", gtc.ExpectReply, reply.Interface().(proto.Message))

assert.Nil(ginkgoT, clientConn.Close())
}
11 changes: 7 additions & 4 deletions pkg/core/tests/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ func RunHTTPTestCase(htc HTTPTestCase) {

res, err := req.Execute(htc.Method, htc.Host+htc.Path)

assert.Nil(ginkgoT, err, err)
assert.Nil(ginkgoT, err, "error: %s", err)

if htc.ExpectStatus > 0 {
assert.Equal(ginkgoT, htc.ExpectStatus, res.StatusCode())
assert.Equal(ginkgoT, htc.ExpectStatus, res.StatusCode(),
"expected: %s\nactually: %s", htc.ExpectStatus, res.StatusCode())
}

if len(htc.ExpectHeader) > 0 {
assert.EqualValues(ginkgoT, htc.ExpectHeader, res.Header())
assert.EqualValues(ginkgoT, htc.ExpectHeader, res.Header(),
"expected: %s\nactually: %s", htc.ExpectHeader, res.Header())
}

assert.Equal(ginkgoT, htc.ExpectBody, res.String())
assert.Equal(ginkgoT, htc.ExpectBody, res.String(),
"expected: %s\nactually: %s", htc.ExpectBody, res.String())
}

0 comments on commit 8d1a9d7

Please sign in to comment.