-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
197 lines (167 loc) · 5.01 KB
/
api_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
197
package binance_test
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/jaztec/go-binance"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
apiKey = "aap"
apiSecret = "noot"
)
func testServer(expectedPath string, expectedQueryParts map[string]struct{}, status int, response []byte, responseHeaders map[string]string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
if responseHeaders != nil {
for k, v := range responseHeaders {
w.Header().Set(k, v)
}
}
if status != http.StatusOK {
w.WriteHeader(status)
_, _ = w.Write(response)
return
}
Expect(r.URL.Path).To(Equal(expectedPath))
for k, _ := range r.URL.Query() {
_, ok := expectedQueryParts[k]
Expect(ok).To(Equal(true), fmt.Sprintf("Expected %s to be present", k))
}
_, _ = w.Write(response)
}))
}
func newAPI(uri string) binance.APICaller {
defer GinkgoRecover()
a, err := binance.NewAPICaller(binance.APIConfig{
Key: apiKey,
Secret: apiSecret,
BaseURI: uri,
BaseStreamURI: strings.ReplaceAll(uri, "ws", "http"),
})
Expect(err).To(BeNil())
return a.(binance.APICaller)
}
func loadFixture(name string) []byte {
defer GinkgoRecover()
b, err := ioutil.ReadFile(fmt.Sprintf("tests/data/%s.json", name))
Expect(err).To(BeNil())
return b
}
var _ = Describe("Api", func() {
Context("create new API", func() {
var a binance.API
BeforeEach(func() {
var err error
a, err = binance.NewAPICaller(binance.APIConfig{
Key: apiKey,
Secret: apiSecret,
BaseURI: "http://mies.mees",
BaseStreamURI: "ws://mies.mees",
})
Expect(err).To(BeNil())
})
It("should have an instance of a Stream", func() {
Expect(a.Stream()).ToNot(BeNil())
})
})
Context("API weight results must be respected", func() {
Context("Should halt on warning", func() {
It("should respect API limits", func() {
ts := testServer("/api/v3/avgPrice", map[string]struct{}{
"symbol": {},
}, http.StatusTooManyRequests, nil, map[string]string{"Retry-After": "30"})
defer ts.Close()
a := newAPI(ts.URL)
_, err := a.AvgPrice("ETHBTC")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(binance.TooMuchCalls.Error()))
_, err = a.AvgPrice("ETHBTC")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(binance.AtTimeout.Error()))
})
})
Context("Should halt on block", func() {
It("should respect API limits", func() {
ts := testServer("/api/v3/avgPrice", map[string]struct{}{
"symbol": {},
}, http.StatusTeapot, nil, map[string]string{"Retry-After": "30"})
defer ts.Close()
a := newAPI(ts.URL)
_, err := a.AvgPrice("ETHBTC")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(binance.Blocked.Error()))
_, err = a.AvgPrice("ETHBTC")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(binance.AtTimeout.Error()))
})
})
})
Context("call API endpoints", func() {
Context("should call account data", func() {
It("should work on success", func() {
v := url.Values{}
v.Set("timestamp", "0001")
v.Set("signature", "aSignature")
res := loadFixture("account_data")
ts := testServer("/api/v3/account", map[string]struct{}{
"timestamp": {},
"signature": {},
}, http.StatusOK, res, nil)
defer ts.Close()
a := newAPI(ts.URL)
ai, err := a.Account()
Expect(err).To(BeNil(), "calling Account should not return error")
Expect(ai.MakerCommission).To(Equal(10))
Expect(ai.UpdateTime).To(Equal(1619000000000))
Expect(ai.Balances).To(HaveLen(1))
Expect(ai.Balances[0].Asset).To(Equal("BTC"))
Expect(ai.Balances[0].Free).To(Equal("10.00000000"))
Expect(ai.Balances[0].Locked).To(Equal("5.00000000"))
})
It("should work on error", func() {
v := url.Values{}
v.Set("timestamp", "0001")
v.Set("signature", "aSignature")
ts := testServer("/api/v3/account", map[string]struct{}{
"timestamp": {},
"signature": {},
}, http.StatusInternalServerError, []byte("{}"), nil)
defer ts.Close()
a := newAPI(ts.URL)
_, err := a.Account()
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("code=0, msg="))
})
})
Context("Work on AvgPrice", func() {
var ts *httptest.Server
var a binance.APICaller
BeforeEach(func() {
res := loadFixture("avg_price_data")
ts = testServer("/api/v3/avgPrice", map[string]struct{}{
"symbol": {},
}, http.StatusOK, res, nil)
a = newAPI(ts.URL)
})
AfterEach(func() {
ts.Close()
})
It("should work on regular call", func() {
av, err := a.AvgPrice("ETHBTC")
Expect(err).To(BeNil(), "calling AvgPrice should not return error")
Expect(av.Mins).To(Equal(5))
Expect(av.Price).To(Equal("0.06656334"))
})
It("should detect missing symbol", func() {
_, err := a.AvgPrice("")
Expect(err).ToNot(BeNil())
Expect(err).To(Equal(binance.NoSymbolProvided))
})
})
})
})