Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
http-assert-plus
Advanced tools
Readme
More assertions with status codes.
const assert = require('http-assert-plus');
const username = 'jdrydn';
assert(username === 'someimportantcompany', 403, 'Authorization failed', {
code: 'NOT_AUTHORIZED',
username,
});
// Error: Authorization failed
// at createErr (http-assert-plus/README.md:13:7) {
// code: 'NOT_AUTHORIZED',
// statusCode: 403,
// status: 403,
// statusText: 'Forbidden',
// username: 'jdrydn'
// }
$ npm install http-assert-plus
This API matches the built-in assert
module, and builds upon the success of http-assert
, with a few differences:
Error
when the assertion fails.assert(value, [status], [message], [props])
Tests if value
is truthy, and throws an Error
if falsey.
assert.ok(value, [status], [message], [props])
Alias for above, tests if value
is truthy, and throws an Error
if falsey.
assert.fail([status], [message], [props])
Always throws an Error
with the provided status/message/props.
assert.equal(a, b, [status], [message], [props])
Tests shallow, coercive equality between a
& b
using ==
.
assert.notEqual(a, b, [status], [message], [props])
Tests shallow, coercive inequality between a
& b
using !=
.
assert.strictEqual(a, b, [status], [message], [props])
Tests strict equality between a
& b
using ===
.
assert.notStrictEqual(a, b, [status], [message], [props])
Tests strict inequality between a
& b
using !==
.
assert.includes(a, b, [status], [message], [props])
Tests whether a
includes b
- where a
has a method call includes
.
assert.notIncludes(a, b, [status], [message], [props])
Tests whether a
does not include b
- where a
has a method includes
.
You can optionally create your own instance of http-assert-plus
, useful if you want to add your own methods for common assertions:
const assert = require('http-assert-plus');
ctx.assert = assert.create();
ctx.assert.isAuthenticated = (props = null) => {
const { isAuthenticated } = ctx.state;
assert(isAuthenticated, 401, 'You are not authenticated', {
code: 'NOT_AUTHENTICATED',
...props,
});
};
// Later on:
ctx.assert.isAuthenticated({ action: 'EditUser' });
// Error: You are not authenticated
// at isAuthenticated (http-assert-plus/README.md:78:9) {
// code: 'NOT_AUTHENTICATED',
// statusCode: 401,
// status: 401,
// statusText: 'Unauthorized',
// username: 'jdrydn'
// }
If you're looking for deep equality checks, check out deep-equal
:
const assert = require('http-assert-plus');
const deepEqual = require('deep-equal');
assert(deepEqual(a, b), 400, 'These two are not entirely equal');
assert(deepEqual(a, b, { strict: true }), 400, 'These two are not entirely equal');
// Or create your own http-assert-plus instance:
req.assert = assert.create();
req.assert.deepEqual = (a, b, ...args) => assert(deepEqual(a, b), ...args);
req.assert.strictDeepEqual = (a, b, ...args) => assert(deepEqual(a, b, { strict: true }), ...args);
req.assert.deepEqual([ 1, 2, 3 ], [ '1', 2, 3.0 ], 400, 'Array does not equal');
req.assert.strictDeepEqual([ 1, 2, 3 ], [ '1', 2, 3.0 ], 400, 'Array does not strict-equal');
// Error: Array does not strict-equal
// at REPL:1:27 (http-assert-plus/README.md:113:27) {
// statusCode: 400,
// status: 400,
// statusText: 'Bad Request'
// }
Yes! Not all browsers support Error.captureStackTrace
so this library checks if it is present in the current environment - if it isn't available the only behaviour you'll likely want to change is to pre-construct Error
arguments to preserve a proper stack trace, like so:
const { origin } = window.location;
assert(origin.startsWith('https://'), new Error('Expected origin to start with https://'));
FAQs
More assertions with status codes
We found that http-assert-plus demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).