用nock模拟一个开放的networking套接字

我正在尝试使用nock对Oanda交易API编写的代码进行反向testing。 为此,我需要模拟stream媒体价格API(请参阅http://developer.oanda.com/rest-practice/streaming/上的stream媒体stream量)。 但是,看起来nock只能让你回复一个单一的回复,即使回应是一个stream。 有没有办法将数以千计的价格事件stream作为个人对单个请求的回应?

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}}] }) 

根据这个Nock文档,你可以在你的回复中返回一个ReadStream。

我使用了stream-spigot npm包来创build下面的例子(用于模拟Marathon事件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);