[#168] session: Implement binary/JSON encoders/decoders on ResponseVerify

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 17:14:43 +03:00 committed by Alex Vanin
parent edf6ec05b3
commit 6456fcf8fa
4 changed files with 45 additions and 4 deletions

View file

@ -164,3 +164,23 @@ func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error {
return nil 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
}

View file

@ -102,3 +102,15 @@ func TestResponseMetaHeaderJSON(t *testing.T) {
require.Equal(t, r, r2) 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)
}

View file

@ -745,3 +745,14 @@ func (r *ResponseVerificationHeader) StableSize() (size int) {
return size 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
}

View file

@ -166,16 +166,14 @@ func TestResponseVerificationHeader_StableMarshal(t *testing.T) {
verifHeaderOrigin := generateResponseVerificationHeader("Key", "Inside") verifHeaderOrigin := generateResponseVerificationHeader("Key", "Inside")
verifHeaderFrom := generateResponseVerificationHeader("Value", "Outside") verifHeaderFrom := generateResponseVerificationHeader("Value", "Outside")
verifHeaderFrom.SetOrigin(verifHeaderOrigin) verifHeaderFrom.SetOrigin(verifHeaderOrigin)
transport := new(grpc.ResponseVerificationHeader)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
wire, err := verifHeaderFrom.StableMarshal(nil) wire, err := verifHeaderFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, transport) verifHeaderTo := new(session.ResponseVerificationHeader)
require.NoError(t, err) require.NoError(t, verifHeaderTo.Unmarshal(wire))
verifHeaderTo := session.ResponseVerificationHeaderFromGRPCMessage(transport)
require.Equal(t, verifHeaderFrom, verifHeaderTo) require.Equal(t, verifHeaderFrom, verifHeaderTo)
}) })
} }