|
| 1 | +package bybit_connector |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "github.com/stretchr/testify/suite" |
| 16 | +) |
| 17 | + |
| 18 | +type baseTestSuite struct { |
| 19 | + suite.Suite |
| 20 | + client *mockedClient |
| 21 | + apiKey string |
| 22 | + apiSecret string |
| 23 | + baseURL string |
| 24 | +} |
| 25 | + |
| 26 | +func (s *baseTestSuite) r() *require.Assertions { |
| 27 | + return s.Require() |
| 28 | +} |
| 29 | + |
| 30 | +func (s *baseTestSuite) SetupTest() { |
| 31 | + s.apiKey = "dummyAPIKey" |
| 32 | + s.apiSecret = "dummyApiSecret" |
| 33 | + s.baseURL = "https://dummyapi.com" |
| 34 | + s.client = newMockedClient(s.apiKey, s.apiSecret, s.baseURL) |
| 35 | +} |
| 36 | + |
| 37 | +func (s *baseTestSuite) mockDo(data []byte, err error, statusCode ...int) { |
| 38 | + s.client.Client.do = s.client.do |
| 39 | + code := http.StatusOK |
| 40 | + if len(statusCode) > 0 { |
| 41 | + code = statusCode[0] |
| 42 | + } |
| 43 | + s.client.On("do", anyHTTPRequest()).Return(newHTTPResponse(data, code), err) |
| 44 | +} |
| 45 | + |
| 46 | +func (s *baseTestSuite) assertDo() { |
| 47 | + s.client.AssertCalled(s.T(), "do", anyHTTPRequest()) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *baseTestSuite) assertReq(f func(r *request)) { |
| 51 | + s.client.assertReq = f |
| 52 | +} |
| 53 | + |
| 54 | +func (s *baseTestSuite) assertRequestEqual(e, a *request) { |
| 55 | + s.assertURLValuesEqual(e.query, a.query) |
| 56 | +} |
| 57 | + |
| 58 | +func (s *baseTestSuite) assertURLValuesEqual(e, a url.Values) { |
| 59 | + var eKeys, aKeys []string |
| 60 | + for k := range e { |
| 61 | + eKeys = append(eKeys, k) |
| 62 | + } |
| 63 | + for k := range a { |
| 64 | + aKeys = append(aKeys, k) |
| 65 | + } |
| 66 | + r := s.r() |
| 67 | + r.Len(aKeys, len(eKeys)) |
| 68 | + for k := range a { |
| 69 | + switch k { |
| 70 | + case timestampKey, signatureKey: |
| 71 | + r.NotEmpty(a.Get(k)) |
| 72 | + continue |
| 73 | + } |
| 74 | + r.Equal(e[k], a[k], k) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func anythingOfType(t string) mock.AnythingOfTypeArgument { |
| 79 | + return mock.AnythingOfType(t) |
| 80 | +} |
| 81 | + |
| 82 | +func newContext() context.Context { |
| 83 | + return context.Background() |
| 84 | +} |
| 85 | + |
| 86 | +func anyHTTPRequest() mock.AnythingOfTypeArgument { |
| 87 | + return anythingOfType("*http.Request") |
| 88 | +} |
| 89 | + |
| 90 | +func newHTTPResponse(data []byte, statusCode int) *http.Response { |
| 91 | + return &http.Response{ |
| 92 | + Body: io.NopCloser(bytes.NewBuffer(data)), |
| 93 | + StatusCode: statusCode, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func newRequest() *request { |
| 98 | + r := &request{ |
| 99 | + query: url.Values{}, |
| 100 | + } |
| 101 | + return r |
| 102 | +} |
| 103 | + |
| 104 | +func newSignedRequest() *request { |
| 105 | + return newRequest().setParams(params{ |
| 106 | + timestampKey: "", |
| 107 | + signatureKey: "", |
| 108 | + apiRequestKey: "", |
| 109 | + recvWindowKey: "5000", |
| 110 | + signTypeKey: "2", |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +type assertReqFunc func(r *request) |
| 115 | + |
| 116 | +type mockedClient struct { |
| 117 | + mock.Mock |
| 118 | + *Client |
| 119 | + assertReq assertReqFunc |
| 120 | +} |
| 121 | + |
| 122 | +func newMockedClient(apiKey, apiSecret, baseURL string) *mockedClient { |
| 123 | + m := new(mockedClient) |
| 124 | + m.Client = NewBybitHttpClient(apiKey, apiSecret, WithBaseURL(baseURL)) |
| 125 | + return m |
| 126 | +} |
| 127 | + |
| 128 | +func (m *mockedClient) do(req *http.Request) (*http.Response, error) { |
| 129 | + if m.assertReq != nil { |
| 130 | + r := newRequest() |
| 131 | + r.query = req.URL.Query() |
| 132 | + if req.Body != nil && req.ContentLength > 0 { |
| 133 | + bs := make([]byte, req.ContentLength) |
| 134 | + _, err := req.Body.Read(bs) |
| 135 | + if err != nil && err != io.EOF { |
| 136 | + return nil, err // Handle read error |
| 137 | + } |
| 138 | + _ = req.Body.Close() // Close the body if we have read from it |
| 139 | + r.body = bytes.NewBuffer(bs) |
| 140 | + } |
| 141 | + m.assertReq(r) |
| 142 | + } |
| 143 | + args := m.Called(req) |
| 144 | + return args.Get(0).(*http.Response), args.Error(1) |
| 145 | +} |
| 146 | + |
| 147 | +func TestFormatTimestamp(t *testing.T) { |
| 148 | + tm, _ := time.Parse("2006-01-02 15:04:05", "2018-06-01 01:01:01") |
| 149 | + assert.Equal(t, int64(1527814861000), FormatTimestamp(tm)) |
| 150 | +} |
0 commit comments