Socket
Socket
Sign inDemoInstall

http-assert

Package Overview
Dependencies
7
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    http-assert

assert with status codes


Version published
Weekly downloads
1.4M
decreased by-1.04%
Maintainers
3
Install size
96.7 kB
Created
Weekly downloads
 

Package description

What is http-assert?

The http-assert npm package is a simple assertion library for HTTP server testing. It allows developers to assert certain conditions and automatically throw HTTP errors when those conditions are not met. This can help to streamline error handling in HTTP applications by providing a declarative way to validate requests and ensure that the server responds with the correct status codes and messages when something goes wrong.

What are http-assert's main functionalities?

Asserting conditions with HTTP error codes

This feature allows you to assert a condition and throw an HTTP error with a specific status code and message if the condition is false. In the code sample, if the user is not found, a 404 error with the message 'User not found' is thrown.

const assert = require('http-assert');

// Example usage in an Express route handler
app.get('/user/:id', (req, res, next) => {
  const user = getUserById(req.params.id);
  assert(user, 404, 'User not found');
  res.send(user);
});

Custom error properties

This feature allows you to add custom properties to the error object. In the code sample, if authentication fails, a 401 error is thrown with additional information about the user that attempted to log in.

const assert = require('http-assert');

// Example usage with custom error properties
app.post('/login', (req, res, next) => {
  const user = authenticate(req.body.username, req.body.password);
  assert(user, 401, 'Authentication failed', { user: req.body.username });
  res.send('Logged in successfully');
});

Other packages similar to http-assert

Readme

Source

http-assert

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Assert 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 the npm 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.js assert module.

Each function will throw an instance of HttpError from the http-errors module when the assertion fails.

assert(value, [status], [message], [properties])

Tests if value 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 between a 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 between a 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 an HttpError that is constructed with the given status, message, and properties.

assert.notDeepEqual(a, b, [status], [message], [properties])

Tests for deep equality between a 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 between a 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 between a 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 if value 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 between a 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.

Licence

MIT

Keywords

FAQs

Last updated on 25 Aug 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc