forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
196 lines (166 loc) · 5.27 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package bybit_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/Issengaard/bybit_sdk"
"github.com/Issengaard/bybit_sdk/testhelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient(t *testing.T) {
t.Run("rate limit error", func(t *testing.T) {
path := "/test"
method := http.MethodGet
status := http.StatusOK
respBody := struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}{
RetCode: 10006,
RetMsg: "Too many visits!",
ExtCode: "",
ExtInfo: "",
TimeNow: "1664610970.291886",
RateLimitStatus: 0,
RateLimitResetMs: 1664611023016,
RateLimit: 75,
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)
server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()
req, err := http.NewRequest(method, server.URL+path, nil)
require.NoError(t, err)
client := bybit.NewTestClient().
WithBaseURL(server.URL)
var got interface{}
var wantErr *bybit.RateLimitError
gotErr := client.Request(req, &got)
assert.ErrorAs(t, gotErr, &wantErr)
})
t.Run("34036, leverage not modified", func(t *testing.T) {
path := "/test"
method := http.MethodGet
status := http.StatusOK
respBody := struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}{
RetCode: 34036,
RetMsg: "leverage not modified",
ExtCode: "",
ExtInfo: "",
TimeNow: "1664610970.291886",
RateLimitStatus: 0,
RateLimitResetMs: 1664611023016,
RateLimit: 75,
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)
server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()
req, err := http.NewRequest(method, server.URL+path, nil)
require.NoError(t, err)
client := bybit.NewTestClient().
WithBaseURL(server.URL)
var got interface{}
var wantErr *bybit.ErrorResponse
gotErr := client.Request(req, &got)
assert.ErrorAs(t, gotErr, &wantErr)
t.Log(gotErr)
})
t.Run("10005, API key is invalid", func(t *testing.T) {
path := "/test"
method := http.MethodGet
status := http.StatusOK
respBody := struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
Result interface{} `json:"result"`
}{
RetCode: 10005,
RetMsg: "Permission denied, please check your API key permissions",
ExtCode: "",
ExtInfo: "",
TimeNow: "1674306441.372007",
RateLimitStatus: 0,
RateLimitResetMs: 0,
RateLimit: 0,
Result: struct{}{},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)
server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()
req, err := http.NewRequest(method, server.URL+path, nil)
require.NoError(t, err)
client := bybit.NewTestClient().
WithBaseURL(server.URL)
var got interface{}
var wantErr *bybit.ErrorResponse
gotErr := client.Request(req, &got)
assert.ErrorAs(t, gotErr, &wantErr)
t.Log(gotErr)
})
t.Run("403, access denied", func(t *testing.T) {
path := "/test"
method := http.MethodGet
status := http.StatusForbidden
respBody := struct {
Message string `json:"message"`
}{
Message: "ok",
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)
server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()
req, err := http.NewRequest(method, server.URL+path, nil)
require.NoError(t, err)
client := bybit.NewTestClient().
WithBaseURL(server.URL)
var got interface{}
gotErr := client.Request(req, &got)
assert.ErrorIs(t, gotErr, bybit.ErrForbiddenRequest)
})
t.Run("404, path not found", func(t *testing.T) {
path := "/test"
method := http.MethodGet
server, teardown := testhelper.NewServer()
defer teardown()
req, err := http.NewRequest(method, server.URL+path, nil)
require.NoError(t, err)
client := bybit.NewTestClient().
WithBaseURL(server.URL)
var got interface{}
gotErr := client.Request(req, &got)
assert.ErrorIs(t, gotErr, bybit.ErrPathNotFound)
})
}