diff --git a/README.md b/README.md index 97727f3..4a64fd7 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,9 @@ var opts = { headers: { 'x-custom': 'headers', }, + validateStatus: function (status) { + return status >= 200 && status < 300; // default if not provided + }, }; // Usage with callback function diff --git a/lib/wait-on.js b/lib/wait-on.js index a5a86b1..5c014dc 100644 --- a/lib/wait-on.js +++ b/lib/wait-on.js @@ -29,6 +29,7 @@ const WAIT_ON_SCHEMA = Joi.object({ reverse: Joi.boolean().default(false), simultaneous: Joi.number().integer().min(1).default(Infinity), timeout: Joi.number().integer().min(0).default(Infinity), + validateStatus: Joi.function(), verbose: Joi.boolean().default(false), window: Joi.number().integer().min(0).default(750), tcpTimeout: Joi.number().integer().min(0).default(300), @@ -271,7 +272,7 @@ function createHTTP$({ validatedOpts, output }, resource) { : { url }; const socketPathDesc = urlSocketOptions.socketPath ? `socketPath:${urlSocketOptions.socketPath}` : ''; const httpOptions = { - ...pick(['auth', 'headers'], validatedOpts), + ...pick(['auth', 'headers', 'validateStatus'], validatedOpts), httpsAgent: new https.Agent({ rejectUnauthorized, ...pick(['ca', 'cert', 'key', 'passphrase'], validatedOpts), diff --git a/test/api.mocha.js b/test/api.mocha.js index 8c1189e..963466e 100644 --- a/test/api.mocha.js +++ b/test/api.mocha.js @@ -81,6 +81,28 @@ describe('api', function () { }); }); + it('should succeed when custom validateStatus fn is provided http resource returns 401', function (done) { + var opts = { + resources: ['http://localhost:3000'], + validateStatus: function (status) { + return status === 401 || (status >= 200 && status < 300); + }, + }; + + setTimeout(function () { + httpServer = http.createServer().on('request', function (req, res) { + res.statusCode = 401; + res.end('Not authorized'); + }); + httpServer.listen(3000, 'localhost'); + }, 300); + + waitOn(opts, function (err) { + expect(err).toNotExist(); + done(); + }); + }); + it('should succeed when http resource become available later via redirect', function (done) { var opts = { // followRedirect: true // default is true