-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_test.go
311 lines (249 loc) · 7.53 KB
/
utilities_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package apirouter
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/mrz1836/go-parameters"
)
// TestSnakeCase test our snake case method
func TestSnakeCase(t *testing.T) {
t.Parallel()
// Create the list of tests
var tests = []struct {
input string
expected string
}{
{"testCamelCase", "test_camel_case"},
{"TestCamelCase", "test_camel_case"},
{"TEstCamelCase", "test_camel_case"},
{"testCamelCASE", "test_camel_case"},
{"testCamel!CASE", "test_camel_case"},
{"testCamelAPI", "test_camel_api"},
{"testCamelIP", "test_camel_ip"},
{"testCamelURL", "test_camel_url"},
{"testCamelJSON", "test_camel_json"},
}
// Test all
for _, test := range tests {
if output := SnakeCase(test.input); output != test.expected {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expected, output)
}
}
}
// TestFindString test our find string method
func TestFindString(t *testing.T) {
t.Parallel()
// needle string, haystack []string
haystack := []string{"test", "stack"}
needle := "stack"
if index := FindString(needle, haystack); index == -1 {
t.Fatal("FindString does not work correctly!")
}
if index := FindString("wrong", haystack); index >= 0 {
t.Fatal("FindString does not work correctly!")
}
}
// TestGetParams test getting params
func TestGetParams(t *testing.T) {
t.Parallel()
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = req.WithContext(
context.WithValue(req.Context(), parameters.ParamsKeyName, parameters.ParseParams(req)),
)
params := GetParams(req)
if params == nil {
t.Fatal("params should not be nil")
}
if id, success := params.GetUint64Ok("id"); !success {
t.Fatal("failed to find the param: id", success, id, params)
} else if id == 0 {
t.Fatal("failed to find the param: id", success, id, params)
}
}
// TestGetParams_BadKey tests a bad key on the context storage
func TestGetParams_BadKey(t *testing.T) {
t.Parallel()
type badParamKey string
const BadParamKey badParamKey = "bad_key"
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = req.WithContext(context.WithValue(req.Context(), BadParamKey, parameters.ParseParams(req)))
params := GetParams(req)
if params != nil {
t.Fatal("params should be nil")
}
}
// TestPermitParams tests the permitting params
func TestPermitParams(t *testing.T) {
t.Parallel()
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234&private=data", strings.NewReader(""),
)
req = req.WithContext(context.WithValue(req.Context(), parameters.ParamsKeyName, parameters.ParseParams(req)))
params := GetParams(req)
if params == nil {
t.Fatal("params should not be nil")
}
PermitParams(params, []string{"this", "id"})
p, ok := params.GetStringOk("private")
if ok {
t.Fatal("parameter should not exit", p, ok)
} else if len(p) > 0 {
t.Fatal("parameter value should be empty")
}
}
// TestGetIPFromRequest test getting IP from req
func TestGetIPFromRequest(t *testing.T) {
t.Parallel()
// Fake storing the ip address
testIP := "127.0.0.1"
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetOnRequest(req, ipAddressKey, testIP)
ip, ok := GetIPFromRequest(req)
if !ok {
t.Fatal("failed to get ip address", ip, ok)
} else if ip != testIP {
t.Fatal("ip address was not what was returned", ip, ok)
}
}
// TestGetRequestID test getting request ID from req
func TestGetRequestID(t *testing.T) {
t.Parallel()
// Fake storing the request id
testFakeID := "ern8347t88e7zrhg8eh48e7hg8e"
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetOnRequest(req, requestIDKey, testFakeID)
id, ok := GetRequestID(req)
if !ok {
t.Fatal("failed to get request id", id, ok)
} else if id != testFakeID {
t.Fatal("request id was not what was returned", id, ok)
}
}
// TestGetClientIPAddress test getting client ip address
func TestGetClientIPAddress(t *testing.T) {
t.Parallel()
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet, "/test?this=that&id=1234",
strings.NewReader(""),
)
ip := GetClientIPAddress(req)
if ip != "" {
t.Fatal("expected ip to be localhost on the test, IP:", ip)
}
}
// TestSetAuthToken test setting the auth token
func TestSetAuthToken(t *testing.T) {
t.Parallel()
// Fake storing the token
testFakeToken := "ern8347t88e7zrhg8eh48e7hg8e" //nolint:gosec // this is a fake token
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetAuthToken(req, testFakeToken)
token, ok := GetAuthToken(req)
if !ok {
t.Fatal("failed to get auth token", token, ok)
} else if token != testFakeToken {
t.Fatal("token was not what was returned", token, ok)
}
}
// TestGetAuthToken test setting the auth token
func TestGetAuthToken(t *testing.T) {
t.Parallel()
// Test getting the token
testFakeToken := "ern8347t88e7zrhg8eh48e7hg8e" //nolint:gosec // this is a fake token
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetAuthToken(req, testFakeToken)
token, ok := GetAuthToken(req)
if !ok {
t.Fatal("failed to get auth token", token, ok)
} else if token != testFakeToken {
t.Fatal("token was not what was returned", token, ok)
}
}
// TestSetUserData test setting the auth token
func TestSetUserData(t *testing.T) {
t.Parallel()
type TestThis struct {
FieldName string
FieldTwo string
}
// Fake storing the ip address
testFakeUserData := new(TestThis)
testFakeUserData.FieldName = "this"
testFakeUserData.FieldTwo = "that"
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetCustomData(req, testFakeUserData)
data := GetCustomData(req)
newData := data.(*TestThis)
if newData.FieldTwo != testFakeUserData.FieldTwo {
t.Fatal("failed get the correct data", newData.FieldTwo, testFakeUserData.FieldTwo)
}
}
// TestGetUserData test setting the auth token
func TestGetUserData(t *testing.T) {
t.Parallel()
type TestThis struct {
FieldName string
FieldTwo string
}
// Fake storing the ip address
testFakeUserData := new(TestThis)
testFakeUserData.FieldName = "this"
testFakeUserData.FieldTwo = "that"
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
req = SetCustomData(req, testFakeUserData)
data := GetCustomData(req)
newData := data.(*TestThis)
if newData.FieldName != testFakeUserData.FieldName {
t.Fatal("failed get the correct data", newData.FieldName, testFakeUserData.FieldName)
}
}
// TestNoCache test using the NoCache on a request
func TestNoCache(t *testing.T) {
t.Parallel()
req, _ := http.NewRequestWithContext(
context.Background(), http.MethodGet,
"/test?this=that&id=1234", strings.NewReader(""),
)
w := httptest.NewRecorder()
for _, v := range etagHeaders {
req.Header.Set(v, "something")
}
NoCache(w, req)
for _, v := range etagHeaders {
if req.Header.Get(v) != "" {
t.Fatal("header should be removed", v)
}
}
for key := range noCacheHeaders {
if w.Header().Get(key) == "" {
t.Fatal("header should have been added", key)
}
}
}