Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
http-assert
Advanced tools
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.
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');
});
The 'assert' module is a simple assertion library that comes with Node.js. It provides basic assertion tests and is used primarily for testing. Unlike 'http-assert', it does not automatically map assertions to HTTP error codes.
Chai-http is an assertion library that can be used with the Chai assertion library for HTTP server testing. It provides a more fluent API for testing HTTP servers and includes assertions specifically designed for HTTP interactions. It is more feature-rich compared to 'http-assert' but also more complex.
Supertest is a library for testing Node.js HTTP servers. It provides a high-level abstraction for testing HTTP, while allowing you to assert the HTTP response. It is not an assertion library per se, but it integrates well with assertion libraries to provide a full testing solution.
#http-assert
Assert with status codes. Like ctx.throw() in Koa, but with a guard.
##Example
var assert = require('http-assert');
var ok = require('assert');
try {
assert(username == 'fjodor', 401, 'authentication failed');
} catch (err) {
ok(err.status == 401);
ok(err.message == 'authentication failed');
ok(err.expose);
}
##Licence MIT
FAQs
assert with status codes
We found that http-assert demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.