forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testclient.go
78 lines (66 loc) · 2.08 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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package common
import (
"fmt"
"net/http"
)
// 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
}
// 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 {
// calls collects any API calls made through the client
calls []TestAPICall
}
// CapturedCalls gets all calls that were made through this instance
func (c *TestClient) CapturedCalls() []TestAPICall {
return c.calls
}
// Reset resets the captured information for this mock client.
func (c *TestClient) Reset() {
c.calls = []TestAPICall{}
}
// recordCall is a helper to record any API calls made through this client
func (c *TestClient) recordCall(action string, url string, payload interface{}) {
call := TestAPICall{
Action: action,
URL: url,
Payload: fmt.Sprintf("%v", payload),
}
c.calls = append(c.calls, call)
}
// Get performs a GET request against the Redfish service.
func (c *TestClient) Get(url string) (*http.Response, error) {
c.recordCall("GET", url, nil)
return nil, nil
}
// Post performs a Post request against the Redfish service.
func (c *TestClient) Post(url string, payload interface{}) (*http.Response, error) {
c.recordCall("POST", url, payload)
return nil, nil
}
// Put performs a Put request against the Redfish service.
func (c *TestClient) Put(url string, payload interface{}) (*http.Response, error) {
c.recordCall("PUT", url, payload)
return nil, nil
}
// Patch performs a Patch request against the Redfish service.
func (c *TestClient) Patch(url string, payload interface{}) (*http.Response, error) {
c.recordCall("PATH", url, payload)
return nil, nil
}
// Delete performs a Delete request against the Redfish service.
func (c *TestClient) Delete(url string) error {
c.recordCall("DELETE", url, nil)
return nil
}