Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not consume response when recording #22

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export default {
interceptedRequest[method] = function (chunk, encoding, callback) {
// chunk argument may be set to the callback function, see
// https://github.com/nodejs/node/blob/9e1a08057a0cd803d0878ed4b87774b5f84d6f0a/lib/_http_outgoing.js#L834-L841
if (typeof encoding === "function") {
callback = encoding;
encoding = null;
}
if (chunk && typeof chunk !== "function") {
requestBodyChunks.push(Buffer.from(chunk, encoding));
requestBodyChunks.push(Buffer.from(chunk, encoding || "utf8"));
}

return originalMethod.call(this, chunk, encoding, callback);
Expand Down
55 changes: 50 additions & 5 deletions test/record-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ test("request.write() with base64 encoding", async () => {
return flowControl.promise;
});

test("request.write(data, callback)", async () => {
const recordDataControl = getFlowControl();
const writeCallbackControl = getFlowControl();

const server = http.createServer(async (_request, response) => {
response.end();
});
const { port } = server.listen().address();

HttpRecorder.enable();
HttpRecorder.on("record", async ({ requestBody }) => {
try {
assert.equal(Buffer.concat(requestBody).toString(), "Hello!");
recordDataControl.resolve();
} catch (error) {
recordDataControl.reject(error);
}
});

const request = http.request(
`http://localhost:${port}/path`,
{
method: "post",
},
() => server.close()
);
request.on("error", recordDataControl.reject);
request.write("Hello!", (error) => {
if (error) return writeCallbackControl.reject(error);
writeCallbackControl.resolve();
});
request.end();

await recordDataControl.promise;
await writeCallbackControl.promise;
});

test("request.end(text)", () => {
const flowControl = getFlowControl();
const server = http.createServer(async (_request, response) => {
Expand Down Expand Up @@ -218,6 +255,7 @@ test("request.end(callback)", () => {
test("delayed response read", async () => {
const recordDataControl = getFlowControl();
const responseDataControl = getFlowControl();
const timeoutControl = getFlowControl();

const server = http.createServer(async (_request, response) => {
response.write("Hello!");
Expand All @@ -239,11 +277,13 @@ test("delayed response read", async () => {

let retrievedResponseData = false;
http.get(`http://localhost:${port}`, (response) => {
response.pause();
response.on("close", () => {
server.close();
});
setTimeout(() => {
response.on("close", () => {
clearTimeout(timeout);
timeoutControl.resolve();
server.close();
});

response.on("data", (data) => {
try {
assert.equal(data.toString(), "Hello!");
Expand All @@ -253,10 +293,15 @@ test("delayed response read", async () => {
responseDataControl.reject(error);
}
});
response.resume();
}, 10);
});

const timeout = setTimeout(() => {
server.close();
timeoutControl.reject(new Error("Timeout"));
}, 100);

await timeoutControl.promise;
await recordDataControl.promise;
await responseDataControl.promise;

Expand Down