req.emit('data', {sampleData: 'wrongOrRightSampleDataHere'}) should do this. When creating an instance of an http object or, therefore, req make sure you create a new instance and no other test receives this event.
To be more complete ...
var assert = require('assert') function test() { var hasBeenCalledAtLeastOnce = false var userInput = ""; // req must be defined somewhere though req.on("data", function(data){ userInput += data; if(hasBeenCalledAtLeastOnce) { assert.equal(userInput, "HelloWorld", "userInput is in fact 'HelloWorld'") } hasBeenCalledAtLeastOnce = true }); req.emit('data', "Hello") req.emit('data', "World") } test()
source share