2015-07-19 24 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);