Skip to content

Commit

Permalink
Emit done on Hock when all assertions pass
Browse files Browse the repository at this point in the history
This is useful when you want to test requests made out of bounds (for
example, when batching logging requests).
  • Loading branch information
mmalecki committed Jul 9, 2018
1 parent 553620c commit 6a65814
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/hock.js
Expand Up @@ -3,6 +3,8 @@
var qs = require('querystring'),
url_ = require('url'),
http = require('http'),
util = require('util'),
events = require('events').EventEmitter,
Request = require('./request'),
deepEqual = require('deep-equal'),
urlEqual = require('url-equal');
Expand All @@ -24,7 +26,9 @@ var Hock = module.exports = function (options) {
this._throwOnUnmatched = (typeof options.throwOnUnmatched === 'boolean' ? options.throwOnUnmatched : true);
this._assertions = [];
this.handler = Hock.prototype.handler.bind(this);
events.EventEmitter.call(this);
};
util.inherits(Hock, events.EventEmitter);

/**
* Hock.enqueue
Expand Down Expand Up @@ -357,14 +361,9 @@ Hock.prototype.handler = function (req, res) {

req.on('end', function () {

for (var i = 0; i < self._assertions.length; i++) {
if (self._assertions[i].isMatch(req)) {
matchIndex = i;
break;
}
}
const matchIndex = self._assertions.findIndex(assertion => assertion.isMatch(req));

if (matchIndex === null) {
if (matchIndex === -1) {
if (self._throwOnUnmatched) {
throw new Error('No Match For: ' + req.method + ' ' + req.url);
}
Expand All @@ -380,6 +379,7 @@ Hock.prototype.handler = function (req, res) {
if (self._assertions[matchIndex].sendResponse(res)) {
self._assertions.splice(matchIndex, 1)[0];
}
if (self._assertions.length === 0) self.emit('done');
}
});
};
Expand Down

0 comments on commit 6a65814

Please sign in to comment.