From ff54fb7647557a0cb01336782749f1da2ed1eaf6 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 27 Oct 2021 13:00:18 +0300 Subject: [PATCH] [#53] session: move XHeader from neofs-api-go Signed-off-by: Evgenii Stratonikov --- session/xheader.go | 55 ++++++++++++++++++++++++++++++++++++++ session/xheader_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 session/xheader.go create mode 100644 session/xheader_test.go diff --git a/session/xheader.go b/session/xheader.go new file mode 100644 index 0000000..9705941 --- /dev/null +++ b/session/xheader.go @@ -0,0 +1,55 @@ +package session + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/session" +) + +// XHeader represents v2-compatible XHeader. +type XHeader session.XHeader + +// NewXHeaderFromV2 wraps v2 XHeader message to XHeader. +// +// Nil session.XHeader converts to nil. +func NewXHeaderFromV2(v *session.XHeader) *XHeader { + return (*XHeader)(v) +} + +// NewXHeader creates, initializes and returns blank XHeader instance. +// +// Defaults: +// - key: ""; +// - value: "". +func NewXHeader() *XHeader { + return NewXHeaderFromV2(new(session.XHeader)) +} + +// ToV2 converts XHeader to v2 XHeader message. +// +// Nil XHeader converts to nil. +func (x *XHeader) ToV2() *session.XHeader { + return (*session.XHeader)(x) +} + +// Key returns key to X-Header. +func (x *XHeader) Key() string { + return (*session.XHeader)(x). + GetKey() +} + +// SetKey sets key to X-Header. +func (x *XHeader) SetKey(k string) { + (*session.XHeader)(x). + SetKey(k) +} + +// Value returns value of X-Header. +func (x *XHeader) Value() string { + return (*session.XHeader)(x). + GetValue() +} + +// SetValue sets value of X-Header. +func (x *XHeader) SetValue(k string) { + (*session.XHeader)(x). + SetValue(k) +} diff --git a/session/xheader_test.go b/session/xheader_test.go new file mode 100644 index 0000000..28fe7a3 --- /dev/null +++ b/session/xheader_test.go @@ -0,0 +1,58 @@ +package session + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/stretchr/testify/require" +) + +func TestXHeader(t *testing.T) { + x := NewXHeader() + + key := "some key" + val := "some value" + + x.SetKey(key) + x.SetValue(val) + + require.Equal(t, key, x.Key()) + require.Equal(t, val, x.Value()) + + xV2 := x.ToV2() + + require.Equal(t, key, xV2.GetKey()) + require.Equal(t, val, xV2.GetValue()) +} + +func TestNewXHeaderFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *session.XHeader + + require.Nil(t, NewXHeaderFromV2(x)) + }) +} + +func TestXHeader_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *XHeader + + require.Nil(t, x.ToV2()) + }) +} + +func TestNewXHeader(t *testing.T) { + t.Run("default values", func(t *testing.T) { + xh := NewXHeader() + + // check initial values + require.Empty(t, xh.Value()) + require.Empty(t, xh.Key()) + + // convert to v2 message + xhV2 := xh.ToV2() + + require.Empty(t, xhV2.GetValue()) + require.Empty(t, xhV2.GetKey()) + }) +}