环境:
NodeJS 8.1.2
axios 0.16.2
axios-mock-adapter 1.9.0
测试 JSON-RPC 端点,我能做这样的事情:
const mockHttpClient = new MockAdapter(axios, { delayResponse: 50 })
mockHttpClient.onPost().reply((config) => { // Capture all POST methods
const dataObj = JSON.parse(config.data) // Example data: '{"jsonrpc":"2.0","method":"getProduct","params":[123],"id":0}'
if (dataObj.method === 'getProduct') { // Recognised method, provide mock response override
return [200, { jsonrpc: '2.0', id: 0, result: { productId: 123, productName: 'Lorem' } }]
}
// TODO: PassThrough for all non-recognised methods
})
mockHttpClient.onAny().passThrough() // Allow pass through on anything that's not a POST method