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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 17:07:49 +03:00 committed by Alex Vanin
parent 8f8e977a3e
commit 986bc1b0ea
4 changed files with 45 additions and 4 deletions

View file

@ -124,3 +124,23 @@ func (r *RequestMetaHeader) UnmarshalJSON(data []byte) error {
return nil
}
func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
RequestVerificationHeaderToGRPCMessage(r),
)
}
func (r *RequestVerificationHeader) UnmarshalJSON(data []byte) error {
msg := new(session.RequestVerificationHeader)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*r = *RequestVerificationHeaderFromGRPCMessage(msg)
return nil
}

View file

@ -78,3 +78,15 @@ func TestRequestMetaHeaderJSON(t *testing.T) {
require.Equal(t, r, r2)
}
func TestRequestVerificationHeaderJSON(t *testing.T) {
r := generateRequestVerificationHeader("key", "value")
data, err := r.MarshalJSON()
require.NoError(t, err)
r2 := new(session.RequestVerificationHeader)
require.NoError(t, r2.UnmarshalJSON(data))
require.Equal(t, r, r2)
}

View file

@ -595,6 +595,17 @@ func (r *RequestVerificationHeader) StableSize() (size int) {
return size
}
func (r *RequestVerificationHeader) Unmarshal(data []byte) error {
m := new(session.RequestVerificationHeader)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*r = *RequestVerificationHeaderFromGRPCMessage(m)
return nil
}
func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
if r == nil {
return []byte{}, nil

View file

@ -134,16 +134,14 @@ func TestRequestVerificationHeader_StableMarshal(t *testing.T) {
verifHeaderOrigin := generateRequestVerificationHeader("Key", "Inside")
verifHeaderFrom := generateRequestVerificationHeader("Value", "Outside")
verifHeaderFrom.SetOrigin(verifHeaderOrigin)
transport := new(grpc.RequestVerificationHeader)
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.RequestVerificationHeader)
require.NoError(t, verifHeaderTo.Unmarshal(wire))
verifHeaderTo := session.RequestVerificationHeaderFromGRPCMessage(transport)
require.Equal(t, verifHeaderFrom, verifHeaderTo)
})
}