diff --git a/exampleConfig.js b/exampleConfig.js index ce918ac..7da47eb 100644 --- a/exampleConfig.js +++ b/exampleConfig.js @@ -18,5 +18,8 @@ module.exports = { followRedirect: false, headers: { 'x-custom': 'headers' - } + }, + + //set this to 'true' to accept all GET and HEAD response codes as a success (not only 2XX) + allowAnyResponse: false }; diff --git a/lib/wait-on.js b/lib/wait-on.js index f0a2f8d..44a74b9 100644 --- a/lib/wait-on.js +++ b/lib/wait-on.js @@ -41,7 +41,8 @@ var WAIT_ON_SCHEMA = Joi.object().keys({ strictSSL: Joi.boolean(), followAllRedirects: Joi.boolean(), followRedirect: Joi.boolean(), - headers: Joi.object() + headers: Joi.object(), + allowAnyResponse: Joi.bool() }); /** @@ -274,11 +275,12 @@ function createHttp$(url, options) { ) .map(function (response) { // Why is response in array here? - var statusCode = response[0].statusCode; + var statusCode = response[0].statusCode; return { // request will handle redirects before returning - // anything but 2XX is a failure - val: (statusCode >= 200 && statusCode <= 299) ? + // anything but 2XX is a failure if options.allowAnyResponse set to true + // allowAnyResponse: still compare to 600 as sometimes 999 codes are returned even if the resource is not reachable + val: (options.allowAnyResponse && statusCode < 600 || statusCode >= 200 && statusCode <= 299) ? statusCode : -1 * statusCode, data: response[0] @@ -297,7 +299,7 @@ function createHttpGet$(url, options) { return { // request will handle redirects before returning // anything but 2XX is a failure - val: (statusCode >= 200 && statusCode <= 299) ? + val: (options.allowAnyResponse && statusCode < 600 || statusCode >= 200 && statusCode <= 299) ? statusCode : -1 * statusCode, data: response[0] diff --git a/test/api.mocha.js b/test/api.mocha.js index 8a7f6ad..2930724 100644 --- a/test/api.mocha.js +++ b/test/api.mocha.js @@ -233,6 +233,33 @@ describe('api', function () { }); }); + it('should succeed when an http resource returns 404 and allowAnyResponse is set', function (done) { + var opts = { + resources: [ + 'http://localhost:3002' + ], + timeout: 1000, + interval: 100, + window: 100, + allowAnyResponse: true + }; + + setTimeout(function () { + httpServer = http.createServer() + .on('request', function (req, res) { + res.statusCode = 404; + res.end('data'); + }); + httpServer.listen(3002, 'localhost'); + }, 300); + + waitOn(opts, function (err) { + expect(err).toNotExist(); + done(); + }); + }); + + // Error situations it('should timeout when all resources are not available and timout option is specified', function (done) { @@ -265,7 +292,7 @@ describe('api', function () { }); }); - it('should timeout when an http resource returns 404', function (done) { + it('should timeout when an http resource returns 404 and allowAnyResponse is not set', function (done) { var opts = { resources: [ 'http://localhost:3002' @@ -306,6 +333,23 @@ describe('api', function () { }); }); + it('should timeout when an http resource is not available even if allowAnyResponse is set', function (done) { + var opts = { + resources: [ + 'http://localhost:3010' + ], + timeout: 1000, + interval: 100, + window: 100, + allowAnyResponse: true + }; + + waitOn(opts, function (err) { + expect(err).toExist(); + done(); + }); + }); + it('should timeout when an http resource does not respond before httpTimeout', function (done) { var opts = { resources: [