Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion exampleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
12 changes: 7 additions & 5 deletions lib/wait-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});

/**
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
46 changes: 45 additions & 1 deletion test/api.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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: [
Expand Down