-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
550 lines (480 loc) · 14.6 KB
/
client.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright (c) 2018 Senseye Ltd. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in the LICENSE file.
package mbgo
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"time"
"github.com/senseyeio/mbgo/internal/rest"
)
// Client represents a native client to the mountebank REST API.
type Client struct {
restCli *rest.Client
}
// NewClient returns a new instance of *Client given its underlying
// *http.Client restCli and base *url.URL to the mountebank API root.
//
// If nil, defaults the root *url.URL value to point to http://localhost:2525.
func NewClient(cli *http.Client, root *url.URL) *Client {
if root == nil {
root = &url.URL{
Scheme: "http",
Host: net.JoinHostPort("localhost", "2525"),
}
}
return &Client{
restCli: rest.NewClient(cli, root),
}
}
// errorDTO represents the structure of an error received from the mountebank API.
type errorDTO struct {
Code string `json:"code"`
Message string `json:"message"`
}
// decodeError is a helper method used to decode an errorDTO structure from the
// given response body, usually when an unexpected response code is returned.
func (cli *Client) decodeError(body io.ReadCloser) error {
var wrap struct {
Errors []errorDTO `json:"errors"`
}
if err := cli.restCli.DecodeResponseBody(body, &wrap); err != nil {
return err
}
// Silently ignore all but the first error value if multiple are returned
dto := wrap.Errors[0]
return fmt.Errorf("%s: %s", dto.Code, dto.Message)
}
// Create creates a single new Imposter given its creation details imp.
//
// Note that the Imposter.RequestCount field is not used during creation.
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#post-imposters.
func (cli *Client) Create(ctx context.Context, imp Imposter) (*Imposter, error) {
p := "/imposters"
b, err := json.Marshal(&imp)
if err != nil {
return nil, err
}
req, err := cli.restCli.NewRequest(ctx, http.MethodPost, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusCreated {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// Imposter retrieves the Imposter data at the given port.
//
// Note that the Imposter.RecordRequests and Imposter.AllowCORS fields
// are ignored when un-marshalling an Imposter value and should only be
// used when creating an Imposter.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#get-imposter.
func (cli *Client) Imposter(ctx context.Context, port int, replay bool) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d", port)
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// AddStub adds a new Stub without restarting its Imposter given the imposter's
// port and the new stub's index, or simply to the end of the array if index < 0.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#add-stub
func (cli *Client) AddStub(ctx context.Context, port, index int, stub Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs", port)
dto := map[string]interface{}{"stub": stub}
if index >= 0 {
dto["index"] = index
}
b, err := json.Marshal(dto)
if err != nil {
return nil, err
}
req, err := cli.restCli.NewRequest(ctx, http.MethodPost, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// OverwriteStub overwrites an existing Stub without restarting its Imposter,
// where the stub index denotes the stub to be changed.
//
// See more information about this resouce at:
// http://www.mbtest.org/docs/api/overview#change-stub
func (cli *Client) OverwriteStub(ctx context.Context, port, index int, stub Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs/%d", port, index)
b, err := json.Marshal(stub)
if err != nil {
return nil, err
}
req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// OverwriteAllStubs overwrites all existing Stubs without restarting their Imposter.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#change-stubs
func (cli *Client) OverwriteAllStubs(ctx context.Context, port int, stubs []Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs", port)
b, err := json.Marshal(map[string]interface{}{
"stubs": stubs,
})
if err != nil {
return nil, err
}
req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// RemoveStub removes a Stub without restarting its Imposter.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-stub
func (cli *Client) RemoveStub(ctx context.Context, port, index int) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs/%d", port, index)
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, http.NoBody, nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// Delete removes an Imposter configured on the given port and returns
// the deleted Imposter data, or an empty Imposter struct if one does not
// exist on the port.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposter.
func (cli *Client) Delete(ctx context.Context, port int, replay bool) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d", port)
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, vs)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
// DeleteRequests removes any recorded requests associated with the
// Imposter on the given port and returns the Imposter including the
// deleted requests, or an empty Imposter struct if one does not exist
// on the port.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposter-requests.
func (cli *Client) DeleteRequests(ctx context.Context, port int) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/savedProxyResponses", port)
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}
type imposterListWrapper struct {
Imposters []Imposter `json:"imposters"`
}
// Overwrite is used to overwrite all registered Imposters with a new
// set of Imposters. This call is destructive, removing all previous
// Imposters even if the new set of Imposters do not conflict with
// previously registered protocols/ports.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#put-imposters.
func (cli *Client) Overwrite(ctx context.Context, imps []Imposter) ([]Imposter, error) {
p := "/imposters"
b, err := json.Marshal(&struct {
Imposters []Imposter `json:"imposters"`
}{
Imposters: imps,
})
if err != nil {
return nil, err
}
req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var wrap imposterListWrapper
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &wrap); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return wrap.Imposters, nil
}
// Imposters retrieves a list of all Imposters registered in mountebank.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#get-imposters.
func (cli *Client) Imposters(ctx context.Context, replay bool) ([]Imposter, error) {
p := "/imposters"
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var wrap imposterListWrapper
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &wrap); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return wrap.Imposters, nil
}
// DeleteAll removes all registered Imposters from mountebank and closes
// their listening socket. This is the surest way to reset mountebank
// between test runs.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposters.
func (cli *Client) DeleteAll(ctx context.Context, replay bool) ([]Imposter, error) {
p := "/imposters"
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, vs)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var wrap imposterListWrapper
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &wrap); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return wrap.Imposters, nil
}
// Config represents information about the configuration of the mountebank
// server runtime, including its version, options and runtime information.
//
// See more information about its full structure at:
// http://www.mbtest.org/docs/api/contracts?type=config.
type Config struct {
// Version represents the mountebank version in semantic M.m.p format.
Version string `json:"version"`
// Options represent runtime options of the mountebank server process.
Options struct {
Help bool `json:"help"`
NoParse bool `json:"noParse"`
NoLogFile bool `json:"nologfile"`
AllowInjection bool `json:"allowInjection"`
LocalOnly bool `json:"localOnly"`
Mock bool `json:"mock"`
Debug bool `json:"debug"`
Port int `json:"port"`
PIDFile string `json:"pidfile"`
LogFile string `json:"logfile"`
LogLevel string `json:"loglevel"`
IPWhitelist []string `json:"ipWhitelist"`
} `json:"options"`
// Process represents information about the mountebank server NodeJS runtime.
Process struct {
NodeVersion string `json:"nodeVersion"`
Architecture string `json:"architecture"`
Platform string `json:"platform"`
RSS int64 `json:"rss"`
HeapTotal int64 `json:"heapTotal"`
HeapUsed int64 `json:"heapUsed"`
Uptime float64 `json:"uptime"`
CWD string `json:"cwd"`
} `json:"process"`
}
// Config retrieves the configuration information of the mountebank
// server pointed to by the client.
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#get-config.
func (cli *Client) Config(ctx context.Context) (*Config, error) {
p := "/config"
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, nil)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var cfg Config
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &cfg); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &cfg, nil
}
// Log represents a log entry value in mountebank.
//
// See more information about its full structure at:
// http://www.mbtest.org/docs/api/contracts?type=logs.
type Log struct {
Level string `json:"level"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
}
// Logs retrieves the Log values across all registered Imposters
// between the provided start and end indices, with either index
// filter being excluded if less than zero. Set start < 0 and
// end < 0 to include all Log values.
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#get-logs.
func (cli *Client) Logs(ctx context.Context, start, end int) ([]Log, error) {
p := "/logs"
vs := url.Values{}
if start >= 0 {
vs.Add("startIndex", strconv.Itoa(start))
}
if end >= 0 {
vs.Add("endIndex", strconv.Itoa(end))
}
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}
var wrap struct {
Logs []Log `json:"logs"`
}
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &wrap); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return wrap.Logs, nil
}