-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
htransformation_test.go
194 lines (177 loc) · 3.81 KB
/
htransformation_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
package htransformation_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
plug "github.com/tomMoulard/htransformation"
"github.com/tomMoulard/htransformation/pkg/tests/assert"
"github.com/tomMoulard/htransformation/pkg/tests/require"
"github.com/tomMoulard/htransformation/pkg/types"
)
func TestValidation(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
config *plug.Config
wantErr bool
}{
{
name: "no rules",
config: &plug.Config{},
wantErr: false,
},
{
name: "no rules type",
config: &plug.Config{
Rules: []types.Rule{
{
Name: "no rule",
},
},
},
wantErr: true,
},
{
name: "invalid rules type",
config: &plug.Config{
Rules: []types.Rule{
{
Name: "invalid rule",
Type: "THIS IS NOT A VALID RULE TYPE",
},
},
},
wantErr: true,
},
{
name: "missing type",
config: &plug.Config{
Rules: []types.Rule{
{
Name: "rule with no type",
Header: "not-empty",
},
},
},
wantErr: true,
},
{
name: "valid rule",
config: &plug.Config{
Rules: []types.Rule{
{
Name: "delete rule",
Header: "not-empty",
Type: types.Delete,
},
{
Name: "join Rule",
Header: "not-empty",
Values: []string{"not-empty"},
Sep: "not-empty",
Type: types.Join,
},
{
Name: "rewrite rule",
Header: "not-empty",
ValueReplace: "not-empty",
Value: "not-empty",
Type: types.RewriteValueRule,
},
{
Name: "set rule",
Header: "not-empty",
Value: "not-empty",
Type: types.Set,
},
},
},
wantErr: false,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
_, err := plug.New(context.Background(), nil, test.config, "test")
if test.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestHeaderRules(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
rule types.Rule
wantErr bool
additionalHeader map[string]string
}{
{
name: "set rule",
rule: types.Rule{
Name: "set rule",
Header: "not-empty",
Value: "not-empty",
Type: types.Set,
},
wantErr: false,
},
{
name: "rename rule",
rule: types.Rule{
Name: "rename rule",
Header: "not-empty",
Value: "not-empty",
Type: types.Rename,
},
additionalHeader: map[string]string{
"Referer": "http://foo.bar",
},
wantErr: false,
},
{
name: "rewrite rule",
rule: types.Rule{
Name: "rewrite rule",
Header: "not-empty",
Value: "not-empty",
ValueReplace: "not-empty",
Type: types.RewriteValueRule,
},
additionalHeader: map[string]string{
"Referer": "http://foo.bar",
},
wantErr: false,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cfg := plug.CreateConfig()
cfg.Rules = []types.Rule{test.rule}
ctx := context.Background()
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
handler, err := plug.New(ctx, next, cfg, "demo-plugin")
require.NoError(t, err)
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
require.NoError(t, err)
for key, value := range test.additionalHeader {
req.Header.Set(key, value)
}
handler.ServeHTTP(recorder, req)
result := recorder.Result()
statusCode := result.StatusCode
require.NoError(t, result.Body.Close())
if test.wantErr {
assert.Equal(t, http.StatusInternalServerError, statusCode)
} else {
assert.Equal(t, http.StatusOK, statusCode)
}
})
}
}