Skip to content

Commit

Permalink
add optional validateStatus fn property
Browse files Browse the repository at this point in the history
If `validateStatus` property is provided in the
config and set to a function, it will use this to
validate whether a status is valid.

The default validateStatus if not provided is

```js
validateStatus: function (status) {
  return status >= 200 && status < 300;
}
```

To also allow a 401 unauthorized as a valid status

```js
validateStatus: function (status) {
  return status === 401 || (status >= 200 && status < 300);
}
```
  • Loading branch information
jeffbski committed May 5, 2020
1 parent e5ac27d commit e547fa7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/wait-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
22 changes: 22 additions & 0 deletions test/api.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e547fa7

Please sign in to comment.