Skip to content

Commit

Permalink
fix: verify grpc expect body (#223)
Browse files Browse the repository at this point in the history
* fix: verify grpc expect body

Signed-off-by: Ink33 <[email protected]>

* chore(grpc_test): add JSON cache bypass

Signed-off-by: Ink33 <[email protected]>

---------

Signed-off-by: Ink33 <[email protected]>
  • Loading branch information
Ink-33 authored Oct 9, 2023
1 parent 8b1cd29 commit 90b4b07
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/runner/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func parseExpect(md protoreflect.MethodDescriptor, expect testing.Response) (exp
}
msgb = append(msgb, '[')
for i := range msgpbs {
msg, _ := json.Marshal(msgpbs[i])
msg, _ := protojson.Marshal(msgpbs[i])
msgb = append(msgb, msg...)
msg = append(msg, ',')
}
Expand All @@ -552,7 +552,7 @@ func parseExpect(md protoreflect.MethodDescriptor, expect testing.Response) (exp
if err != nil {
return gjson.Result{}, err
}
msgb, _ = json.Marshal(msgpb)
msgb, _ = protojson.Marshal(msgpb)
}
return gjson.ParseBytes(msgb), nil
}
110 changes: 105 additions & 5 deletions pkg/runner/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
assert.Nil(t, err)
},
},
{
name: "test unary rpc not equal",
testCase: &atest.TestCase{
Request: atest.Request{
API: unary,
Body: "{}",
},
Expect: atest.Response{
Body: getJSONOrCache(nil, &testsrv.HelloReply{
Message: "Happy!",
}),
},
},
verify: func(t *testing.T, output any, err error) {
assert.NotNil(t, err)
},
},
{
name: "test client stream rpc",
testCase: &atest.TestCase{
Expand Down Expand Up @@ -351,6 +368,43 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
assert.Nil(t, err)
},
},
{
name: "test bid stream rpc len not equal",
testCase: &atest.TestCase{
Request: atest.Request{
API: bidStream,
Body: getJSONOrCache("stream", nil),
},
Expect: atest.Response{
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
{MsgID: 1, ExpectLen: 2},
{MsgID: 2, ExpectLen: 2},
}),
},
},
verify: func(t *testing.T, output any, err error) {
assert.NotNil(t, err)
},
},
{
name: "test bid stream rpc content not equal",
testCase: &atest.TestCase{
Request: atest.Request{
API: bidStream,
Body: getJSONOrCache("stream", nil),
},
Expect: atest.Response{
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
{MsgID: 4, ExpectLen: 3},
{MsgID: 5, ExpectLen: 3},
{MsgID: 6, ExpectLen: 3},
}),
},
},
verify: func(t *testing.T, output any, err error) {
assert.NotNil(t, err)
},
},
{
name: "test basic type",
testCase: &atest.TestCase{
Expand Down Expand Up @@ -404,6 +458,47 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
assert.Nil(t, err)
},
},
{
name: "test advanced type not equal",
testCase: &atest.TestCase{
Request: atest.Request{
API: advanced,
Body: getJSONOrCache("advanced",
&testsrv.AdvancedType{
Int32Array: []int32{rand.Int31(), rand.Int31()},
Int64Array: []int64{rand.Int63(), rand.Int63()},
Uint32Array: []uint32{rand.Uint32(), rand.Uint32()},
Uint64Array: []uint64{rand.Uint64(), rand.Uint64()},
Float32Array: []float32{rand.Float32(), rand.Float32()},
Float64Array: []float64{rand.NormFloat64(), rand.NormFloat64()},
StringArray: []string{time.Now().Format(time.RFC3339), time.Now().Format(time.RFC822)},
BoolArray: []bool{true, false},
HelloReplyMap: map[string]*testsrv.HelloReply{"key": {
Message: "Hello",
}},
}),
},
Expect: atest.Response{
Body: getJSONOrCache(nil,
&testsrv.AdvancedType{
Int32Array: []int32{rand.Int31(), rand.Int31()},
Int64Array: []int64{rand.Int63(), rand.Int63()},
Uint32Array: []uint32{rand.Uint32(), rand.Uint32()},
Uint64Array: []uint64{rand.Uint64(), rand.Uint64()},
Float32Array: []float32{rand.Float32(), rand.Float32()},
Float64Array: []float64{rand.NormFloat64(), rand.NormFloat64()},
StringArray: []string{time.Now().Format(time.RFC3339), time.Now().Format(time.RFC822)},
BoolArray: []bool{true, false},
HelloReplyMap: map[string]*testsrv.HelloReply{"key": {
Message: "Happy",
}},
}),
},
},
verify: func(t *testing.T, output any, err error) {
assert.NotNil(t, err)
},
},
{
name: "test unknown rpc",
testCase: &atest.TestCase{
Expand Down Expand Up @@ -506,13 +601,18 @@ func TestAPINameMatch(t *testing.T) {
)
}

func getJSONOrCache(k string, s any) (msg string) {
v, ok := cache.Load(k)
if ok {
// getJSONOrCache can store the JSON string of value.
//
// Let key be nil represent not using cache.
func getJSONOrCache(key any, value any) (msg string) {
v, ok := cache.Load(key)
if ok && key != nil {
return v.(string)
}
b, _ := json.Marshal(s)
b, _ := json.Marshal(value)
msg = string(b)
cache.Store(k, msg)
if key != nil {
cache.Store(key, msg)
}
return
}

0 comments on commit 90b4b07

Please sign in to comment.