-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
116 lines (98 loc) · 3.26 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
package mockserver
import (
"bytes"
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
)
type MockServerClient struct {
restyClient *resty.Client
}
// NewClient creates a new client provided its host and port
func NewClient(host string, port int) MockServerClient {
return NewClientURL(fmt.Sprintf("http://%s:%d", host, port))
}
// NewClientURL creates a new client provided its URL
func NewClientURL(url string) MockServerClient {
return MockServerClient{
restyClient: resty.New().
SetHostURL(url),
}
}
// SetDebug enables or disables the debug
func (c MockServerClient) SetDebug(d bool) MockServerClient {
c.restyClient.SetDebug(d)
return c
}
// Verify checks if the mock server received requests matching the matcher.
func (c MockServerClient) Verify(matcher RequestMatcher, times Times) error {
payload := map[string]interface{}{
"httpRequest": matcher,
"times": times,
}
resp, err := c.restyClient.NewRequest().
SetDoNotParseResponse(true).
SetBody(payload).
Put("/mockserver/verify")
if err != nil {
return errors.Wrap(err, "error calling verify endpoint")
}
if resp.StatusCode() == http.StatusAccepted {
return nil
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.RawBody())
return errors.Wrap(errors.New(buf.String()), "verification failed")
}
// Clear erases from the mock server all the requests matching the matcher.
func (c MockServerClient) Clear(matcher RequestMatcher) error {
resp, err := c.restyClient.NewRequest().
SetBody(matcher).
Put("mockserver/clear?type=LOG")
if err != nil {
return errors.Wrap(err, "error calling clear endpoint (type=LOGS)")
}
if resp.StatusCode() != http.StatusOK {
return errors.Wrap(errors.New("status was expected to be 200"), "log clearing failed")
}
return nil
}
// VerifyAndClear checks if the mock server received requests matching the matcher
// and then erases from the logs the requests matching the matcher.
func (c MockServerClient) VerifyAndClear(matcher RequestMatcher, times Times) error {
err_verify := c.Verify(matcher, times)
err_clear := c.Clear(matcher)
if err_verify != nil {
return errors.Wrap(err_verify, "could not verify")
}
if err_clear != nil {
return errors.Wrap(err_clear, "could not clear")
}
return nil
}
// VerifyAndClearByHeader checks if the mock server received requests matching the matcher
// and having the specified header name and value.
// It then erases from the logs the requests matching the same header name and value.
func (c MockServerClient) VerifyAndClearByHeader(headerName, headerValue string, matcher RequestMatcher, times Times) error {
err_verify := c.Verify(matcher.WithHeader(headerName, headerValue), times)
err_clear := c.Clear(RequestMatcher{}.WithHeader(headerName, headerValue))
if err_verify != nil {
return errors.Wrap(err_verify, "could not verify")
}
if err_clear != nil {
return errors.Wrap(err_clear, "could not clear")
}
return nil
}
// Set a new Expectation in mock server with request and response
func (c MockServerClient) RegisterExpectation(expectation Expectation) error {
_, err := c.restyClient.NewRequest().
SetDoNotParseResponse(true).
SetBody(expectation).
Put("/mockserver/expectation")
if err != nil {
return errors.Wrap(err, "error calling SetExpectation endpoint")
}
return nil
}