forked from anexia-it/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_test.go
235 lines (199 loc) · 6.11 KB
/
operation_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
package oas
import (
"context"
"strings"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"github.com/TykTechnologies/tyk/apidef"
)
func minimumValidOAS() OAS {
return OAS{
T: openapi3.T{
Info: &openapi3.Info{
Title: "title",
Version: "version",
},
OpenAPI: DefaultOpenAPI,
},
}
}
func TestOAS_PathsAndOperations(t *testing.T) {
t.Parallel()
const operationId = "userGET"
const existingOperationId = "userPOST"
var oas OAS
oas.Paths = openapi3.Paths{
"/user": {
Get: &openapi3.Operation{
OperationID: operationId,
},
},
}
var operation Operation
Fill(t, &operation, 0)
operation.ValidateRequest = nil // This one also fills native part, let's skip it for this test.
operation.MockResponse = nil // This one also fills native part, let's skip it for this test.
operation.TransformRequestBody.Path = "" // if `path` and `body` are present, `body` would take precedence, detailed tests can be found in middleware_test.go
operation.VirtualEndpoint.Path = "" // if `path` and `body` are present, `body` would take precedence, detailed tests can be found in middleware_test.go
operation.PostPlugins = operation.PostPlugins[:1] // only 1 post plugin is considered at this point, ignore others.
xTykAPIGateway := &XTykAPIGateway{
Middleware: &Middleware{
Operations: Operations{
operationId: &operation,
},
},
}
oas.SetTykExtension(xTykAPIGateway)
var ep apidef.ExtendedPathsSet
oas.extractPathsAndOperations(&ep)
convertedOAS := minimumValidOAS()
convertedOAS.Paths = openapi3.Paths{
"/user": {
Post: &openapi3.Operation{
OperationID: existingOperationId,
Responses: openapi3.NewResponses(),
},
},
}
convertedOAS.SetTykExtension(&XTykAPIGateway{Middleware: &Middleware{Operations: Operations{}}})
convertedOAS.fillPathsAndOperations(ep)
assert.Equal(t, oas.getTykOperations(), convertedOAS.getTykOperations())
expCombinedPaths := openapi3.Paths{
"/user": {
Post: &openapi3.Operation{
OperationID: existingOperationId,
Responses: openapi3.NewResponses(),
},
Get: &openapi3.Operation{
OperationID: operationId,
Responses: openapi3.NewResponses(),
},
},
}
assert.Equal(t, expCombinedPaths, convertedOAS.Paths)
t.Run("oas validation", func(t *testing.T) {
err := convertedOAS.Validate(context.Background())
assert.NoError(t, err)
})
}
func TestOAS_PathsAndOperationsRegex(t *testing.T) {
t.Parallel()
expectedOperationID := "users/[a-z]+/[0-9]+$GET"
expectedPath := "/users/{customRegex1}/{customRegex2}"
var oas OAS
oas.Paths = openapi3.Paths{}
_ = oas.getOperationID("/users/[a-z]+/[0-9]+$", "GET")
expectedPathItems := openapi3.Paths{
expectedPath: &openapi3.PathItem{
Get: &openapi3.Operation{
OperationID: expectedOperationID,
Responses: openapi3.NewResponses(),
},
Parameters: []*openapi3.ParameterRef{
{
Value: &openapi3.Parameter{
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Type: "string",
Pattern: "[a-z]+",
},
},
Name: "customRegex1",
In: "path",
Required: true,
},
},
{
Value: &openapi3.Parameter{
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Type: "string",
Pattern: "[0-9]+$",
},
},
Name: "customRegex2",
In: "path",
Required: true,
},
},
},
},
}
assert.Equal(t, expectedPathItems, oas.Paths, "expected path item differs")
}
func TestOAS_RegexOperationIDs(t *testing.T) {
t.Parallel()
type test struct {
input string
method string
want string
}
tests := []test{
{"/.+", "GET", ".+GET"},
{"/.*", "GET", ".*GET"},
{"/[^a]*", "GET", "[^a]*GET"},
{"/foo$", "GET", "foo$GET"},
{"/group/.+", "GET", "group/.+GET"},
{"/group/.*", "GET", "group/.*GET"},
{"/group/[^a]*", "GET", "group/[^a]*GET"},
{"/group/foo$", "GET", "group/foo$GET"},
{"/group/[^a]*/.*", "GET", "group/[^a]*/.*GET"},
}
for i, tc := range tests {
var oas OAS
oas.Paths = openapi3.Paths{
tc.input: {
Get: &openapi3.Operation{},
},
}
got := oas.getOperationID(tc.input, tc.method)
assert.Equalf(t, tc.want, got, "test %d: expected operationID %v, got %v", i, tc.want, got)
}
}
func TestOAS_RegexPaths(t *testing.T) {
t.Parallel()
type test struct {
input string
want string
params int
}
tests := []test{
{"/v1.Service", "/v1.Service", 0},
{"/v1.Service/stats.Service", "/v1.Service/stats.Service", 0},
{"/.+", "/{customRegex1}", 1},
{"/.*", "/{customRegex1}", 1},
{"/[^a]*", "/{customRegex1}", 1},
{"/foo$", "/{customRegex1}", 1},
{"/group/.+", "/group/{customRegex1}", 1},
{"/group/.*", "/group/{customRegex1}", 1},
{"/group/[^a]*", "/group/{customRegex1}", 1},
{"/group/foo$", "/group/{customRegex1}", 1},
{"/group/[^a]*/.*", "/group/{customRegex1}/{customRegex2}", 2},
}
for i, tc := range tests {
var oas OAS
oas.Paths = openapi3.Paths{}
_ = oas.getOperationID(tc.input, "GET")
pathKeys := make([]string, 0, len(oas.Paths))
for k := range oas.Paths {
pathKeys = append(pathKeys, k)
}
assert.Lenf(t, oas.Paths, 1, "Expected one path key being created, got %#v", pathKeys)
_, ok := oas.Paths[tc.want]
assert.True(t, ok)
p, ok := oas.Paths[tc.want]
assert.Truef(t, ok, "test %d: path doesn't exist in OAS: %v", i, tc.want)
assert.Lenf(t, p.Parameters, tc.params, "test %d: expected %d parameters, got %d", i, tc.params, len(p.Parameters))
// rebuild original link
got := tc.want
for _, param := range p.Parameters {
assert.NotNilf(t, param.Value, "test %d: missing value", i)
assert.NotNilf(t, param.Value.Schema, "test %d: missing schema", i)
assert.NotNilf(t, param.Value.Schema.Value, "test %d: missing schema value", i)
assert.Truef(t, strings.HasPrefix(param.Value.Name, "customRegex"), "test %d: invalid name %v", i, param.Value.Name)
got = strings.ReplaceAll(got, "{"+param.Value.Name+"}", param.Value.Schema.Value.Pattern)
}
assert.Equalf(t, tc.input, got, "test %d: rebuilt link, expected %v, got %v", i, tc.input, got)
}
}