-
Notifications
You must be signed in to change notification settings - Fork 75
/
client_test.go
154 lines (133 loc) · 3.12 KB
/
client_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
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
package binance_connector
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type baseTestSuite struct {
suite.Suite
client *mockedClient
apiKey string
secretKey string
baseURL string
}
func (s *baseTestSuite) r() *require.Assertions {
return s.Require()
}
func (s *baseTestSuite) SetupTest() {
s.apiKey = "dummyAPIKey"
s.secretKey = "dummySecretKey"
s.baseURL = "https://dummyapi.com"
s.client = newMockedClient(s.apiKey, s.secretKey, s.baseURL)
}
func (s *baseTestSuite) mockDo(data []byte, err error, statusCode ...int) {
s.client.Client.do = s.client.do
code := http.StatusOK
if len(statusCode) > 0 {
code = statusCode[0]
}
s.client.On("do", anyHTTPRequest()).Return(newHTTPResponse(data, code), err)
}
func (s *baseTestSuite) assertDo() {
s.client.AssertCalled(s.T(), "do", anyHTTPRequest())
}
func (s *baseTestSuite) assertReq(f func(r *request)) {
s.client.assertReq = f
}
func (s *baseTestSuite) assertRequestEqual(e, a *request) {
s.assertURLValuesEqual(e.query, a.query)
s.assertURLValuesEqual(e.form, a.form)
}
func (s *baseTestSuite) assertURLValuesEqual(e, a url.Values) {
var eKeys, aKeys []string
for k := range e {
eKeys = append(eKeys, k)
}
for k := range a {
aKeys = append(aKeys, k)
}
r := s.r()
r.Len(aKeys, len(eKeys))
for k := range a {
switch k {
case timestampKey, signatureKey:
r.NotEmpty(a.Get(k))
continue
}
r.Equal(e[k], a[k], k)
}
}
func anythingOfType(t string) mock.AnythingOfTypeArgument {
return mock.AnythingOfType(t)
}
func newContext() context.Context {
return context.Background()
}
func anyHTTPRequest() mock.AnythingOfTypeArgument {
return anythingOfType("*http.Request")
}
func newHTTPResponse(data []byte, statusCode int) *http.Response {
return &http.Response{
Body: io.NopCloser(bytes.NewBuffer(data)),
StatusCode: statusCode,
}
}
func newRequest() *request {
r := &request{
query: url.Values{},
form: url.Values{},
}
return r
}
func newSignedRequest() *request {
return newRequest().setParams(params{
timestampKey: "",
signatureKey: "",
})
}
type assertReqFunc func(r *request)
type mockedClient struct {
mock.Mock
*Client
assertReq assertReqFunc
}
func newMockedClient(apiKey, secretKey, baseURL string) *mockedClient {
m := new(mockedClient)
m.Client = NewClient(apiKey, secretKey, baseURL)
return m
}
func (m *mockedClient) do(req *http.Request) (*http.Response, error) {
if m.assertReq != nil {
r := newRequest()
r.query = req.URL.Query()
if req.Body != nil {
bs := make([]byte, req.ContentLength)
for {
n, _ := req.Body.Read(bs)
if n == 0 {
break
}
}
form, err := url.ParseQuery(string(bs))
if err != nil {
panic(err)
}
r.form = form
}
m.assertReq(r)
}
args := m.Called(req)
return args.Get(0).(*http.Response), args.Error(1)
}
func TestFormatTimestamp(t *testing.T) {
tm, _ := time.Parse("2006-01-02 15:04:05", "2018-06-01 01:01:01")
assert.Equal(t, int64(1527814861000), FormatTimestamp(tm))
}