service: make RequestData to provide ExtendedHeadersSource interface

This commit is contained in:
Leonard Lyubich 2020-06-18 15:47:55 +03:00
parent 2bef390cc6
commit db53e2ea39
4 changed files with 98 additions and 0 deletions

View file

@ -4,6 +4,10 @@ import (
"io" "io"
) )
type extHdrWrapper struct {
msg *RequestExtendedHeader_KV
}
type extHdrSrcWrapper struct { type extHdrSrcWrapper struct {
extHdrSrc ExtendedHeadersSource extHdrSrc ExtendedHeadersSource
} }
@ -84,3 +88,44 @@ func (m *RequestExtendedHeader_KV) SetV(v string) {
func (m *RequestExtendedHeader) SetHeaders(v []RequestExtendedHeader_KV) { func (m *RequestExtendedHeader) SetHeaders(v []RequestExtendedHeader_KV) {
m.Headers = v 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
}

View file

@ -51,3 +51,49 @@ func TestRequestExtendedHeader_SetHeaders(t *testing.T) {
require.Equal(t, hdrs, s.GetHeaders()) 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())
}

View file

@ -20,6 +20,8 @@ type testSignedDataSrc struct {
token SessionToken token SessionToken
bearer BearerToken bearer BearerToken
extHdrs []ExtendedHeader
} }
type testSignedDataReader struct { type testSignedDataReader struct {
@ -60,6 +62,10 @@ func (s testSignedDataSrc) GetBearerToken() BearerToken {
return s.bearer return s.bearer
} }
func (s testSignedDataSrc) ExtendedHeaders() []ExtendedHeader {
return s.extHdrs
}
func (s testSignedDataReader) SignedDataSize() int { func (s testSignedDataReader) SignedDataSize() int {
return len(s.data) return len(s.data)
} }

View file

@ -255,6 +255,7 @@ type RequestData interface {
SignedDataSource SignedDataSource
SessionTokenSource SessionTokenSource
BearerTokenSource BearerTokenSource
ExtendedHeadersSource
} }
// RequestSignedData is an interface of request information with signature write access. // RequestSignedData is an interface of request information with signature write access.