-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_response.go
71 lines (62 loc) · 1.79 KB
/
exchange_response.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
package testutil
import (
"fmt"
"testing"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/constants/exchange_common"
client_types "github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
"github.com/h2non/gock"
"github.com/stretchr/testify/require"
)
// Response represents an API response from an exchange.
type Response struct {
ResponseCode int
Tickers []JsonResponse
}
// JsonResponse is an interface that encapsulates a JSON API response.
type JsonResponse interface {
toJson() interface{}
}
// ExchangeIdAndName represents an exchange with an ID and a name.
type ExchangeIdAndName struct {
exchangeId client_types.ExchangeId
exchangeName string
}
// SetupExchangeResponses validates and sets up responses returned by exchange APIs using `gock`.
func SetupExchangeResponses(
t *testing.T,
responses map[ExchangeIdAndName]Response,
) {
// Setup `gock` responses.
for exchange, response := range responses {
setupGockResponse(t, exchange, response)
}
}
// setupGockResponse sets up the mock API responses returned by exchanges using `gock`.
func setupGockResponse(
t *testing.T,
exchange ExchangeIdAndName,
response Response,
) {
exchangeName := exchange.exchangeName
var gockResponse *gock.Response
switch exchangeName {
case exchange_common.EXCHANGE_ID_BINANCE:
gockResponse = NewGockBinanceResponse(
response.ResponseCode,
response.Tickers,
)
case exchange_common.EXCHANGE_ID_BINANCE_US:
gockResponse = NewGockBinanceUSResponse(
response.ResponseCode,
response.Tickers,
)
case exchange_common.EXCHANGE_ID_BITFINEX:
gockResponse = NewGockBitfinexResponse(
response.ResponseCode,
response.Tickers,
)
default:
panic(fmt.Errorf("unsupported exchange: %s", exchangeName))
}
require.NotNil(t, gockResponse)
}