- bfx-api-mock-srv
This module hosts mock servers for the WSv2 and RESTv2 Bitfinex APIs, and is intended for testing the Bitfinex API libraries.
- MockRESTv2Server ⇐
MockServer REST v2 API server mock
Exposes the same routes as the real API, and maps them to a response table. Multiple potential responses can be defined for endpoints with arguments, with the best match sent to clients on request.
i.e. If the following responses are configured:
orders.tBTCUSD: [42]orders: [41]
A
GETon/v2/auth/r/orders/tBTCUSD/histwould return[42], but a query for a different symbol (tETHUSD) would return[41].- MockServer ⇐
events.EventEmitter Mock server base class, listens for commands to get/set responses
- MockWSv2Server ⇐
MockServer Acts as a mock for v2 of the Bitfinex websocket API. Responses to available commands are loaded from data/ws2.json and can be modified at runtime. The command API allows for arbitrary packets to be injected into the ws stream.
Responses are of the form
[{ packets: [...] }], where mulitple packets are sent in order. A packet can be a string referencing another response by key.
This module hosts mock servers for the WSv2 and RESTv2 Bitfinex APIs, and is intended for testing the Bitfinex API libraries.
License: Apache-2.0
Example
const { MockRESTv2Server } = require('bfx-api-mock-srv')
const FUNDING_OFFER = [
41215275, 'fUSD', 1524784806000, 1524784806000, 1000, 1000, 'FRRDELTAVAR',
null, null, 0, 'ACTIVE', null, null, null, 0, 30, 0, 0, null, 0,
0.00207328
]
debug('spawning mock server...')
const srv = new MockRESTv2Server({ listen: true })
const rest = new RESTv2({
apiKey: 'dummy',
apiSecret: 'dummy',
url: 'http://localhost:9999'
})
srv.setResponse('f_offers.fUSD', [FUNDING_OFFER])
debug('requesting preset response...')
rest.fundingOffers({ symbol: 'fUSD' }).then(([incomingFundingOffer]) => {
assert.deepStrictEqual(incomingFundingOffer, FUNDING_OFFER)
debug('correct response received')
srv.close()
return null
}).catch((e) => {
debug(`error: ${e.message}`)
})MockRESTv2Server ⇐ MockServer
REST v2 API server mock
Exposes the same routes as the real API, and maps them to a response table. Multiple potential responses can be defined for endpoints with arguments, with the best match sent to clients on request.
i.e. If the following responses are configured:
orders.tBTCUSD: [42]orders: [41]
A GET on /v2/auth/r/orders/tBTCUSD/hist would return [42], but a query
for a different symbol (tETHUSD) would return [41].
Kind: global class
Extends: MockServer
- MockRESTv2Server ⇐
MockServer- new MockRESTv2Server([args])
- instance
- .listen()
- .close() ⇒
Promise - .getResponse(key) ⇒
string - .setResponse(key, data)
- static
- .keysForRoute(req, routeKey) ⇒
Array.<string>
- .keysForRoute(req, routeKey) ⇒
| Param | Type | Default | Description |
|---|---|---|---|
| [args] | object |
{} |
args |
| [args.apiPort] | number |
9999 |
API port number |
| [args.cmdPort] | number |
9998 |
command port number |
| [args.listen] | boolean |
true |
enables auto listen() |
Starts the API server listening on the configured port. This is a no-op if the server is already up
Kind: instance method of MockRESTv2Server
Overrides: listen
Closes the API server if it is running; This is a no-op if it is not.
Kind: instance method of MockRESTv2Server
Overrides: close
Returns: Promise - p
Returns the configured server response for the given key
Kind: instance method of MockRESTv2Server
Overrides: getResponse
Returns: string - response - JSON
| Param | Type | Description |
|---|---|---|
| key | string |
key |
Sets the provided data as the server response for the given key.
Kind: instance method of MockRESTv2Server
Overrides: setResponse
| Param | Type | Description |
|---|---|---|
| key | string |
key |
| data | Array | object |
data |
Kind: static method of MockRESTv2Server
Returns: Array.<string> - keys
| Param | Type | Description |
|---|---|---|
| req | express.Request |
request |
| routeKey | string |
key |
Mock server base class, listens for commands to get/set responses
Kind: global class
Extends: events.EventEmitter
- MockServer ⇐
events.EventEmitter- new MockServer(args, dataPath)
- .listen()
- .close() ⇒
Promise - .getResponse(key) ⇒
string - .setResponse(key, data)
| Param | Type | Default | Description |
|---|---|---|---|
| args | object |
args | |
| [args.cmdPort] | number |
9998 |
port to listen on for HTTP commands |
| dataPath | string |
path to JSON file with responses |
Starts the HTTP command server listening on the configured port. This is a no-op if the server is already up.
Kind: instance method of MockServer
Closes the command server if it is running, no-op if not.
Kind: instance method of MockServer
Returns: Promise - p
Returns the configured server response for the given key
Kind: instance method of MockServer
Returns: string - response - JSON
| Param | Type | Description |
|---|---|---|
| key | string |
key |
Sets the provided data as the server response for the given key.
Kind: instance method of MockServer
| Param | Type | Description |
|---|---|---|
| key | string |
key |
| data | Array | object |
data |
MockWSv2Server ⇐ MockServer
Acts as a mock for v2 of the Bitfinex websocket API. Responses to available commands are loaded from data/ws2.json and can be modified at runtime. The command API allows for arbitrary packets to be injected into the ws stream.
Responses are of the form [{ packets: [...] }], where mulitple packets are
sent in order. A packet can be a string referencing another response by key.
Kind: global class
Extends: MockServer
- MockWSv2Server ⇐
MockServer- new MockWSv2Server([args])
- .isOpen() ⇒
boolean - .listen()
- .close() ⇒
Promise - .once(eventName, cb)
- .send(packet)
- .getResponse(key) ⇒
string - .setResponse(key, data)
Spawns a new mock WS2 API server. Supported commands:
- POST /send - body is parsed as JSON and sent to all clients
- POST /config - body is parsed as JSON, and valid config keys are saved
| Param | Type | Default | Description |
|---|---|---|---|
| [args] | object |
[] |
arguments |
| [args.apiPort] | number |
9997 |
which port to listen on for ws clients |
| [args.cmdPort] | number |
9996 |
which port to listen on for commands |
| [args.syncOnConnect] | boolean |
true |
send snapshots to clients on connect |
| [args.authMiddleware=] | function |
handle auth response | |
| [args.listen] | boolean |
true |
if true, listen() is called automatically |
Returns server active status
Kind: instance method of MockWSv2Server
Returns: boolean - open
Starts the API server listening on the configured port. This is a no-op if the server is already up
Kind: instance method of MockWSv2Server
Overrides: listen
Closes the API server if it is running; This is a no-op if it is not.
Kind: instance method of MockWSv2Server
Overrides: close
Returns: Promise - p
Configures an event handler to be called once when the specified event is emitted by the API server. No-op if the server is not yet up.
Kind: instance method of MockWSv2Server
| Param | Type | Description |
|---|---|---|
| eventName | string |
event name |
| cb | function |
callback |
Sends the provided packet to all connected clients
Kind: instance method of MockWSv2Server
| Param | Type | Description |
|---|---|---|
| packet | object | Array |
stringifed before being sent |
Returns the configured server response for the given key
Kind: instance method of MockWSv2Server
Overrides: getResponse
Returns: string - response - JSON
| Param | Type | Description |
|---|---|---|
| key | string |
key |
Sets the provided data as the server response for the given key.
Kind: instance method of MockWSv2Server
Overrides: setResponse
| Param | Type | Description |
|---|---|---|
| key | string |
key |
| data | Array | object |
data |