Repository URL to install this package:
|
Version:
7.1.2-patch-delete ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
class MockIncomingMessage extends stream_1.Transform {
constructor(options = {}) {
super();
this._writableState.objectMode = true;
this._readableState.objectMode = false;
// Copy unreserved options
const reservedOptions = ['method', 'url', 'headers', 'rawHeaders'];
Object.keys(options).forEach(key => {
if (reservedOptions.includes(key) === false) {
this[key] = options[key];
}
});
this.method = options.method || 'GET';
this.url = options.url || '';
// Set header names
this.headers = {};
this.rawHeaders = [];
if (options.headers) {
Object.keys(options.headers).forEach(key => {
let val = options.headers[key];
if (val !== undefined) {
if (typeof val !== 'string') {
// I think this is trying to convert it to a string? @@fork
val += '';
}
this.headers[key.toLowerCase()] = val;
this.rawHeaders.push(key);
this.rawHeaders.push(val);
}
});
}
// Auto-end when no body
if (this.method === 'GET' ||
this.method === 'HEAD' ||
this.method === 'DELETE') {
this.end();
}
}
_transform(chunk, encoding, next) {
if (this._failError) {
return this.emit('error', this._failError);
}
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) {
chunk = JSON.stringify(chunk);
}
this.push(chunk);
next();
}
// Causes the request to emit an error when the body is read.
_fail(error) {
this._failError = error;
}
}
exports.MockIncomingMessage = MockIncomingMessage;
//# sourceMappingURL=MockRequest.js.map