Skip to content

Commit

Permalink
more tests to restore coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Feb 25, 2020
1 parent d41664e commit 16de285
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/mockHttp.js
Expand Up @@ -121,9 +121,10 @@ function mockRequest( httpHandler, defaultOptions ) {

// callback is optional, but http seems to always pass one in
// if (!callback) callback = _noop;
req.socket = stream.Writable ? new stream.Writable() : new stream.Stream();
// todo: node-v0.8 does not have stream.Writable
req.socket = new stream.Writable();
req.socket.setTimeout = _noop;
req.socket.destroy = req.socket.destroy || req.abort;
req.socket.destroy = req.abort; // mock only, do not actually destroy sockets

// preserve the request options, we might need them later
req._options = options;
Expand Down Expand Up @@ -232,8 +233,8 @@ function composeUrl( options ) {
}
**/

// makeError from qibl 1.5.0-pre
// makeError adapted from qibl 1.5.0-pre
function makeError( code, message, baseFunc ) {
var err = typeof message === 'object' ? message : (err = new Error(message), Error.captureStackTrace(err, baseFunc), err);
var err = (err = new Error(message), Error.captureStackTrace(err, baseFunc), err);
return (err.code = code, err);
}
29 changes: 29 additions & 0 deletions test/test-mockHttp.js
Expand Up @@ -222,5 +222,34 @@ module.exports = {
t.throws(function(){ http.request({ protocol: 'http', host: 'somehost', path: '/some/path', auth: 1234 }, function(res) {}) });
t.done();
},

'abort should emit an error': function(t) {
qmock.mockHttp(function() {});
var req = http.request('http://somehost/some/path');
req.on('error', function(err) {
t.ok(err);
t.equal(err.code, 'ECONNRESET');
t.contains(err.message, 'socket hang up');
t.done();
})
req.end();
req.abort();
},

'socket.destroy should emit an error and stop delivering writes': function(t) {
qmock.mockHttp(function(req, res) {
// destroyed connections should not deliver writes
req.on('_mockWrite', function() { t.fail() });
})
var req = http.request('http://somehost/some/path');
req.on('error', function(err) {
t.ok(err);
t.ok(err.code);
setTimeout(t.done, 40);
})
req.socket.destroy();
req.write('');
req.end();
},
},
};

0 comments on commit 16de285

Please sign in to comment.