[英]Is it possible to apply passThrough() within a mock reply using axios-mock-adapter?
Environment: 环境:
NodeJS 8.1.2
axios 0.16.2
axios-mock-adapter 1.9.0
Testing a JSON-RPC endpoint, am I able to do something like this: 测试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
You can do that by passing the call to the original adapter within the reply
callback : 您可以通过将调用传递给
reply
回调中的原始适配器来实现:
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' } }]
}
// PassThrough for all non-recognised methods
return mockHttpClient.originalAdapter(config);
})
It's essentially what passThrough()
does. 本质上是
passThrough()
所做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.