Skip to content

Commit

Permalink
Handle server not-running & superagent system errors (#348)
Browse files Browse the repository at this point in the history
* Handle server not-running & network errors

Handles server not-running (ECONNREFUSED) and other system errors from
superagent

* Test errors corrected
  • Loading branch information
santanu-biswas authored and mikelax committed Sep 27, 2016
1 parent 054ad57 commit df45dd7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.idea
*.log
18 changes: 18 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ Test.prototype.assert = function(resError, res, fn) {
var error;
var i;

// check for unexpected network errors or server not running/reachable errors
// when there is no response and superagent sends back a System Error
// do not check further for other asserts, if any, in such case
// https://nodejs.org/api/errors.html#errors_common_system_errors
var sysErrors = {
ECONNREFUSED: 'Connection refused',
ECONNRESET: 'Connection reset by peer',
EPIPE: 'Broken pipe',
ETIMEDOUT: 'Operation timed out'
};

if (!res && resError && (resError instanceof Error) && (resError.syscall === 'connect')
&& (Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0)) {
error = new Error(resError.code + ': ' + sysErrors[resError.code]);
fn.call(this, error, null);
return;
}

// asserts
for (i = 0; i < this._asserts.length && !error; ++i) {
error = this._assertFunction(this._asserts[i], res);
Expand Down
14 changes: 14 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ describe('request(app)', function() {
});
});

describe('.expect(status)', function () {
it('should handle connection error', function (done) {
var req = request.agent('http://localhost:1234');

req
.get('/')
.expect(200)
.end(function (err, res) {
err.message.should.equal('ECONNREFUSED: Connection refused');
done();
});
});
});

describe('.expect(status)', function () {
it('should assert only status', function (done) {
var app = express();
Expand Down

0 comments on commit df45dd7

Please sign in to comment.