-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlooping_test.go
184 lines (153 loc) · 5.62 KB
/
looping_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
// +build !race
// Looping by itself has race nature
package main
import (
"encoding/json"
"sync"
"testing"
"github.com/TykTechnologies/tyk/test"
"github.com/TykTechnologies/tyk/user"
)
func TestLooping(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
postAction := `<operation action="https://example.com/post_action">data</operation>`
getAction := `<operation action="https://example.com/get_action">data</operation>`
t.Run("Using advanced URL rewrite", func(t *testing.T) {
// We defined internnal advanced rewrite based on body data
// which rewrites to internal paths (marked as blacklist so they protected from outside world)
buildAndLoadAPI(func(spec *APISpec) {
version := spec.VersionData.Versions["v1"]
json.Unmarshal([]byte(`{
"use_extended_paths": true,
"extended_paths": {
"url_rewrites": [{
"path": "/xml",
"method": "POST",
"match_pattern": "/xml(.*)",
"rewrite_to": "/xml$1",
"triggers": [
{
"on": "all",
"options": {
"payload_matches": {
"match_rx": "post_action"
}
},
"rewrite_to": "tyk://self/post_action"
},
{
"on": "all",
"options": {
"payload_matches": {
"match_rx": "get_action"
}
},
"rewrite_to": "tyk://self/get_action?method=GET"
}
]
}]
}
}`), &version)
spec.VersionData.Versions["v1"] = version
spec.Proxy.ListenPath = "/"
})
ts.Run(t, []test.TestCase{
{Method: "POST", Path: "/xml", Data: postAction, BodyMatch: `"Url":"/post_action`},
// Should retain original query params
{Method: "POST", Path: "/xml?a=b", Data: getAction, BodyMatch: `"Url":"/get_action`},
// Should rewrite http method, if loop rewrite param passed
{Method: "POST", Path: "/xml", Data: getAction, BodyMatch: `"Method":"GET"`},
}...)
})
t.Run("VirtualEndpoint or plugins", func(t *testing.T) {
testPrepareVirtualEndpoint(`
function testVirtData(request, session, config) {
var loopLocation = "/default"
if (request.Body.match("post_action")) {
loopLocation = "tyk://self/post_action"
} else if (request.Body.match("get_action")) {
loopLocation = "tyk://self/get_action?method=GET"
}
var resp = {
Headers: {
"Location": loopLocation,
},
Code: 302
}
return TykJsResponse(resp, session.meta_data)
}
`, "POST", "/virt", true)
ts.Run(t, []test.TestCase{
{Method: "POST", Path: "/virt", Data: postAction, BodyMatch: `"Url":"/post_action`},
// Should retain original query params
{Method: "POST", Path: "/virt?a=b", Data: getAction, BodyMatch: `"Url":"/get_action`},
// Should rewrite http method, if loop rewrite param passed
{Method: "POST", Path: "/virt", Data: getAction, BodyMatch: `"Method":"GET"`},
}...)
})
t.Run("Loop limit", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
version := spec.VersionData.Versions["v1"]
json.Unmarshal([]byte(`{
"use_extended_paths": true,
"extended_paths": {
"url_rewrites": [{
"path": "/recursion",
"match_pattern": "/recursion(.*)",
"method": "GET",
"rewrite_to": "tyk://self/recursion?loop_limit=2"
}]
}
}`), &version)
spec.VersionData.Versions["v1"] = version
spec.Proxy.ListenPath = "/"
})
ts.Run(t, []test.TestCase{
{Method: "GET", Path: "/recursion", Code: 500, BodyMatch: "Loop level too deep. Found more than 2 loops in single request"},
}...)
})
t.Run("Quota and rate limit calculation", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
version := spec.VersionData.Versions["v1"]
json.Unmarshal([]byte(`{
"use_extended_paths": true,
"extended_paths": {
"url_rewrites": [{
"path": "/recursion",
"match_pattern": "/recursion(.*)",
"method": "GET",
"rewrite_to": "tyk://self/recursion?loop_limit=2"
}]
}
}`), &version)
spec.VersionData.Versions["v1"] = version
spec.Proxy.ListenPath = "/"
spec.UseKeylessAccess = false
})
keyID := createSession(func(s *user.SessionState) {
s.QuotaMax = 2
})
authHeaders := map[string]string{"authorization": keyID}
ts.Run(t, []test.TestCase{
{Method: "GET", Path: "/recursion", Headers: authHeaders, BodyNotMatch: "Quota exceeded"},
}...)
})
}
func TestConcurrencyReloads(t *testing.T) {
var wg sync.WaitGroup
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
ts.Run(t, test.TestCase{Path: "/sample", Code: 200})
wg.Done()
}()
}
for j := 0; j < 5; j++ {
buildAndLoadAPI()
}
wg.Wait()
}