|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const express = require('express') |
| 4 | +const Bluebird = require('bluebird') |
| 5 | +const bodyParser = require('body-parser') |
| 6 | +const _keys = require('lodash/keys') |
| 7 | +const _isUndefined = require('lodash/isUndefined') |
| 8 | +const debug = require('debug')('bfx:api-mock-srv:rest2') |
| 9 | + |
| 10 | +const MockServer = require('../server') |
| 11 | +const RESTV2_MOCK_METHODS = require('./methods') |
| 12 | + |
| 13 | +/** |
| 14 | + * REST v2 API server mock |
| 15 | + * |
| 16 | + * Exposes the same routes as the real API, and maps them to a response table. |
| 17 | + * Multiple potential responses can be defined for endpoints with arguments, |
| 18 | + * with the best match sent to clients on request. |
| 19 | + * |
| 20 | + * i.e. If the following responses are configured: |
| 21 | + * - `orders.tBTCUSD: [42]` |
| 22 | + * - `orders: [41]` |
| 23 | + * |
| 24 | + * A `GET` on `/v2/auth/r/orders/tBTCUSD/hist` would return `[42]`, but a query |
| 25 | + * for a different symbol (`tETHUSD`) would return `[41]`. |
| 26 | + * |
| 27 | + * @class |
| 28 | + * @memberof module:bfx-api-mock-srv |
| 29 | + * @augments module:bfx-api-mock-srv.MockServer |
| 30 | + */ |
| 31 | +class MockRESTv2Server extends MockServer { |
| 32 | + /** |
| 33 | + * @param {object} [args={}] - args |
| 34 | + * @param {number} [args.apiPort=9999] - API port number |
| 35 | + * @param {number} [args.cmdPort=9998] - command port number |
| 36 | + * @param {boolean} [args.listen=true] - enables auto listen() |
| 37 | + */ |
| 38 | + constructor (args = {}) { |
| 39 | + const { apiPort = 9999, cmdPort = 9998, listen = true } = args |
| 40 | + |
| 41 | + super({ cmdPort }) |
| 42 | + |
| 43 | + this._apiServer = express() |
| 44 | + this._apiServer.use(bodyParser.json()) |
| 45 | + this._apiServerHTTP = null |
| 46 | + this._apiPort = apiPort |
| 47 | + |
| 48 | + _keys(RESTV2_MOCK_METHODS).forEach((route) => { |
| 49 | + const auth = route.split('/')[2] === 'auth' |
| 50 | + |
| 51 | + this._generateRoute( |
| 52 | + auth ? 'post' : 'get', route, RESTV2_MOCK_METHODS[route] |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + if (listen) { |
| 57 | + this.listen() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @static |
| 63 | + * |
| 64 | + * @param {express.Request} req - request |
| 65 | + * @param {string} routeKey - key |
| 66 | + * @returns {string[]} keys |
| 67 | + */ |
| 68 | + static keysForRoute (req, routeKey) { |
| 69 | + const args = Object.assign( |
| 70 | + {}, req.params || {}, req.query || {}, req.body || {} |
| 71 | + ) |
| 72 | + |
| 73 | + // Replace {tokens} with data values, where possible |
| 74 | + let tokens = routeKey.split('.').map((token) => { |
| 75 | + if (token[0] !== '{' || token[token.length - 1] !== '}') { |
| 76 | + return token |
| 77 | + } |
| 78 | + |
| 79 | + const val = args[token.substring(1, token.length - 1)] |
| 80 | + |
| 81 | + return _isUndefined(val) ? '' : val |
| 82 | + }) |
| 83 | + |
| 84 | + const keys = [] |
| 85 | + |
| 86 | + while (tokens.length > 0) { |
| 87 | + keys.push(tokens.join('.')) |
| 88 | + tokens = tokens.splice(0, tokens.length - 1) |
| 89 | + } |
| 90 | + |
| 91 | + return keys |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @private |
| 96 | + * |
| 97 | + * @param {string} type - 'post' or 'get' |
| 98 | + * @param {string} route - route |
| 99 | + * @param {string} routeKey - key |
| 100 | + */ |
| 101 | + _generateRoute (type, route, routeKey) { |
| 102 | + this._apiServer[type](route, (req, res) => { |
| 103 | + const keys = MockRESTv2Server.keysForRoute(req, routeKey) |
| 104 | + |
| 105 | + // Check keys in order of token qty |
| 106 | + for (let i = 0; i < keys.length; i++) { |
| 107 | + if (this._responses.has(keys[i])) { |
| 108 | + const response = this._responses.get(keys[i]) |
| 109 | + |
| 110 | + if (!response) continue // could be null |
| 111 | + |
| 112 | + try { |
| 113 | + return res.json(JSON.parse(response)) |
| 114 | + } catch (err) { |
| 115 | + return res.status(500).json({ |
| 116 | + error: 'bad response json' |
| 117 | + }) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return res.status(404).json({ |
| 123 | + error: 'unknown arguments', |
| 124 | + keys |
| 125 | + }) |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Starts the API server listening on the configured port. This is a no-op if |
| 131 | + * the server is already up |
| 132 | + */ |
| 133 | + listen () { |
| 134 | + if (this._apiServerHTTP) { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + super.listen() |
| 139 | + this._apiServerHTTP = this._apiServer.listen(this._apiPort) |
| 140 | + |
| 141 | + debug('rest2 api server listening on port %d', this._apiPort) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Closes the API server if it is running; This is a no-op if it is not. |
| 146 | + * |
| 147 | + * @async |
| 148 | + * @returns {Promise} p |
| 149 | + */ |
| 150 | + async close () { |
| 151 | + await super.close() |
| 152 | + |
| 153 | + if (!this._apiServerHTTP) { |
| 154 | + return null |
| 155 | + } |
| 156 | + |
| 157 | + return new Bluebird((resolve, reject) => { |
| 158 | + this._apiServerHTTP.close((err) => { |
| 159 | + if (err) { |
| 160 | + reject(err) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + this._apiServerHTTP = null |
| 165 | + debug('rest2 api server closed') |
| 166 | + resolve() |
| 167 | + }) |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +module.exports = MockRESTv2Server |
0 commit comments