forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_context_vars_test.go
242 lines (222 loc) · 6.67 KB
/
mw_context_vars_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
package main
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/test"
)
func testPrepareContextVarsMiddleware() {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.EnableContextVars = true
spec.VersionData.Versions = map[string]apidef.VersionInfo{
"v1": {
UseExtendedPaths: true,
GlobalHeaders: map[string]string{
"X-Static": "foo",
"X-Request-ID": "$tyk_context.request_id",
"X-Path": "$tyk_context.path",
"X-Remote-Addr": "$tyk_context.remote_addr",
},
},
}
})
}
func TestContextVarsMiddleware(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
testPrepareContextVarsMiddleware()
ts.Run(t, []test.TestCase{
{Path: "/test/path", Code: 200, BodyMatch: `"X-Remote-Addr":"127.0.0.1"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Path":"/test/path"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Static":"foo"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Request-Id":"`},
}...)
}
func BenchmarkContextVarsMiddleware(b *testing.B) {
b.ReportAllocs()
ts := newTykTestServer()
defer ts.Close()
testPrepareContextVarsMiddleware()
for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/test/path", Code: 200, BodyMatch: `"X-Remote-Addr":"127.0.0.1"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Path":"/test/path"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Static":"foo"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Request-Id":"`},
}...)
}
}
type testContextVarsData struct {
Method string
URL string
Data string
ExpectedCtxDataObject map[string]interface{}
Header http.Header
}
func testPrepareTestContextVarsMiddleware() map[string]testContextVarsData {
return map[string]testContextVarsData{
"GET with query string": {
Method: http.MethodGet,
URL: "http://abc.com/aaa/bbb/111/222?x=123&y=test",
Header: http.Header{
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
},
ExpectedCtxDataObject: map[string]interface{}{
"remote_addr": "192.0.2.1",
"request_data": url.Values{
"x": {"123"},
"y": {"test"},
},
"headers": map[string][]string{
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
},
"headers_x_header_a": "A",
"headers_x_header_b": "B",
"headers_x_header_c": "C",
"headers_Host": "abc.com",
"path_parts": []string{"", "aaa", "bbb", "111", "222"},
"path": "/aaa/bbb/111/222",
},
},
"POST with query string and encoded form data": {
Method: http.MethodPost,
URL: "http://abc.com/aaa/bbb/111/222?x=123&y=test",
Data: "i=1&j=2&str=abc",
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
},
ExpectedCtxDataObject: map[string]interface{}{
"remote_addr": "192.0.2.1",
"request_data": url.Values{
"x": {"123"},
"y": {"test"},
"i": {"1"},
"j": {"2"},
"str": {"abc"},
},
"headers": map[string][]string{
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
"headers_x_header_a": "A",
"headers_x_header_b": "B",
"headers_x_header_c": "C",
"headers_Content_Type": "application/x-www-form-urlencoded",
"headers_Host": "abc.com",
"path_parts": []string{"", "aaa", "bbb", "111", "222"},
"path": "/aaa/bbb/111/222",
},
},
"POST with query string and encoded form data and cookies": {
Method: http.MethodPost,
URL: "http://abc.com/aaa/bbb/111/222?x=123&y=test",
Data: "i=1&j=2&str=abc",
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
"Cookie": {"c-1=cookie1;c-2=cookie2"},
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
},
ExpectedCtxDataObject: map[string]interface{}{
"remote_addr": "192.0.2.1",
"request_data": url.Values{
"x": {"123"},
"y": {"test"},
"i": {"1"},
"j": {"2"},
"str": {"abc"},
},
"headers": map[string][]string{
"x-header-a": {"A"},
"x-header-b": {"B"},
"x-header-c": {"C"},
"Content-Type": {"application/x-www-form-urlencoded"},
"Cookie": {"c-1=cookie1;c-2=cookie2"},
},
"headers_x_header_a": "A",
"headers_x_header_b": "B",
"headers_x_header_c": "C",
"headers_Content_Type": "application/x-www-form-urlencoded",
"headers_Cookie": "c-1=cookie1;c-2=cookie2",
"headers_Host": "abc.com",
"cookies_c_1": "cookie1",
"cookies_c_2": "cookie2",
"path_parts": []string{"", "aaa", "bbb", "111", "222"},
"path": "/aaa/bbb/111/222",
},
},
}
}
func TestContextVarsMiddlewareProcessRequest(t *testing.T) {
mw := &MiddlewareContextVars{}
tests := testPrepareTestContextVarsMiddleware()
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var bodyReader io.Reader
if test.Data != "" {
bodyReader = strings.NewReader(test.Data)
}
req := httptest.NewRequest(test.Method, test.URL, bodyReader)
req.Header = test.Header
err, code := mw.ProcessRequest(nil, req, nil)
if err != nil {
t.Error(err)
}
if code != http.StatusOK {
t.Errorf("Wrong response code: %d Eexpected 200.", code)
}
ctxDataObject := ctxGetData(req)
// check request_id
if _, ok := ctxDataObject["request_id"].(string); !ok {
t.Error("Missing 'request_id' field")
}
// delete request_if to do DeepEqual
delete(ctxDataObject, "request_id")
if !reflect.DeepEqual(ctxDataObject, test.ExpectedCtxDataObject) {
t.Errorf("Expected: %v\n Got: %v\n", test.ExpectedCtxDataObject, ctxDataObject)
}
})
}
}
func BenchmarkContextVarsMiddlewareProcessRequest(b *testing.B) {
mw := &MiddlewareContextVars{}
tests := testPrepareTestContextVarsMiddleware()
var err error
var code int
for name, test := range tests {
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var bodyReader io.Reader
if test.Data != "" {
bodyReader = strings.NewReader(test.Data)
}
req := httptest.NewRequest(test.Method, test.URL, bodyReader)
req.Header = test.Header
err, code = mw.ProcessRequest(nil, req, nil)
if err != nil {
b.Error(err)
}
if code != http.StatusOK {
b.Errorf("Wrong response code: %d Eexpected 200.", code)
}
}
})
}
}