-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathquerymodification_test.go
More file actions
264 lines (214 loc) · 7.3 KB
/
Copy pathquerymodification_test.go
File metadata and controls
264 lines (214 loc) · 7.3 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
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
package traefik_plugin_query_modification_test
import (
"context"
traefik_plugin_query_modification "github.com/kingjan1999/traefik-plugin-query-modification"
"net/http"
"net/http/httptest"
"testing"
)
// region Test Add
func TestAddQueryParam_NoPrevious(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "add"
cfg.ParamName = "newparam"
cfg.NewValue = "newvalue"
expected := "newparam=newvalue"
assertQueryModification(t, cfg, "", expected)
}
func TestAddQueryParam_OtherPrevious(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "add"
cfg.ParamName = "newparam"
cfg.NewValue = "newvalue"
expected := "a=b&newparam=newvalue"
previous := "a=b"
assertQueryModification(t, cfg, previous, expected)
}
func TestAddQueryParam_AddPrevious(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "add"
cfg.ParamName = "newparam"
cfg.NewValue = "newvalue"
expected := "newparam=oldvalue&newparam=newvalue"
previous := "newparam=oldvalue"
assertQueryModification(t, cfg, previous, expected)
}
func TestAddQueryParam_Previous(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "add"
cfg.ParamName = "newparam"
cfg.NewValue = "newvalue"
expected := "a=b&newparam=newvalue"
previous := "a=b"
assertQueryModification(t, cfg, previous, expected)
}
// endregion
//region Delete
func TestDeleteQueryParam(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
cfg.ParamName = "paramtodelete"
expected := ""
previous := "paramtodelete=anything"
assertQueryModification(t, cfg, previous, expected)
}
func TestDeleteQueryParam_Multiple(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
cfg.ParamName = "paramtodelete"
expected := ""
previous := "paramtodelete=anything¶mtodelete=somethingelse"
assertQueryModification(t, cfg, previous, expected)
}
func TestDeleteQueryParam_NotFound(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
cfg.ParamName = "paramtodelete"
expected := "some=thing"
previous := "some=thing"
assertQueryModification(t, cfg, previous, expected)
}
func TestDeleteQueryParam_Others(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
cfg.ParamName = "paramtodelete"
expected := "otherparam=stillhere"
previous := "otherparam=stillhere¶mtodelete=away"
assertQueryModification(t, cfg, previous, expected)
}
func TestDeleteQueryParam_Readme(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
cfg.ParamValueRegex = "password"
expected := "tracker=1234"
previous := "secret=password&othersecret=other-password&tracker=1234"
assertQueryModification(t, cfg, previous, expected)
}
//endregion
// region Modify
func TestModifyQueryParam_Simple(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamName = "a"
cfg.NewValue = "c"
previous := "a=b"
expected := "a=c"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_NotFound(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamName = "a"
cfg.NewValue = "c"
previous := "d=b"
expected := "d=b"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_SimpleMultiple(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamName = "a"
cfg.NewValue = "c"
previous := "a=b&a=d"
expected := "a=c&a=c"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_SimpleReplace(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamName = "a"
cfg.NewValue = "c$1"
previous := "a=b"
expected := "a=cb"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_SimpleReplaceMultiple(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamName = "a"
cfg.NewValue = "c$1"
previous := "a=b&a=d"
expected := "a=cb&a=cd"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_RegexKey(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamNameRegex = "^[abc]$"
cfg.NewValue = "d"
previous := "a=b&c=e&e=f"
expected := "a=d&c=d&e=f"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_RegexValue(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamValueRegex = "^secretpassword$"
cfg.NewValue = "censored"
previous := "a=secretpassword&b=secretpassword&c=somethingelse"
expected := "a=censored&b=censored&c=somethingelse"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_RegexValueRegexReplace(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamValueRegex = "^.*(p..sword)$"
cfg.NewValueRegex = "no-$1"
previous := "a=secretpassword&b=secretpassword&c=somethingelse"
expected := "a=no-password&b=no-password&c=somethingelse"
assertQueryModification(t, cfg, previous, expected)
}
func TestModifyQueryParam_RegexValueMultiple(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "modify"
cfg.ParamValueRegex = "^secretpassword$"
cfg.NewValue = "censored"
previous := "a=secretpassword&a=somethingelse"
expected := "a=censored&a=somethingelse"
assertQueryModification(t, cfg, previous, expected)
}
// endregion
func TestErrorInvalidType(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "bla"
cfg.ParamName = "blub"
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
_, err := traefik_plugin_query_modification.New(ctx, next, cfg, "query-modification-plugin")
if err == nil {
t.Error("expected error but err is nil")
}
}
func TestErrorNoParam(t *testing.T) {
cfg := traefik_plugin_query_modification.CreateConfig()
cfg.Type = "delete"
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
_, err := traefik_plugin_query_modification.New(ctx, next, cfg, "query-modification-plugin")
if err == nil {
t.Error("expected error but err is nil")
}
}
func createReqAndRecorder(cfg *traefik_plugin_query_modification.Config) (http.Handler, error, *httptest.ResponseRecorder, *http.Request) {
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := traefik_plugin_query_modification.New(ctx, next, cfg, "query-modification-plugin")
if err != nil {
return nil, err, nil, nil
}
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
return handler, err, recorder, req
}
func assertQueryModification(t *testing.T, cfg *traefik_plugin_query_modification.Config, previous, expected string) {
handler, err, recorder, req := createReqAndRecorder(cfg)
if err != nil {
t.Fatal(err)
return
}
req.URL.RawQuery = previous
handler.ServeHTTP(recorder, req)
if req.URL.Query().Encode() != expected {
t.Errorf("Expected %s, got %s", expected, req.URL.Query().Encode())
}
}