Mock HTTP Response Stream

4.0.2 · active · verified Tue Apr 21

Responselike provides a streamable object designed to mimic a Node.js HTTP response stream (`http.IncomingMessage`). This utility is primarily used for mocking HTTP responses in testing scenarios or for reformatting cached data to be consumed by code expecting a standard HTTP response. The current stable version is 4.0.2. The package is actively maintained by Sindre Sorhus, with releases typically occurring as needed for bug fixes or Node.js version updates, as seen with the recent v4.0.0 and v4.0.1/v4.0.2 updates. Its key differentiator is its focus on accurately replicating the streamable nature and properties of a native HTTP response, making it suitable for integrations where stream-based consumption is expected, unlike simpler object mocks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a mock HTTP response, accessing its properties, and piping its body as a stream.

import Response from 'responselike';
import { Writable } from 'stream';

class TestStream extends Writable {
  _write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
}

const response = new Response({
	statusCode: 200,
	headers: {
		'content-type': 'text/plain',
		'x-custom-header': 'value'
	},
	body: Buffer.from('Hello, World! This is a mocked response body.'),
	url: 'https://example.com/api/data'
});

console.log(`Status Code: ${response.statusCode}`);
console.log(`URL: ${response.url}`);
console.log('Headers:', response.headers);

const testOutput = new TestStream();
console.log('\nStreaming body content:');
response.pipe(testOutput);

view raw JSON →