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

feat: Hook into OutgoingMessage.prototype.end #6

Merged
merged 2 commits into from
Dec 22, 2021
Merged
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
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export default {
const interceptedRequest = this;

// read the request body as an array of Buffer chuncks
// by hooking into the `request.write` method.
// by hooking into the `request.{write,end}` methods.
const requestBodyChunks = [];
const originalRequestWrite = interceptedRequest.write;
interceptedRequest.write = function (chunk) {
requestBodyChunks.push(Buffer.from(chunk));
return originalRequestWrite.call(this, chunk);
};
for (const method of ["write", "end"]) {
const originalMethod = interceptedRequest[method];
interceptedRequest[method] = function (chunk) {
if (chunk) requestBodyChunks.push(Buffer.from(chunk));
return originalMethod.call(this, chunk);
};
}

interceptedRequest.on("response", async (response) => {
// read the response body as an array of Buffer chuncks
Expand Down
57 changes: 57 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,63 @@ test("happy path", () => {
request.end();
});

test("Using request.end", () => {
const server = http.createServer(async (_request, response) => {
response.setHeader("x-my-response-header", 2);
response.end("World!");
});
const { port } = server.listen().address();

HttpRecorder.enable();
HttpRecorder.on(
"record",
async ({ request, response, requestBody, responseBody }) => {
const { method, protocol, host, path } = request;
const requestHeaders = { ...request.getHeaders() };

try {
assert.equal(method, "POST");
assert.equal(protocol, "http:");
assert.equal(host, "localhost");
assert.equal(path, "/path");
assert.equal(requestHeaders, {
host: "localhost:" + port,
"x-my-request-header": "1",
});
assert.equal(Buffer.concat(requestBody).toString(), "Hello!");

const {
statusCode,
statusMessage,
headers: responseHeaders,
} = response;

assert.equal(statusCode, 200);
assert.equal(statusMessage, "OK");
assert.equal(responseHeaders["x-my-response-header"], "2");
assert.equal(Buffer.concat(responseBody).toString(), "World!");
} catch (error) {
if (error.code !== "ERR_ASSERTION") throw error;
console.log(error.details);
console.log("expected:", error.expects);
console.log("actual:", error.actual);
}
}
);

const request = http.request(
`http://localhost:${port}/path`,
{
method: "post",
headers: {
"x-my-request-header": "1",
},
},
() => server.close()
);
request.end("Hello!");
});

test("Calling .enable() multiple times is a no-op", () => {
HttpRecorder.enable();
HttpRecorder.enable();
Expand Down