-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrequest_fields_test.go
More file actions
60 lines (53 loc) · 1.82 KB
/
Copy pathrequest_fields_test.go
File metadata and controls
60 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"context"
"testing"
)
func TestRequestFields(t *testing.T) {
// we need a request body that will successfully unmarshal
acctRequest := NewAcctRequest(
SetAcctRequestMethod(AuthenMethodTacacsPlus),
SetAcctRequestPrivLvl(PrivLvlRoot),
SetAcctRequestType(AuthenTypeASCII),
SetAcctRequestService(AuthenServiceLogin),
SetAcctRequestPort("4"),
SetAcctRequestRemAddr("async"),
)
acctBody, err := acctRequest.MarshalBinary()
if err != nil {
t.Error("failed to marshal an AccountRequest, uh oh")
}
// helper to add multiple values to a context
withValues := func(ctx context.Context, kv map[ContextKey]string) context.Context {
for k, v := range kv {
ctx = context.WithValue(ctx, k, v)
}
return ctx
}
tests := []struct {
name string
request Request
expected map[string]string
ctxKeys []ContextKey
}{
{
name: "ensure ContextKeys are added to fields map",
request: Request{Header: *NewHeader(SetHeaderType(Accounting)), Body: acctBody, Context: withValues(context.Background(), map[ContextKey]string{ContextSessionID: "123", ContextReqID: "1", ContextConnRemoteAddr: "9.9.9.9"})},
expected: map[string]string{string(ContextSessionID): "123", string(ContextReqID): "1", string(ContextConnRemoteAddr): "9.9.9.9"},
ctxKeys: []ContextKey{ContextSessionID, ContextReqID, ContextConnRemoteAddr},
},
}
for _, test := range tests {
fields := test.request.Fields(test.ctxKeys...)
for expectedKey, expectedValue := range test.expected {
if v, ok := fields[expectedKey]; !ok || v != expectedValue {
t.Fatalf("request fields dont match, got %v, wanted %v", fields, test.expected)
}
}
}
}