-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
936 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package gateway | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
api "github.com/begonia-org/go-sdk/api/example/v1" | ||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
c "github.com/smartystreets/goconvey/convey" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/types/dynamicpb" | ||
) | ||
|
||
func TestMask(t *testing.T) { | ||
c.Convey("TestMask", t, func() { | ||
pb := &api.ExampleMessage{} | ||
dpb := dynamicpb.NewMessage(pb.ProtoReflect().Descriptor()) | ||
r := bytes.NewReader([]byte(`{"message":"John Doe","msg":{"msg":"hello world"},"allow":"DENY","repeated_msg":[{"msg":"John Doe"}]}`)) | ||
decoder := NewMaskDecoder(NewJsonDecoder(r)) | ||
err := decoder.Decode(dpb) | ||
c.So(err, c.ShouldBeNil) | ||
bData, err := protojson.Marshal(dpb) | ||
c.So(err, c.ShouldBeNil) | ||
err = protojson.Unmarshal(bData, pb) | ||
c.So(err, c.ShouldBeNil) | ||
c.So(pb.Message, c.ShouldEqual, "John Doe") | ||
c.So(pb.Allow, c.ShouldEqual, api.EnumAllow_DENY) | ||
c.So(pb.RepeatedMsg[0].Msg, c.ShouldEqual, "John Doe") | ||
c.So(pb.Msg.Msg, c.ShouldEqual, "hello world") | ||
c.So(pb.Mask.Paths, c.ShouldNotBeEmpty) | ||
|
||
pb2 := &api.HelloReply{} | ||
dpb2 := dynamicpb.NewMessage(pb2.ProtoReflect().Descriptor()) | ||
r2 := bytes.NewReader([]byte(`{"message":"John Doe","name":"tester"}`)) | ||
decoder2 := NewMaskDecoder(NewJsonDecoder(r2)) | ||
err = decoder2.Decode(dpb2) | ||
c.So(err, c.ShouldBeNil) | ||
}) | ||
} | ||
func TestDecodeErr(t *testing.T) { | ||
c.Convey("TestDecodeErr", t, func() { | ||
cases := []struct { | ||
patch interface{} | ||
output []interface{} | ||
err error | ||
}{ | ||
{ | ||
patch: (*runtime.DecoderWrapper).Decode, | ||
output: []interface{}{fmt.Errorf("test")}, | ||
err: fmt.Errorf("test"), | ||
}, | ||
{ | ||
patch: json.Marshal, | ||
output: []interface{}{nil, fmt.Errorf("test DECODE")}, | ||
err: fmt.Errorf("test DECODE"), | ||
}, | ||
} | ||
for _, caseV := range cases { | ||
pb := &api.ExampleMessage{} | ||
dpb := dynamicpb.NewMessage(pb.ProtoReflect().Descriptor()) | ||
r := bytes.NewReader([]byte(`{"message":"John Doe","msg":{"msg":"hello world"},"allow":"DENY","repeated_msg":[{"msg":"John Doe"}]}`)) | ||
patch := gomonkey.ApplyFuncReturn(caseV.patch, caseV.output...) | ||
defer patch.Reset() | ||
decoder := NewMaskDecoder(NewJsonDecoder(r)) | ||
|
||
err := decoder.Decode(dpb) | ||
patch.Reset() | ||
c.So(err, c.ShouldNotBeNil) | ||
c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error()) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package gateway | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
c "github.com/smartystreets/goconvey/convey" | ||
"google.golang.org/genproto/googleapis/api/httpbody" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/dynamicpb" | ||
) | ||
|
||
func TestRawBinaryUnmarshaler(t *testing.T) { | ||
c.Convey("TestRawBinaryUnmarshaler", t, func() { | ||
r := bytes.NewReader([]byte(`{"test":"test"}`)) | ||
marshal := NewRawBinaryUnmarshaler() | ||
decoder := marshal.NewDecoder(r) | ||
val := make(map[string]string) | ||
err := decoder.Decode(&val) | ||
c.So(err, c.ShouldBeNil) | ||
|
||
r2 := bytes.NewReader([]byte(`"test":"test"}`)) | ||
decoder2 := marshal.NewDecoder(r2) | ||
err = decoder2.Decode(&val) | ||
c.So(err, c.ShouldNotBeNil) | ||
// t.Logf("err: %v", err) | ||
|
||
body := &httpbody.HttpBody{ | ||
Data: []byte("test"), | ||
} | ||
msg := dynamicpb.NewMessage(body.ProtoReflect().Descriptor()).New() | ||
|
||
r3 := bytes.NewReader([]byte(`test`)) | ||
decoder3 := marshal.NewDecoder(r3) | ||
err = decoder3.Decode(msg) | ||
c.So(err, c.ShouldBeNil) | ||
c.So(msg.Get(msg.Descriptor().Fields().ByName("content_type")).String(), c.ShouldEqual, "application/octet-stream") | ||
c.So(msg.Get(msg.Descriptor().Fields().ByName("data")).Bytes(), c.ShouldEqual, []byte(`test`)) | ||
|
||
httpBody := &httpbody.HttpBody{ | ||
ContentType: "application/octet-stream-test", | ||
Data: []byte("test"), | ||
} | ||
msg2 := dynamicpb.NewMessage(httpBody.ProtoReflect().Descriptor()).New() | ||
httpBodyBytes, err := proto.Marshal(httpBody) | ||
c.So(err, c.ShouldBeNil) | ||
err2 := proto.Unmarshal(httpBodyBytes, msg2.Interface()) | ||
c.So(err2, c.ShouldBeNil) | ||
c.So(marshal.ContentType(msg2), c.ShouldContainSubstring, "application/octet-stream-test") | ||
|
||
c.So(decoder3.Decode(nil),c.ShouldBeNil) | ||
}) | ||
} | ||
|
||
func TestRawBinaryDecodeErr(t *testing.T) { | ||
c.Convey("TestRawBinaryDecodeErr", t, func() { | ||
cases := []struct { | ||
patch interface{} | ||
err error | ||
output []interface{} | ||
}{ | ||
{ | ||
patch: io.ReadAll, | ||
output: []interface{}{[]byte{}, fmt.Errorf("io.ReadAll: unexpected EOF")}, | ||
err: fmt.Errorf("io.ReadAll: unexpected EOF"), | ||
}, | ||
{ | ||
patch: io.ReadAll, | ||
output: []interface{}{[]byte{}, nil}, | ||
err: io.EOF, | ||
}, | ||
{ | ||
patch: proto.Marshal, | ||
output: []interface{}{nil, fmt.Errorf("proto.Marshal: nil")}, | ||
err: fmt.Errorf("proto.Marshal: nil"), | ||
}, | ||
} | ||
marshal := NewRawBinaryUnmarshaler() | ||
|
||
for _, caseV := range cases { | ||
body := &httpbody.HttpBody{ | ||
Data: []byte("test"), | ||
} | ||
msg := dynamicpb.NewMessage(body.ProtoReflect().Descriptor()).New() | ||
|
||
r3 := bytes.NewReader([]byte(`test`)) | ||
decoder3 := marshal.NewDecoder(r3) | ||
patch := gomonkey.ApplyFuncReturn(caseV.patch, caseV.output...) | ||
defer patch.Reset() | ||
err := decoder3.Decode(msg) | ||
patch.Reset() | ||
c.So(err, c.ShouldNotBeNil) | ||
c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error()) | ||
|
||
} | ||
|
||
}) | ||
} |
Oops, something went wrong.