should-http: HTTP Assertions for should.js
should-http extends the `should.js` assertion library with specific methods for validating Node.js HTTP `IncomingMessage` objects. Primarily used for testing HTTP requests and responses, it enables fluent assertions on status codes, headers (e.g., `Content-Length`), and content types (e.g., `application/json`, `text/html`). Currently at version 0.1.1, this package appears to be abandoned, with no active development or maintenance. Its approach of patching the global `should` instance, rather than exporting specific utilities, ties it closely to older `should.js` usage patterns and makes it less suitable for modern module development (ESM) where global side-effects are generally avoided. It focuses specifically on the standard Node.js `http` module's request and response objects, distinguishing it from broader HTTP client assertion libraries.
Common errors
-
Error: should.js: assertion for 'status' was not added
cause The `should-http` module was required before `should.js` was initialized, or `should.js` itself was not required/available in the environment.fixEnsure `require('should');` is called *before* `require('should-http');` to guarantee the `should` instance is available for patching. -
TypeError: Cannot read properties of undefined (reading 'should')
cause The `should` library was not correctly loaded or applied, meaning `Object.prototype.should` (or `should(obj)`) is not available, and thus `res.should` fails.fixVerify that `should` is properly installed (`npm install should`) and required at the top of your test file, as per `should.js`'s standard usage. If using a test runner, ensure `should` is loaded correctly (e.g., via Mocha's `-r` flag). -
TypeError: 'res.should.be.json' is not a function
cause The assertion `json` is a method and requires parentheses `()` to be called, even though it takes no arguments.fixChange `res.should.be.json` to `res.should.be.json()` to correctly invoke the assertion method. The same applies to `.html()`.
Warnings
- breaking This package is largely unmaintained and may not be compatible with newer versions of Node.js or `should.js` beyond the specified peer dependency (>= 4.x). Using older, unmaintained packages can introduce security vulnerabilities or unexpected behavior in modern environments.
- gotcha should-http works by patching the global `should` instance (and by extension, potentially `Object.prototype` depending on `should.js` configuration). This can lead to global namespace pollution and conflicts with other libraries or unexpected side effects, which is a deprecated pattern in modern JavaScript development.
- gotcha This package is designed for CommonJS (`require`). While Node.js has robust interoperability, using a CJS module that relies on global side-effects within an ESM project might introduce subtle issues or require specific configuration (e.g., `--experimental-json-modules` or using bundlers).
Install
-
npm install should-http -
yarn add should-http -
pnpm add should-http
Imports
- * (side-effect import)
import shouldHttp from 'should-http';
require('should-http');
Quickstart
const should = require('should');
require('should-http');
// Simulate an HTTP response object
const mockResponse = {
statusCode: 200,
headers: {
'content-type': 'application/json; charset=utf-8',
'content-length': '123'
},
body: '{ "key": "value" }'
};
// Assertions using should-http
try {
mockResponse.should.have.status(200);
console.log('✔ Status code is 200');
mockResponse.should.have.header('content-length');
console.log('✔ Has content-length header');
mockResponse.should.have.header('Content-Length', '123');
console.log('✔ Content-Length header matches value');
mockResponse.should.be.json();
console.log('✔ Content-Type is application/json');
// Example of a failing assertion (uncomment to see it fail)
// mockResponse.should.have.status(404);
} catch (e) {
console.error('Assertion failed:', e.message);
}
// Example for .html (if content-type were different)
const htmlResponse = {
statusCode: 200,
headers: {
'content-type': 'text/html; charset=utf-8'
}
};
try {
htmlResponse.should.be.html();
console.log('✔ Content-Type is text/html');
} catch (e) {
console.error('HTML assertion failed:', e.message);
}