-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
184 lines (147 loc) · 4.93 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
package wiremock
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
wiremockAdminURN = "__admin"
wiremockAdminMappingsURN = "__admin/mappings"
)
// A Client implements requests to the wiremock server.
type Client struct {
url string
}
// NewClient returns *Client.
func NewClient(url string) *Client {
return &Client{url: url}
}
// StubFor creates a new stub mapping.
func (c *Client) StubFor(stubRule *StubRule) error {
requestBody, err := stubRule.MarshalJSON()
if err != nil {
return fmt.Errorf("build stub request error: %s", err.Error())
}
res, err := http.Post(fmt.Sprintf("%s/%s", c.url, wiremockAdminMappingsURN), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return fmt.Errorf("stub request error: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response error: %s", err.Error())
}
return fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}
return nil
}
// Clear deletes all stub mappings.
func (c *Client) Clear() error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", c.url, wiremockAdminMappingsURN), nil)
if err != nil {
return fmt.Errorf("build cleare Request error: %s", err.Error())
}
res, err := (&http.Client{}).Do(req)
if err != nil {
return fmt.Errorf("clear Request error: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("bad response status: %d", res.StatusCode)
}
return nil
}
// Reset restores stub mappings to the defaults defined back in the backing store.
func (c *Client) Reset() error {
res, err := http.Post(fmt.Sprintf("%s/%s/reset", c.url, wiremockAdminMappingsURN), "application/json", nil)
if err != nil {
return fmt.Errorf("reset Request error: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response error: %s", err.Error())
}
return fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}
return nil
}
// ResetAllScenarios resets back to start of the state of all configured scenarios.
func (c *Client) ResetAllScenarios() error {
res, err := http.Post(fmt.Sprintf("%s/%s/scenarios/reset", c.url, wiremockAdminURN), "application/json", nil)
if err != nil {
return fmt.Errorf("reset all scenarios Request error: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response error: %s", err.Error())
}
return fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}
return nil
}
// GetCountRequests gives count requests by criteria.
func (c *Client) GetCountRequests(r *Request) (int64, error) {
requestBody, err := r.MarshalJSON()
if err != nil {
return 0, fmt.Errorf("get count requests: build error: %s", err.Error())
}
res, err := http.Post(fmt.Sprintf("%s/%s/requests/count", c.url, wiremockAdminURN), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return 0, fmt.Errorf("get count requests: %s", err.Error())
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return 0, fmt.Errorf("get count requests: read response error: %s", err.Error())
}
if res.StatusCode != http.StatusOK {
return 0, fmt.Errorf("get count requests: bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}
var countRequestsResponse struct {
Count int64 `json:"count"`
}
err = json.Unmarshal(bodyBytes, &countRequestsResponse)
if err != nil {
return 0, fmt.Errorf("get count requests: read json error: %s", err.Error())
}
return countRequestsResponse.Count, nil
}
// Verify checks count of request sent.
func (c *Client) Verify(r *Request, expectedCount int64) (bool, error) {
actualCount, err := c.GetCountRequests(r)
if err != nil {
return false, err
}
return actualCount == expectedCount, nil
}
// DeleteStubByID deletes stub by id.
func (c *Client) DeleteStubByID(id string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s/%s", c.url, wiremockAdminMappingsURN, id), nil)
if err != nil {
return fmt.Errorf("delete stub by id: build request error: %s", err.Error())
}
res, err := (&http.Client{}).Do(req)
if err != nil {
return fmt.Errorf("delete stub by id: request error: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response error: %s", err.Error())
}
return fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}
return nil
}
// DeleteStub deletes stub mapping.
func (c *Client) DeleteStub(s *StubRule) error {
return c.DeleteStubByID(s.UUID())
}