I am trying to use nock to run backtests on code written against Oanda's trading API. To do that, I need to simulate the streaming price API (see Rates Streaming at http://developer.oanda.com/rest-practice/streaming/). However, it seems like nock only lets you respond with a single reply, even though the response is a stream. Is there any way to send a stream of thousands of price events as individual responses to a single request?
var scopeStream = nock('https://stream-fxpractice.oanda.com')
.persist()
.filteringPath(function(path) {
return '/stream';
})
.get('/stream')
.reply(function(uri, requestBody) {
return [200, {"tick":{"instrument":"AUD_CAD","time":"2014-01-30T20:47:08.066398Z","bid":0.98114,"ask":0.98139}}]
})
According to this Nock documentation you can return a ReadStream in your reply.
I used the stream-spigot npm package to come up with the following example (used to simulate a Marathon event stream):
const nock = require('nock');
const EventSource = require('eventsource');
const spigot = require('stream-spigot');
let i = 0;
nock('http://marathon.com')
.get('/events')
.reply(200, (uri, req) => {
// reply with a stream
return spigot({objectMode: true}, function() {
const self = this;
if (++i < 5)
setTimeout(() => this.push(`id: ${i}\ndata: foo\n\n`), 1000);
})
});
const es = new EventSource('http://marathon.com/events');
es.onmessage = m => console.log(m);
es.onerror = e => console.log(e);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With