2015-07-19 25 views
2

我想用nock在Oanda的交易API编写的代码上运行backtest。要做到这一点,我需要模拟流媒体价格API(请参阅价格流http://developer.oanda.com/rest-practice/streaming/)。然而,看起来nock只允许你用单个回复做出响应,即使响应是一个流。有没有办法将数以千计的价格事件作为个人对单个请求的回应发送出去?用nock模拟一个开放的网络套接字

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

蟋蟀:-(没有想法? – greymatter

回答

1

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

我用stream-spigot NPM包拿出下面的例子(用来模拟一个马拉松事件流):

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);