From db53e2ea39ab6ab7c2a6d0a5980a018a40e0aa6b Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 18 Jun 2020 15:47:55 +0300 Subject: [PATCH] service: make RequestData to provide ExtendedHeadersSource interface --- service/meta.go | 45 +++++++++++++++++++++++++++++++++++++++++++ service/meta_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ service/sign_test.go | 6 ++++++ service/types.go | 1 + 4 files changed, 98 insertions(+) diff --git a/service/meta.go b/service/meta.go index e838ceca..557d0762 100644 --- a/service/meta.go +++ b/service/meta.go @@ -4,6 +4,10 @@ import ( "io" ) +type extHdrWrapper struct { + msg *RequestExtendedHeader_KV +} + type extHdrSrcWrapper struct { extHdrSrc ExtendedHeadersSource } @@ -84,3 +88,44 @@ func (m *RequestExtendedHeader_KV) SetV(v string) { func (m *RequestExtendedHeader) SetHeaders(v []RequestExtendedHeader_KV) { m.Headers = v } + +func wrapExtendedHeaderKV(msg *RequestExtendedHeader_KV) extHdrWrapper { + return extHdrWrapper{ + msg: msg, + } +} + +// Key returns the result of K field getter. +// +// If message is nil, empty string returns. +func (m extHdrWrapper) Key() string { + if m.msg != nil { + return m.msg.GetK() + } + + return "" +} + +// Value returns the result of V field getter. +// +// If message is nil, empty string returns. +func (m extHdrWrapper) Value() string { + if m.msg != nil { + return m.msg.GetV() + } + + return "" +} + +// ExtendedHeaders composes ExtendedHeader list from the Headers field getter result. +func (m RequestExtendedHeader) ExtendedHeaders() []ExtendedHeader { + hs := m.GetHeaders() + + res := make([]ExtendedHeader, 0, len(hs)) + + for i := range hs { + res = append(res, wrapExtendedHeaderKV(&hs[i])) + } + + return res +} diff --git a/service/meta_test.go b/service/meta_test.go index 7c3853af..9e9c0129 100644 --- a/service/meta_test.go +++ b/service/meta_test.go @@ -51,3 +51,49 @@ func TestRequestExtendedHeader_SetHeaders(t *testing.T) { require.Equal(t, hdrs, s.GetHeaders()) } + +func TestExtHdrWrapper(t *testing.T) { + s := wrapExtendedHeaderKV(nil) + require.Empty(t, s.Key()) + require.Empty(t, s.Value()) + + msg := new(RequestExtendedHeader_KV) + s = wrapExtendedHeaderKV(msg) + + key := "key" + msg.SetK(key) + require.Equal(t, key, s.Key()) + + val := "val" + msg.SetV(val) + require.Equal(t, val, s.Value()) +} + +func TestRequestExtendedHeader_ExtendedHeaders(t *testing.T) { + var ( + k1, v1 = "key1", "value1" + k2, v2 = "key2", "value2" + h1 = new(RequestExtendedHeader_KV) + h2 = new(RequestExtendedHeader_KV) + ) + + h1.SetK(k1) + h1.SetV(v1) + + h2.SetK(k2) + h2.SetV(v2) + + s := new(RequestExtendedHeader) + s.SetHeaders([]RequestExtendedHeader_KV{ + *h1, *h2, + }) + + xHdrs := s.ExtendedHeaders() + require.Len(t, xHdrs, 2) + + require.Equal(t, k1, xHdrs[0].Key()) + require.Equal(t, v1, xHdrs[0].Value()) + + require.Equal(t, k2, xHdrs[1].Key()) + require.Equal(t, v2, xHdrs[1].Value()) +} diff --git a/service/sign_test.go b/service/sign_test.go index 80c0d196..023412fa 100644 --- a/service/sign_test.go +++ b/service/sign_test.go @@ -20,6 +20,8 @@ type testSignedDataSrc struct { token SessionToken bearer BearerToken + + extHdrs []ExtendedHeader } type testSignedDataReader struct { @@ -60,6 +62,10 @@ func (s testSignedDataSrc) GetBearerToken() BearerToken { return s.bearer } +func (s testSignedDataSrc) ExtendedHeaders() []ExtendedHeader { + return s.extHdrs +} + func (s testSignedDataReader) SignedDataSize() int { return len(s.data) } diff --git a/service/types.go b/service/types.go index feba2e3e..75a5a0a8 100644 --- a/service/types.go +++ b/service/types.go @@ -255,6 +255,7 @@ type RequestData interface { SignedDataSource SessionTokenSource BearerTokenSource + ExtendedHeadersSource } // RequestSignedData is an interface of request information with signature write access.