title: How to mock response from http.request (nodejs, jasmine, sinon)
body: <p>I've written a small <code>node</code> module that makes a <code>http</code> request and I am having trouble testing it.<br>
  The code in question looks like this:</p>
  
  <pre><code>module.exports = (function () {
      var http = require("http"),
          Promise = require("promise");
  
      var send = function send(requestOptions, requestBody) {
          return new Promise(function (resolve, reject) {
  
              http.request(requestOptions, function (response) {
                  var responseChunks = [];
  
                  response.on('data', function (chunk) {
                      responseChunks.push(chunk);
                  });
  
                  response.on('end', function () {
                      resolve(Buffer.concat(responseChunks));
                  });
  
                  response.on('error', function (e) {
                      reject(e);
                  });
              }).end(requestBody);
          });
      };
  
      return {
          send: send
      }
  </code></pre>
  
  <p>and I am trying to test my <code>send</code> method, in particular the callback function that is invoked by <code>http.request</code>.<br>
  I <em>think</em> what I need to do is to mock or stub the <code>response</code> object from <code>http.request</code> so that I can then test the execution of the callback function. But I can't figure out how to do that.</p>
  
  <p>If it's of any relevance I am using <code>node</code> v4.1, <code>jasmine</code> v2.3, and <code>sinon</code> v1.17</p>