45 lines
1003 B
JavaScript
45 lines
1003 B
JavaScript
import { Writable, Readable } from "stream";
|
|
|
|
export const mockRequestImpl = httpVersion => method => url => body => headers => () => {
|
|
const stream = new Readable({
|
|
read: function (size) {
|
|
this.push(body);
|
|
this.push(null);
|
|
}
|
|
});
|
|
stream.method = method;
|
|
stream.url = url;
|
|
stream.headers = headers;
|
|
stream.httpVersion = httpVersion;
|
|
|
|
return stream;
|
|
};
|
|
|
|
export const mockResponse = () => {
|
|
const stream = new Writable({
|
|
write: function (chunk, _, cb) {
|
|
this.body += chunk;
|
|
if (cb) cb();
|
|
},
|
|
writev: function (chunks, _, cb) {
|
|
this.body += chunks.join('');
|
|
if (cb) cb();
|
|
},
|
|
})
|
|
stream.body = ''
|
|
stream.headers = {}
|
|
stream.setHeader = function (header, val) {
|
|
this.headers[header] = val;
|
|
}
|
|
|
|
return stream
|
|
}
|
|
|
|
export const stringToStream = str => {
|
|
const stream = new Readable();
|
|
stream._read = function () {};
|
|
stream.push(str);
|
|
stream.push(null);
|
|
return stream;
|
|
}
|