-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsigner_test.go
228 lines (216 loc) · 5.52 KB
/
signer_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package edgegrid
import (
"encoding/base64"
"net/http"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
func TestConfig_createAuthHeader(t *testing.T) {
tests := map[string]struct {
config Config
request *http.Request
expected authHeader
withError error
}{
"method is GET": {
config: Config{
ClientToken: "12345",
AccessToken: "54321",
MaxBody: MaxBodySize,
},
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://akamai.com/test/path?query=test", nil)
require.NoError(t, err)
return req
}(),
expected: authHeader{
authType: authType,
clientToken: "12345",
accessToken: "54321",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := test.config.createAuthHeader(test.request)
assert.Equal(t, test.expected.authType, res.authType)
assert.Equal(t, test.expected.accessToken, res.accessToken)
assert.Equal(t, test.expected.clientToken, res.clientToken)
assert.NotEmpty(t, res.signature)
_, err := uuid.Parse(res.nonce)
assert.NoError(t, err)
_, err = base64.StdEncoding.DecodeString(res.signature)
require.NoError(t, err)
_, err = time.Parse("20060102T15:04:05-0700", res.timestamp)
assert.NoError(t, err)
})
}
}
func TestCanonicalizeHeaders(t *testing.T) {
tests := map[string]struct {
requestHeaders http.Header
headersToSign []string
expected string
}{
"found matching request headers": {
requestHeaders: map[string][]string{
"A": {"val1"},
"B": {" VAL 2 "},
"C": {"V A L 3"},
},
headersToSign: []string{"B", "C"},
expected: "b:val 2\tc:v a l 3",
},
"no matching headers found": {
requestHeaders: map[string][]string{
"A": {"val1"},
"B": {" VAL 2 "},
"C": {"V A L 3"},
},
headersToSign: []string{"D", "E"},
expected: "",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := canonicalizeHeaders(test.requestHeaders, test.headersToSign)
assert.Equal(t, test.expected, res)
})
}
}
func TestCreateContentHash(t *testing.T) {
tests := map[string]struct {
httpMethod string
body string
resultEmpty bool
}{
"PUT request": {
httpMethod: http.MethodPut,
body: `{"key":"value"}`,
resultEmpty: true,
},
"POST request, empty body": {
httpMethod: http.MethodPost,
body: "",
resultEmpty: true,
},
"POST request, body is not empty": {
httpMethod: http.MethodPost,
body: `{"key":"value"}`,
resultEmpty: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
req, err := http.NewRequest(test.httpMethod, "", strings.NewReader(test.body))
require.NoError(t, err)
res := createContentHash(req, MaxBodySize)
if test.resultEmpty {
assert.Empty(t, res)
return
}
require.NotEmpty(t, res)
_, err = base64.StdEncoding.DecodeString(res)
assert.NoError(t, err)
})
}
}
func TestAuthHeader_String(t *testing.T) {
tests := map[string]struct {
given authHeader
expected string
}{
"signature is empty": {
given: authHeader{
authType: "A",
clientToken: "B",
accessToken: "C",
timestamp: "D",
nonce: "E",
},
expected: "A client_token=B;access_token=C;timestamp=D;nonce=E;",
},
"signature is not empty": {
given: authHeader{
authType: "A",
clientToken: "B",
accessToken: "C",
timestamp: "D",
nonce: "E",
signature: "F",
},
expected: "A client_token=B;access_token=C;timestamp=D;nonce=E;signature=F",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := test.given.String()
assert.Equal(t, test.expected, res)
})
}
}
func TestAddAccountSwitchKey(t *testing.T) {
tests := map[string]struct {
config Config
request *http.Request
expected string
withError error
}{
"test account switch single param GET": {
config: Config{
ClientToken: "12345",
AccessToken: "54321",
AccountKey: "test_switch",
MaxBody: MaxBodySize,
},
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://akamai.com/test/path?query=test", nil)
require.NoError(t, err)
return req
}(),
expected: "accountSwitchKey=test_switch&query=test",
},
"test account switch multiple param GET": {
config: Config{
ClientToken: "12345",
AccessToken: "54321",
AccountKey: "test_switch",
MaxBody: MaxBodySize,
},
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://akamai.com/test/path?query1=test1&query2=test2", nil)
require.NoError(t, err)
return req
}(),
expected: "accountSwitchKey=test_switch&query1=test1&query2=test2",
},
"test account switch empty GET": {
config: Config{
ClientToken: "12345",
AccessToken: "54321",
AccountKey: "test_switch",
MaxBody: MaxBodySize,
},
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://akamai.com/test/path", nil)
require.NoError(t, err)
return req
}(),
expected: "accountSwitchKey=test_switch",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := test.config.addAccountSwitchKey(test.request)
assert.NotNil(t, test.request.URL.Host)
assert.NotEmpty(t, res)
assert.Equal(t, test.expected, res)
assert.Equal(t, test.request.URL.Host, "akamai.com")
assert.Equal(t, test.request.URL.Path, "/test/path")
})
}
}