bfx-api-mock-srv
Advanced tools
Comparing version
@@ -15,3 +15,3 @@ 'use strict' | ||
* @param {Object} args | ||
* @param {number} cmdPort - port to listen on for HTTP command API | ||
* @param {number} args.cmdPort - port to listen on for HTTP command API | ||
* @param {string} dataPath - path to JSON file with responses | ||
@@ -41,2 +41,3 @@ */ | ||
* @param {string} path | ||
* @private | ||
*/ | ||
@@ -62,2 +63,6 @@ _loadResponsesFromFile (path) { | ||
/** | ||
* Starts the HTTP command server listening on the configured port. This is | ||
* a no-op if the server is already up. | ||
*/ | ||
listen () { | ||
@@ -70,2 +75,7 @@ if (this._cmdServerHTTP) return | ||
/** | ||
* Closes the command server if it is running, no-op if not. | ||
* | ||
* @return {Promise} p - resolves upon completion | ||
*/ | ||
close () { | ||
@@ -85,2 +95,5 @@ if (!this._cmdServerHTTP) return Promise.resolve() | ||
/** | ||
* @private | ||
*/ | ||
_onGetResponse (req, res) { | ||
@@ -99,2 +112,5 @@ const key = decodeURIComponent(req.params.key) | ||
/** | ||
* @private | ||
*/ | ||
_onSetResponse (req, res) { | ||
@@ -114,2 +130,8 @@ const key = decodeURIComponent(req.params.key) | ||
/** | ||
* Returns the configured server response for the given key | ||
* | ||
* @param {string} key | ||
* @return {string} response - JSON | ||
*/ | ||
getResponse (key) { | ||
@@ -119,2 +141,9 @@ return this._responses.get(key) | ||
/** | ||
* Sets the provided data as the server response for the given key. Note that | ||
* the data is converted to JSON. | ||
* | ||
* @param {string} key | ||
* @param {*} data | ||
*/ | ||
setResponse (key, data) { | ||
@@ -121,0 +150,0 @@ this._responses.set(key, JSON.stringify(data)) |
@@ -72,2 +72,8 @@ 'use strict' | ||
class REST2Server extends Server { | ||
/** | ||
* @param {Object} args | ||
* @param {number} args.apiPort | ||
* @param {number} args.cmdPort | ||
* @param {boolean} args.listen - if true, listen() is called automatically | ||
*/ | ||
constructor (args = { apiPort: 9999, cmdPort: 9998, listen: true }) { | ||
@@ -92,2 +98,5 @@ super(args) | ||
/** | ||
* @private | ||
*/ | ||
static _keysForRoute (req, routeKey) { | ||
@@ -116,2 +125,5 @@ const args = Object.assign( | ||
/** | ||
* @private | ||
*/ | ||
_generateRoute (type, route, routeKey) { | ||
@@ -144,2 +156,6 @@ this._apiServer[type](route, (req, res) => { | ||
/** | ||
* Starts the API server listening on the configured port. This is a no-op if | ||
* the server is already up | ||
*/ | ||
listen () { | ||
@@ -154,2 +170,7 @@ if (this._apiServerHTTP) return | ||
/** | ||
* Closes the API server if it is running; This is a no-op if it is not. | ||
* | ||
* @return {Promise} p - resolves/rejects on success/error | ||
*/ | ||
close () { | ||
@@ -171,2 +192,5 @@ return super.close().then(() => { | ||
/** | ||
* @private | ||
*/ | ||
_sendResponse (key, res) { | ||
@@ -173,0 +197,0 @@ if (!this._responses.has(key)) { |
@@ -53,2 +53,5 @@ 'use strict' | ||
/** | ||
* @private | ||
*/ | ||
_sendResponse (key, ws) { | ||
@@ -69,2 +72,5 @@ if (!this._responses.has(key)) { | ||
/** | ||
* @private | ||
*/ | ||
_prepareResponsePackets (key) { | ||
@@ -95,2 +101,6 @@ const res = this._responses.get(key) | ||
/** | ||
* Starts the API server listening on the configured port. This is a no-op if | ||
* the server is already up | ||
*/ | ||
listen () { | ||
@@ -112,2 +122,7 @@ if (this._wss) return | ||
/** | ||
* Closes the API server if it is running; This is a no-op if it is not. | ||
* | ||
* @return {Promise} p - resolves/rejects on success/error | ||
*/ | ||
close () { | ||
@@ -129,2 +144,9 @@ return super.close().then(() => { | ||
/** | ||
* 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. | ||
* | ||
* @param {string} eventName | ||
* @param {Function} cb | ||
*/ | ||
once (eventName, cb) { | ||
@@ -136,2 +158,5 @@ if (!this._wss) return | ||
/** | ||
* @private | ||
*/ | ||
_onConfigCommand (req, res) { | ||
@@ -155,2 +180,5 @@ let config | ||
/** | ||
* @private | ||
*/ | ||
_onSendCommand (req, res) { | ||
@@ -173,2 +201,7 @@ let packet | ||
/** | ||
* Sends the provided packet to all connected clients | ||
* | ||
* @param {*} packet - stringifed before being sent | ||
*/ | ||
send (packet) { | ||
@@ -180,2 +213,5 @@ const wsPacket = JSON.stringify(packet) | ||
/** | ||
* @private | ||
*/ | ||
_onConnection (ws) { | ||
@@ -189,2 +225,5 @@ this._clients.push(ws) | ||
/** | ||
* @private | ||
*/ | ||
_onClientMessage (ws, msgJSON) { | ||
@@ -218,2 +257,5 @@ const msg = JSON.parse(msgJSON) | ||
/** | ||
* @private | ||
*/ | ||
_handleAuthMessage (ws, msg) { | ||
@@ -228,2 +270,5 @@ this._sendResponse('auth.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleSubscribeMessage (ws, msg) { | ||
@@ -239,2 +284,5 @@ msg.event = 'subscribed' | ||
/** | ||
* @private | ||
*/ | ||
_handleNewOrder (ws, msg) { | ||
@@ -255,2 +303,5 @@ this._sendResponse('on.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleCancelOrder (ws, msg) { | ||
@@ -268,2 +319,5 @@ this._sendResponse('oc.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleCancelMultipleOrders (ws, msg) { | ||
@@ -273,2 +327,5 @@ this._sendResponse('oc_multi.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleOrderMultiOp (ws, msg) { | ||
@@ -278,2 +335,5 @@ this._sendResponse('ox_multi.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleCalc (ws, msg) { | ||
@@ -283,2 +343,5 @@ this._sendResponse('calc.res', ws) | ||
/** | ||
* @private | ||
*/ | ||
_handleUnknownMessagw (ws, msg) {} | ||
@@ -290,2 +353,3 @@ | ||
* @param {WebSocket} ws | ||
* @private | ||
*/ | ||
@@ -292,0 +356,0 @@ _syncClient (ws) { |
{ | ||
"name": "bfx-api-mock-srv", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Bitfinex API server mock library", | ||
@@ -13,3 +13,7 @@ "engines": { | ||
"unit": "NODE_ENV=test istanbul cover _mocha -- -R spec -b --recursive", | ||
"test-without-coverage": "NODE_ENV=test mocha -R spec -b --recursive" | ||
"test-without-coverage": "NODE_ENV=test mocha -R spec -b --recursive", | ||
"base_server_docs": "node_modules/jsdoc-to-markdown/bin/cli.js lib/server.js > docs/base_server.md", | ||
"rest2_docs": "node_modules/jsdoc-to-markdown/bin/cli.js lib/servers/rest2.js > docs/rest2.md", | ||
"ws2_docs": "node_modules/jsdoc-to-markdown/bin/cli.js lib/servers/ws2.js > docs/ws2.md", | ||
"docs": "npm run base_server_docs && npm run rest2_docs && npm run ws2_docs" | ||
}, | ||
@@ -39,4 +43,5 @@ "repository": { | ||
"babel-eslint": "^10.0.3", | ||
"bfx-api-node-rest": "git+http://github.com/bitfinexcom/bfx-api-node-rest.git#semver:^1.1.3", | ||
"bfx-api-node-rest": "^1.1.3", | ||
"istanbul": "^0.4.5", | ||
"jsdoc-to-markdown": "^5.0.1", | ||
"mocha": "^6.2.0", | ||
@@ -43,0 +48,0 @@ "request": "^2.88.0", |
@@ -1,12 +0,19 @@ | ||
# Bitfinex API mock server | ||
# Bitfinex API Mock Server | ||
This repo hosts mock servers for the WSv2 and RESTv2 Bitfinex APIs, and is intended for testing the Bitfinex API libraries. | ||
Example usage: | ||
### Features | ||
```js | ||
'use strict' | ||
* Provides realistic API responses for WSv2 | ||
* Provides configurable responses for RESTv2 | ||
* Allows for basic testing of API libraries without a live API connection | ||
process.env.DEBUG = 'bfx:*' | ||
### Installation | ||
```bash | ||
npm i --save bfx-api-mock-srv | ||
``` | ||
### Quickstart & Example | ||
```js | ||
const assert = require('assert') | ||
@@ -44,1 +51,16 @@ const debug = require('debug')('bfx:api:mock-srv:examples:endpoint-test') | ||
``` | ||
### Docs | ||
* [See `docs/base_server.md`](/docs/base_server.md) for API docs covering the base server | ||
* [See `docs/rest2.md`](/docs/rest2.md) for API docs convering the RESTv2 mock server | ||
* [See `docs/ws2.md`](/docs/ws2.md) for API docs convering the WSv2 mock server | ||
* [Refer to `examples/`](/examples) for executable examples | ||
### Contributing | ||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request |
Sorry, the diff of this file is not supported yet
46952
24.96%21
23.53%920
14.57%66
50%7
16.67%