http-assert
!NPM Versionnpm-version-imagenpm-url
!NPM Downloadsnpm-downloads-imagenpm-url
!Node.js Versionnode-version-imagenode-version-url
!Build Statusci-imageci-url
!Test Coveragecoveralls-imagecoveralls-urlAssert with status codes. Like ctx.throw() in Koa, but with a guard.
Install
This is a Node.js module available through the npm registry. Installation is done using thenpm install
command:$ npm install http-assert
Example
var assert = require('http-assert')
var ok = require('assert')
var username = 'foobar' // username from request
try {
assert(username === 'fjodor', 401, 'authentication failed')
} catch (err) {
ok(err.status === 401)
ok(err.message === 'authentication failed')
ok(err.expose)
}
API
The API of this module is intended to be similar to the Node.jsassert
module.Each function will throw an instance of
HttpError
from
the http-errors
module
when the assertion fails.assert(value, status, message, properties)
Tests ifvalue
is truthy. If value
is not truthy, an HttpError
is thrown that is constructed with the given status
, message
,
and properties
.assert.deepEqual(a, b, status, message, properties)
Tests for deep equality betweena
and b
. Primitive values are
compared with the Abstract Equality Comparison (==
). If a
and b
are not equal, an HttpError
is thrown that is constructed with the
given status
, message
, and properties
.assert.equal(a, b, status, message, properties)
Tests shallow, coercive equality betweena
and b
using the Abstract
Equality Comparison (==
). If a
and b
are not equal, an HttpError
is thrown that is constructed with the given status
, message
,
and properties
.assert.fail(status, message, properties)
Always throws anHttpError
that is constructed with the given status
,
message
, and properties
.assert.notDeepEqual(a, b, status, message, properties)
Tests for deep equality betweena
and b
. Primitive values are
compared with the Abstract Equality Comparison (==
). If a
and b
are equal, an HttpError
is thrown that is constructed with the given
status
, message
, and properties
.assert.notEqual(a, b, status, message, properties)
Tests shallow, coercive equality betweena
and b
using the Abstract
Equality Comparison (==
). If a
and b
are equal, an HttpError
is
thrown that is constructed with the given status
, message
, and
properties
.assert.notStrictEqual(a, b, status, message, properties)
Tests strict equality betweena
and b
as determined by the SameValue
Comparison (===
). If a
and b
are equal, an HttpError
is thrown
that is constructed with the given status
, message
, and properties
.assert.ok(value, status, message, properties)
Tests ifvalue
is truthy. If value
is not truthy, an HttpError
is thrown that is constructed with the given status
, message
,
and properties
.assert.strictEqual(a, b, status, message, properties)
Tests strict equality betweena
and b
as determined by the SameValue
Comparison (===
). If a
and b
are not equal, an HttpError
is thrown that is constructed with the given status
, message
,
and properties
.