-
Notifications
You must be signed in to change notification settings - Fork 107
/
testclient.go
204 lines (177 loc) · 6.9 KB
/
testclient.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package common
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
)
// TestAPICall captures the arguments to one of the API calls.
type TestAPICall struct {
// Action is the REST action (GET, PUT, etc) of the call
Action string
// URL is the URL to send to
URL string
// Payload is the string representation of the payload
Payload string
// CustomHeaders is the Map that holds customer HTTP headers
CustomHeaders map[string]string
}
// TestClient is a mock client to use for unit testing some of the
// function calls and actions that would normally need to connect
// with a host.
type TestClient struct {
mu sync.Mutex
// calls collects any API calls made through the client
calls []TestAPICall
// CustomReturnForActions can be used to define custom
// return for actions, valid keys are:
// http.MethodGet, http.MethodPost, http.MethodPut,
// http.MethodPatch and http.MethodDelete.
// For each key it is possible to define a list of
// returns (in the order they should be returned).
CustomReturnForActions map[string][]interface{}
}
// CapturedCalls gets all calls that were made through this instance
func (c *TestClient) CapturedCalls() []TestAPICall {
return c.calls
}
// actionCount returns how many actions
// of a specific type were already recorded.
func (c *TestClient) actionCount(action string) int {
var actionCount int
for _, call := range c.calls {
if call.Action == action {
actionCount++
}
}
return actionCount
}
// getCustomReturnForAction gets the custom return for the action
func (c *TestClient) getCustomReturnForAction(action string) interface{} {
switch action {
case http.MethodGet, http.MethodPost,
http.MethodPut, http.MethodPatch,
http.MethodDelete:
customReturnForAction, ok := c.CustomReturnForActions[action]
if !ok ||
customReturnForAction == nil ||
customReturnForAction[c.actionCountIndex(action)] == nil {
return nil
}
return customReturnForAction[c.actionCountIndex(action)]
}
return nil
}
// actionCountIndex returns the index that should be used
// to get the custom return from CustomReturnForActions.
func (c *TestClient) actionCountIndex(action string) int {
return c.actionCount(action) - 1
}
// getPayloadToBeRecorded returns the payload that will
// be recorded for the call.
func (c *TestClient) getPayloadToBeRecorded(payload interface{}) string {
// when possible do Marshal/Unmarshal of the payload
// in order to have the json keys when using interfaces
// in the payload.
if payload != nil {
payloadMarshaled, err := json.Marshal(payload)
if err != nil {
return fmt.Sprintf("%v", payload)
}
var payloadInterface interface{}
err = json.Unmarshal(payloadMarshaled, &payloadInterface)
if err != nil {
return fmt.Sprintf("%v", payload)
}
return fmt.Sprintf("%v", payloadInterface)
}
return ""
}
// Reset resets the captured information for this mock client.
func (c *TestClient) Reset() {
c.calls = []TestAPICall{}
c.CustomReturnForActions = map[string][]interface{}{}
}
// recordCall is a helper to record any API calls made through this client
func (c *TestClient) recordCall(action, url string, payload interface{}, customHeaders map[string]string) {
call := TestAPICall{
Action: action,
URL: url,
Payload: c.getPayloadToBeRecorded(payload),
CustomHeaders: customHeaders,
}
c.calls = append(c.calls, call)
}
func (c *TestClient) performAction(action, url string, payload interface{}, customHeaders map[string]string) (*http.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.recordCall(action, url, payload, customHeaders)
customReturnForAction := c.getCustomReturnForAction(action)
if customReturnForAction == nil {
body := io.NopCloser(strings.NewReader(""))
return &http.Response{Body: body}, nil
}
resp := customReturnForAction.(*http.Response)
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 202 && resp.StatusCode != 204 {
payload, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return nil, ConstructError(resp.StatusCode, payload)
}
return resp, nil
}
// Get performs a GET request against the Redfish service.
func (c *TestClient) Get(url string) (*http.Response, error) {
return c.performAction(http.MethodGet, url, nil, nil)
}
// GetWithHeaders performs a GET request against the Redfish service.
func (c *TestClient) GetWithHeaders(url string, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodGet, url, nil, customHeaders)
}
// Post performs a Post request against the Redfish service.
func (c *TestClient) Post(url string, payload interface{}) (*http.Response, error) {
return c.performAction(http.MethodPost, url, payload, nil)
}
// PostWithHeaders performs a Post request against the Redfish service.
func (c *TestClient) PostWithHeaders(url string, payload interface{}, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodPost, url, payload, customHeaders)
}
// PostMultipart performs a Post request against the Redfish service.
func (c *TestClient) PostMultipart(url string, payload map[string]io.Reader) (*http.Response, error) {
return c.performAction(http.MethodPost, url, payload, nil)
}
// PostMultipartWithHeaders performs a Post request against the Redfish service.
func (c *TestClient) PostMultipartWithHeaders(url string, payload map[string]io.Reader, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodPost, url, payload, customHeaders)
}
// Put performs a Put request against the Redfish service.
func (c *TestClient) Put(url string, payload interface{}) (*http.Response, error) {
return c.performAction(http.MethodPut, url, payload, nil)
}
// PutWithHeaders performs a Put request against the Redfish service.
func (c *TestClient) PutWithHeaders(url string, payload interface{}, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodPut, url, payload, customHeaders)
}
// Patch performs a Patch request against the Redfish service.
func (c *TestClient) Patch(url string, payload interface{}) (*http.Response, error) {
return c.performAction(http.MethodPatch, url, payload, nil)
}
// PatchWithHeaders performs a Patch request against the Redfish service.
func (c *TestClient) PatchWithHeaders(url string, payload interface{}, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodPatch, url, payload, customHeaders)
}
// Delete performs a Delete request against the Redfish service.
func (c *TestClient) Delete(url string) (*http.Response, error) {
return c.performAction(http.MethodDelete, url, nil, nil)
}
// DeleteWithHeaders performs a Delete request against the Redfish service.
func (c *TestClient) DeleteWithHeaders(url string, customHeaders map[string]string) (*http.Response, error) {
return c.performAction(http.MethodDelete, url, nil, customHeaders)
}