From 6456fcf8fade90ce2895b6ace54ea6a4442e8357 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 17:14:43 +0300 Subject: [PATCH] [#168] session: Implement binary/JSON encoders/decoders on ResponseVerify Signed-off-by: Leonard Lyubich --- v2/session/json.go | 20 ++++++++++++++++++++ v2/session/json_test.go | 12 ++++++++++++ v2/session/marshal.go | 11 +++++++++++ v2/session/marshal_test.go | 6 ++---- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/v2/session/json.go b/v2/session/json.go index 36525d3e..2dea2d4b 100644 --- a/v2/session/json.go +++ b/v2/session/json.go @@ -164,3 +164,23 @@ func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error { return nil } + +func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + ResponseVerificationHeaderToGRPCMessage(r), + ) +} + +func (r *ResponseVerificationHeader) UnmarshalJSON(data []byte) error { + msg := new(session.ResponseVerificationHeader) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *r = *ResponseVerificationHeaderFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go index 442395c5..00bf382d 100644 --- a/v2/session/json_test.go +++ b/v2/session/json_test.go @@ -102,3 +102,15 @@ func TestResponseMetaHeaderJSON(t *testing.T) { require.Equal(t, r, r2) } + +func TestResponseVerificationHeaderJSON(t *testing.T) { + r := generateResponseVerificationHeader("key", "value") + + data, err := r.MarshalJSON() + require.NoError(t, err) + + r2 := new(session.ResponseVerificationHeader) + require.NoError(t, r2.UnmarshalJSON(data)) + + require.Equal(t, r, r2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index a10ee0ff..568dfed7 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -745,3 +745,14 @@ func (r *ResponseVerificationHeader) StableSize() (size int) { return size } + +func (r *ResponseVerificationHeader) Unmarshal(data []byte) error { + m := new(session.ResponseVerificationHeader) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *r = *ResponseVerificationHeaderFromGRPCMessage(m) + + return nil +} diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 4a79ef68..a2b5d9d2 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -166,16 +166,14 @@ func TestResponseVerificationHeader_StableMarshal(t *testing.T) { verifHeaderOrigin := generateResponseVerificationHeader("Key", "Inside") verifHeaderFrom := generateResponseVerificationHeader("Value", "Outside") verifHeaderFrom.SetOrigin(verifHeaderOrigin) - transport := new(grpc.ResponseVerificationHeader) t.Run("non empty", func(t *testing.T) { wire, err := verifHeaderFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + verifHeaderTo := new(session.ResponseVerificationHeader) + require.NoError(t, verifHeaderTo.Unmarshal(wire)) - verifHeaderTo := session.ResponseVerificationHeaderFromGRPCMessage(transport) require.Equal(t, verifHeaderFrom, verifHeaderTo) }) }