-
Notifications
You must be signed in to change notification settings - Fork 33
/
bybit_api_client_test.go
150 lines (129 loc) · 3.2 KB
/
bybit_api_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
package bybit_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
apiSecret string
baseURL string
}
func (s *baseTestSuite) r() *require.Assertions {
return s.Require()
}
func (s *baseTestSuite) SetupTest() {
s.apiKey = "dummyAPIKey"
s.apiSecret = "dummyApiSecret"
s.baseURL = "https://dummyapi.com"
s.client = newMockedClient(s.apiKey, s.apiSecret, 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)
}
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{},
}
return r
}
func newSignedRequest() *request {
return newRequest().setParams(params{
timestampKey: "",
signatureKey: "",
apiRequestKey: "",
recvWindowKey: "5000",
signTypeKey: "2",
})
}
type assertReqFunc func(r *request)
type mockedClient struct {
mock.Mock
*Client
assertReq assertReqFunc
}
func newMockedClient(apiKey, apiSecret, baseURL string) *mockedClient {
m := new(mockedClient)
m.Client = NewBybitHttpClient(apiKey, apiSecret, WithBaseURL(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 && req.ContentLength > 0 {
bs := make([]byte, req.ContentLength)
_, err := req.Body.Read(bs)
if err != nil && err != io.EOF {
return nil, err // Handle read error
}
_ = req.Body.Close() // Close the body if we have read from it
r.body = bytes.NewBuffer(bs)
}
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))
}