Skip to content

Latest commit

 

History

History
348 lines (260 loc) · 12.7 KB

File metadata and controls

348 lines (260 loc) · 12.7 KB

Modules

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.

Classes

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].

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.

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.

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

new MockRESTv2Server([args])

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()

mockRESTv2Server.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

mockRESTv2Server.close() ⇒ Promise

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

mockRESTv2Server.getResponse(key) ⇒ string

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

mockRESTv2Server.setResponse(key, data)

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

MockRESTv2Server.keysForRoute(req, routeKey) ⇒ Array.<string>

Kind: static method of MockRESTv2Server
Returns: Array.<string> - keys

Param Type Description
req express.Request request
routeKey string key

MockServer ⇐ events.EventEmitter

Mock server base class, listens for commands to get/set responses

Kind: global class
Extends: events.EventEmitter

new MockServer(args, dataPath)

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

mockServer.listen()

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

mockServer.close() ⇒ Promise

Closes the command server if it is running, no-op if not.

Kind: instance method of MockServer
Returns: Promise - p

mockServer.getResponse(key) ⇒ string

Returns the configured server response for the given key

Kind: instance method of MockServer
Returns: string - response - JSON

Param Type Description
key string key

mockServer.setResponse(key, data)

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

new MockWSv2Server([args])

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

mockWSv2Server.isOpen() ⇒ boolean

Returns server active status

Kind: instance method of MockWSv2Server
Returns: boolean - open

mockWSv2Server.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 MockWSv2Server
Overrides: listen

mockWSv2Server.close() ⇒ Promise

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

mockWSv2Server.once(eventName, cb)

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

mockWSv2Server.send(packet)

Sends the provided packet to all connected clients

Kind: instance method of MockWSv2Server

Param Type Description
packet object | Array stringifed before being sent

mockWSv2Server.getResponse(key) ⇒ string

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

mockWSv2Server.setResponse(key, data)

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