From f4e99629f613c52df8c079086ee7fe0c7c235f4e Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 26 May 2017 13:02:02 +0200 Subject: [PATCH] Do not assert inside response, but pass error into callback Preferably we should put response outside of assert and change its callback signature. However, I don't think it is worth the effort right now. --- test/support/assert.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/test/support/assert.js b/test/support/assert.js index a53d6fbf..c5e22e93 100644 --- a/test/support/assert.js +++ b/test/support/assert.js @@ -126,22 +126,25 @@ assert.response = function(server, req, res, callback) { // Assert response body if (res.body) { var eql = res.body instanceof RegExp ? res.body.test(response.body) : res.body === response.body; - assert.ok( - eql, - colorize('[red]{Invalid response body.}\n' + + if (!eql) { + return callback(response, new Error(colorize( + '[red]{Invalid response body.}\n' + ' Expected: [green]{' + res.body + '}\n' + - ' Got: [red]{' + response.body + '}') - ); + ' Got: [red]{' + response.body + '}')) + ); + } } // Assert response status if (typeof status === 'number') { - assert.equal(response.statusCode, status, - colorize('[red]{Invalid response status code.}\n' + + if (response.statusCode != status) { + return callback(response, new Error(colorize( + '[red]{Invalid response status code.}\n' + ' Expected: [green]{' + status + '}\n' + ' Got: [red]{' + response.statusCode + '}\n' + - ' Body: ' + response.body) - ); + ' Body: ' + response.body)) + ); + } } // Assert response headers @@ -152,11 +155,13 @@ assert.response = function(server, req, res, callback) { actual = response.headers[name.toLowerCase()], expected = res.headers[name], headerEql = expected instanceof RegExp ? expected.test(actual) : expected === actual; - assert.ok(headerEql, - colorize('Invalid response header [bold]{' + name + '}.\n' + + if (!headerEql) { + return callback(response, new Error(colorize( + 'Invalid response header [bold]{' + name + '}.\n' + ' Expected: [green]{' + expected + '}\n' + - ' Got: [red]{' + actual + '}') - ); + ' Got: [red]{' + actual + '}')) + ); + } } }