I have a Node application that connects to the Twitter REST and streaming APIs. In order to test code that makes requests to the REST API, I can use Nock to intercept the HTTP request and return mock data like so:
var nock = require('nock')
var mockData = [...]
nock('https://api.twitter.com/1.1')
.get('/search/tweets.json')
.reply(200, mockData)
My application also connects to the streaming API endpoint statuses/filter and performs some analysis on tweets received via the streaming API. In Nock's README it states that you can pass a function to .query()
that returns a stream, however I haven't been able to get this to work.
How would I return mock data from requests to this endpoint using Nock (or another library if necessary)? I'd ideally like to be able to send tweets to the stream in my tests whenever I need to, for example:
it('should correctly process tweets coming in on the streaming API', () => {
var mockTweet = {...}
sendMockTweetToStream(mockTweet)
...verify that the mock tweet was received and processed
})