Skip to content

Commit

Permalink
feat: Hook into OutgoingMessage.prototype.end (#6)
Browse files Browse the repository at this point in the history
* test: Hook into `OutgoingMessage.prototype.end`

* fix: Hook into `OutgoingMessage.prototype.end`
  • Loading branch information
gr2m authored Dec 22, 2021
1 parent a50fc17 commit fb6f23c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
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

0 comments on commit fb6f23c

Please sign in to comment.