forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks_test.go
48 lines (40 loc) · 1.68 KB
/
mocks_test.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
package packngo
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// MockClient makes it simpler to test the Client
type MockClient struct {
fnNewRequest func(method, path string, body interface{}) (*http.Request, error)
fnDo func(req *http.Request, v interface{}) (*Response, error)
fnDoRequest func(method, path string, body, v interface{}) (*Response, error)
fnDoRequestWithHeader func(method string, headers map[string]string, path string, body, v interface{}) (*Response, error)
}
var _ requestDoer = &MockClient{}
// NewRequest uses the mock NewRequest function
func (mc *MockClient) NewRequest(method, path string, body interface{}) (*http.Request, error) {
return mc.fnNewRequest(method, path, body)
}
// Do uses the mock Do function
func (mc *MockClient) Do(req *http.Request, v interface{}) (*Response, error) {
return mc.fnDo(req, v)
}
// DoRequest uses the mock DoRequest function
func (mc *MockClient) DoRequest(method, path string, body, v interface{}) (*Response, error) {
return mc.fnDoRequest(method, path, body, v)
}
// DoRequestWithHeader uses the mock DoRequestWithHeader function
func (mc *MockClient) DoRequestWithHeader(method string, headers map[string]string, path string, body, v interface{}) (*Response, error) {
return mc.fnDoRequestWithHeader(method, headers, path, body, v)
}
func mockResponse(code int, body string, req *http.Request) *Response {
return &Response{Response: &http.Response{
Status: fmt.Sprintf("%d Ignored", code),
StatusCode: code,
Body: ioutil.NopCloser(bytes.NewReader([]byte(body))), // TODO: io.NopCloser requires go 1.16+
ContentLength: int64(len(body)),
Request: req,
}}
}