-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.go
386 lines (317 loc) · 9.42 KB
/
integration.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package gointegration
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/btm6084/gojson"
"github.com/spf13/cast"
)
var (
// To facilitate the ability to pass multiple query parameters with the same name,
// parameters can be named as "paramName{number}"
// e.g. www.example.com?filter=abc&filter=def
// becomes: map[string]interface{}{"filter": "abc", "filter{1}": "def"}
multiParamPattern = regexp.MustCompile("\\{\\d+\\}")
defaultIDHeader = "X-Integration-Tests"
defaultScheme = "http"
defaultHost = "localhost"
defaultPort = 4080
defaultTimeout = 0
)
// Client parses a swagger.json document and exposes an interface for creating
// API calls to the endpoint specified.
type Client struct {
FollowRedirects bool
Hostname string
IdentityHeader string
Port int
Scheme string
Endpoints map[string]Endpoints
// Time is in MS
Timeout int
Client *http.Client
}
// Endpoints is a collection of swagger endpoints
type Endpoints map[string]Route
// Route represents an API path.
type Route struct {
ID string `json:"id"`
Method string `json:"method"`
Parameters map[string]ParamSpec `json:"parameters"`
Path string `json:"path"`
Produces []string `json:"produces"`
}
// ParamSpec represent API param specifications.
type ParamSpec struct {
FoundIn string `json:"found_in"`
Name string `json:"name"`
Required bool `json:"required"`
Type string `json:"type"`
ContentType string `json:"content_type"`
}
// BuildClient creates a new swagger dument from a file on the filesystem.
func BuildClient(path string) (*Client, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
scheme := defaultScheme
if os.Getenv("SCHEME") != "" {
scheme = os.Getenv("SCHEME")
}
host := defaultHost
if os.Getenv("HOST") != "" {
host = os.Getenv("HOST")
}
idHeader := defaultIDHeader
if os.Getenv("IDENTITY") != "" {
idHeader = os.Getenv("IDENTITY")
}
port := defaultPort
if os.Getenv("PORT") != "" {
var err error
port, err = strconv.Atoi(os.Getenv("PORT"))
if err != nil {
fmt.Printf("Invalid Port '%s'.\n", os.Getenv("PORT"))
port = defaultPort
}
}
// Timeout should be an integer in milliseconds.
timeout := defaultTimeout
if os.Getenv("TIMEOUT") != "" {
var err error
timeout, err = strconv.Atoi(os.Getenv("TIMEOUT"))
if err != nil {
fmt.Printf("Invalid Timeout '%s'.\n", os.Getenv("TIMEOUT"))
timeout = defaultTimeout
}
}
sc := Client{}
sc.Scheme = scheme
sc.Hostname = host
sc.IdentityHeader = idHeader
sc.Port = port
sc.Timeout = timeout
sc.Client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if sc.FollowRedirects {
return nil
}
return http.ErrUseLastResponse
},
Timeout: time.Duration(sc.Timeout) * time.Millisecond,
}
sc.load(data)
return &sc, nil
}
func (sc *Client) load(data []byte) error {
reader, err := gojson.NewJSONReader(data)
if err != nil {
return err
}
sc.Endpoints = make(map[string]Endpoints)
paths := reader.Get("paths")
for _, path := range paths.Keys {
methods := paths.Get(path)
for _, method := range methods.Keys {
data := methods.Get(method)
r := Route{}
id := data.GetString("operationId")
r.ID = id
r.Method = method
r.Path = path
r.Produces = data.GetStringSlice("produces")
paramList := data.GetCollection("parameters")
r.Parameters = make(map[string]ParamSpec, len(paramList))
for _, param := range paramList {
var p ParamSpec
p.FoundIn = param.GetString("in")
p.Name = param.GetString("name")
p.Required = param.GetBool("required")
p.Type = param.GetString("type")
r.Parameters[p.Name] = p
}
tags := data.GetStringSlice("tags")
for _, t := range tags {
if sc.Endpoints[t] == nil {
sc.Endpoints[t] = make(Endpoints)
}
sc.Endpoints[t][r.ID] = r
}
if len(tags) == 0 {
if sc.Endpoints["default"] == nil {
sc.Endpoints["default"] = make(Endpoints)
}
sc.Endpoints["default"][r.ID] = r
}
}
}
return nil
}
// ExecJSON takes a path specifier and executes the corresponding functionality if found in the loaded swagger doc, returning a JSONResponse.
func (sc *Client) ExecJSON(specifier string, params map[string]interface{}) JSONResponse {
resp := sc.Exec(specifier, params)
reader, _ := gojson.NewJSONReader([]byte(resp.Body))
return JSONResponse{
ClientResponse: ClientResponse{
Body: resp.Body,
Error: resp.Error,
Headers: resp.Headers,
Status: resp.Status,
RequestTime: resp.RequestTime,
RequestURL: resp.RequestURL,
StatusCode: resp.StatusCode,
},
Reader: reader,
}
}
// Exec takes a path specifier and executes the corresponding functionality if found in the loaded swagger doc, returning a generic ClientResponse.
func (sc *Client) Exec(specifier string, params map[string]interface{}) ClientResponse {
pieces := strings.Split(specifier, ".")
var tag string
var id string
switch len(pieces) {
default:
return ClientResponse{Error: fmt.Errorf("Exec: Invalid path specifier %s", id)}
case 1:
tag = "default"
id = pieces[0]
case 2:
tag = pieces[0]
id = pieces[1]
}
if _, isset := sc.Endpoints[tag]; !isset {
return ClientResponse{Error: fmt.Errorf("Exec: Route %s not found", specifier)}
}
if _, isset := sc.Endpoints[tag][id]; !isset {
return ClientResponse{Error: fmt.Errorf("Exec: Route %s not found", specifier)}
}
route := sc.Endpoints[tag][id]
// Reject if we're missing required parameters.
for _, ps := range route.Parameters {
if _, isset := params[ps.Name]; ps.Required && !isset {
return ClientResponse{Error: fmt.Errorf("Exec: Required Parameter '%s' not provided", ps.Name)}
}
}
var err error
var postBody []byte
var query []string
headers := make(map[string]string)
// Put the parameters into the correct place depending on the "in" value.
for name, val := range params {
// Remove any positional designators to allow for the same query parameter to be used multiple times.
// Refer to comment on var declaration for multiParamPattern
name = multiParamPattern.ReplaceAllString(name, "")
if _, isset := route.Parameters[name]; !isset {
return ClientResponse{Error: fmt.Errorf("[Extraneous Parameter] '%s.%s' has no parameter specification '%s'", tag, id, name)}
}
ps := route.Parameters[name]
switch ps.FoundIn {
case "path":
route.Path = strings.Replace(route.Path, fmt.Sprintf("{%s}", name), url.PathEscape(cast.ToString(val)), -1)
case "body":
if reflect.TypeOf(val).String() == "[]uint8" {
postBody = val.([]byte)
} else {
postBody, err = json.Marshal(val)
if err != nil {
return ClientResponse{Error: fmt.Errorf("Marshal of postBody failed with message: %s", err.Error())}
}
}
case "query":
query = append(query, fmt.Sprintf("%s=%s", name, url.QueryEscape(cast.ToString(val))))
case "header":
headers[name] = cast.ToString(val)
//case "cookie":
// @TODO
}
}
// Construct the URL
url := sc.buildURL(route.Path, query)
// Build the request
req, err := http.NewRequest(strings.ToUpper(route.Method), url, bytes.NewBuffer(postBody))
if err != nil {
return ClientResponse{Error: err}
}
// Set Content-Type header.
contentType := "application/json"
if len(route.Produces) > 0 {
contentType = route.Produces[0]
}
req.Header.Set("Content-Type", contentType)
// Add headers
for k, v := range headers {
req.Header.Set(k, v)
}
// Indentify ourself as an integration test to the service.
// The service can handle / ignore this as it sees fit.
req.Header.Set(sc.IdentityHeader, "true")
// @TODO
// Add cookies
return sc.MakeRequest(req)
}
// buildURL returns the url based on the host, port, and scheme set in the Client
func (sc *Client) buildURL(path string, query []string) string {
var separator string
switch true {
case len(query) > 0 && strings.Contains(path, "?"):
separator = "&"
case len(query) > 0:
separator = "?"
}
path = strings.TrimLeft(path, "/")
return fmt.Sprintf("%s://%s:%d/%s%s%s", sc.Scheme, sc.Hostname, sc.Port, path, separator, strings.Join(query, "&"))
}
// MakeRequest makes a request to a third party HTTP resource based on the given http.Request object.
func (sc *Client) MakeRequest(req *http.Request) ClientResponse {
start := time.Now()
res, err := sc.Client.Do(req)
elapsed := time.Since(start)
if err != nil {
return ClientResponse{Error: fmt.Errorf("Request to URL %s failed with error: %s", req.URL, err.Error())}
}
defer res.Body.Close()
// Decompress gzip content
var rawBody io.ReadCloser
switch res.Header.Get("Content-Encoding") {
case "gzip":
rawBody, err = gzip.NewReader(res.Body)
if err != nil {
rawBody = res.Body
}
default:
rawBody = res.Body
}
body, err := ioutil.ReadAll(rawBody)
if err != nil {
return ClientResponse{Error: fmt.Errorf("Unable to unpack body from request to URL %s: %s", req.URL, err.Error())}
}
headers := make(map[string]string)
for k, set := range res.Header {
if len(set) > 0 {
headers[k] = set[0]
}
}
out := ClientResponse{
Body: string(body),
Cookies: res.Cookies(),
Error: nil,
Headers: headers,
RequestTime: fmt.Sprint(elapsed),
RequestURL: req.URL.String(),
Status: http.StatusText(res.StatusCode),
StatusCode: res.StatusCode,
}
return out
}