Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve test tool #590

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions pkg/core/tests/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ type GRPCTestCase struct {
Addr string
Timeout time.Duration
Method string
Args interface{}
Args proto.Message

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

// RunGRPCTestCase runs a test case against the given handler.
Expand All @@ -54,24 +54,19 @@ 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))
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)
assert.True(ginkgoT, proto.Equal(gtc.ExpectReply, reply.Interface().(proto.Message)),
"expected: %s\nactually: %s", gtc.ExpectReply, reply.Interface().(proto.Message))

if gtc.ExpectMetadata != nil {
assert.EqualValues(ginkgoT, gtc.ExpectMetadata, metadata)
assert.Equal(ginkgoT, gtc.ExpectMetadata, metadata,
"expected: %s\nactually: %s", gtc.ExpectMetadata, metadata)
}

assert.Equal(ginkgoT, string(expectReplyData), string(replyData))

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