-
Notifications
You must be signed in to change notification settings - Fork 30
/
mock_test.go
71 lines (57 loc) · 1.43 KB
/
mock_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package hubspot
import (
"bytes"
"io/ioutil"
"net/http"
"time"
)
// For test only
type RoundTripFunc func(req *http.Request) *http.Response
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
type MockConfig struct {
Status int
Header http.Header
Body []byte
}
// Client
func NewMockClient(conf *MockConfig) *Client {
cli := &Client{
HTTPClient: NewMockHTTPClient(conf),
baseURL: defaultBaseURL,
apiVersion: defaultAPIVersion,
}
cli.CRM = newCRM(cli)
SetPrivateAppToken("token")(cli)
return cli
}
func NewMockHTTPClient(conf *MockConfig) *http.Client {
return &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: conf.Status,
Body: ioutil.NopCloser(bytes.NewBuffer(conf.Body)),
Header: conf.Header,
}
}),
}
}
// OAuth
func NewMockOAuthTokenRetriever() OAuthTokenRetriever {
return &MockOAuthTokenManager{}
}
type MockOAuthTokenManager struct{}
func (otm *MockOAuthTokenManager) RetrieveToken() (*OAuthToken, error) {
return &OAuthToken{
AccessToken: "test_access_token",
RefreshToken: "test_access_token",
ExpiresIn: 21600,
Expiry: time.Date(9999, 12, 31, 0, 0, 0, 0, time.Local),
}, nil
}
func MockTimeNow() func() {
mockTime := time.Date(2020, 12, 31, 12, 0, 0, 0, time.UTC)
timeNow = func() time.Time { return mockTime }
return func() { timeNow = time.Now }
}