This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ramlapi_test.go
260 lines (237 loc) · 5.24 KB
/
ramlapi_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package ramlapi_test
import (
"fmt"
"testing"
. "github.com/EconomistDigitalSolutions/ramlapi"
"github.com/buddhamagnet/raml"
)
var (
endpoints []*Endpoint
patt1 string
patt2 string
)
func init() {
patt1 = "[a-z]"
patt2 = "[0-9]"
}
func testFunc(ep *Endpoint) {
endpoints = append(endpoints, ep)
}
// TestData used to test raml.APIDefinition to ramlapi.Endpoints. The order
// of expected methods is important (GET, POST, PUT, PATCH, HEAD, DELETE).
var TestData = []struct {
api *raml.APIDefinition
expected []map[string]interface{}
}{
{
// test simple endpoint
&raml.APIDefinition{
Resources: map[string]raml.Resource{
"/test": raml.Resource{
Post: &raml.Method{
Name: "POST",
DisplayName: "Post me",
},
Get: &raml.Method{
Name: "GET",
DisplayName: "Get me",
},
},
},
},
[]map[string]interface{}{
{"verb": "GET", "handler": "GetMe", "path": "/test"},
{"verb": "POST", "handler": "PostMe", "path": "/test"},
},
},
{
// test URI parameters with nested resource
&raml.APIDefinition{
Resources: map[string]raml.Resource{
"/{foo}": raml.Resource{
Get: &raml.Method{
Name: "GET",
DisplayName: "Get me",
},
UriParameters: map[string]raml.NamedParameter{
"foo": raml.NamedParameter{
Pattern: &patt1,
},
},
Nested: map[string]*raml.Resource{
"/{bar}": &raml.Resource{
Get: &raml.Method{
Name: "GET",
DisplayName: "Nested get",
},
UriParameters: map[string]raml.NamedParameter{
"bar": raml.NamedParameter{
Pattern: &patt2,
},
},
},
},
},
},
},
[]map[string]interface{}{
{
"verb": "GET",
"handler": "GetMe",
"path": "/{foo}",
"uri_params": []map[string]string{
{
"key": "foo",
"pattern": "[a-z]",
},
},
},
{
"verb": "GET",
"handler": "NestedGet",
"path": "/{foo}/{bar}",
"uri_params": []map[string]string{
{
"key": "foo",
"pattern": "[a-z]",
},
{
"key": "bar",
"pattern": "[0-9]",
},
},
},
},
},
{
// test query parameters
&raml.APIDefinition{
Resources: map[string]raml.Resource{
"/query": raml.Resource{
Get: &raml.Method{
Name: "GET",
DisplayName: "Get me",
QueryParameters: map[string]raml.NamedParameter{
"foo": raml.NamedParameter{
Pattern: &patt1,
Required: true,
},
"bar": raml.NamedParameter{
Pattern: &patt2,
Required: false,
},
},
},
},
},
},
[]map[string]interface{}{
{
"verb": "GET",
"handler": "GetMe",
"path": "/query",
"query_params": []map[string]string{
{
"key": "foo",
"pattern": "[a-z]",
"required": "true",
},
{
"key": "bar",
"pattern": "[0-9]",
"required": "false",
},
},
},
},
},
}
func TestProcess(t *testing.T) {
_, err := Process("fixtures/valid.raml")
if err != nil {
t.Error("could not process valid RAML file")
}
}
func TestEndpoints(t *testing.T) {
for _, data := range TestData {
Build(data.api, testFunc)
if !checkEndpoints(t, data.expected, endpoints) {
t.Errorf("expected endpoint with: %s", data.expected)
}
endpoints = make([]*Endpoint, 0)
}
}
func checkEndpoints(t *testing.T, exp []map[string]interface{}, got []*Endpoint) bool {
var foundHandler, foundPath, foundVerb bool
var found int
if len(exp) != len(got) {
t.Errorf("expected %d endpoints, got %d", len(exp), len(got))
}
for _, e := range exp {
for _, ep := range got {
foundHandler = true
if ep.Handler != e["handler"] {
foundHandler = false
}
foundPath = true
if ep.Path != e["path"] {
foundPath = false
}
foundVerb = true
if ep.Verb != e["verb"] {
foundVerb = false
}
if foundHandler && foundPath && foundVerb {
found += 1
if uParams, ok := e["uri_params"]; ok {
u := uParams.([]map[string]string)
if !checkParameters(t, u, ep.URIParameters) {
t.Errorf("expected uri parameters: %s", u)
}
}
if qParams, ok := e["query_params"]; ok {
q := qParams.([]map[string]string)
if !checkParameters(t, q, ep.QueryParameters) {
t.Errorf("expected query parameters: %s", q)
}
}
}
}
}
return found == len(exp)
}
func checkParameters(t *testing.T, exp []map[string]string, got []*Parameter) bool {
var foundKey, foundPatt, foundReq bool
var found int
if len(got) != len(exp) {
t.Errorf("expected %d parameters, got %d", len(exp), len(got))
}
for _, expParam := range exp {
for _, gotParam := range got {
foundKey = true
if key, ok := expParam["key"]; ok {
if key != gotParam.Key {
foundKey = false
}
}
foundPatt = true
if patt, ok := expParam["pattern"]; ok {
if patt != gotParam.Pattern {
foundPatt = false
}
}
foundReq = true
if req, ok := expParam["required"]; ok {
// convert bool to string
gotReq := fmt.Sprintf("%t", gotParam.Required)
if req != gotReq {
foundReq = false
}
}
if foundKey && foundPatt && foundReq {
found += 1
}
}
}
return found == len(exp)
}