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: also trigger postbody function on non esi responses #276

Merged
merged 1 commit into from
Sep 27, 2024
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
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ export class esi {
const resp = new Response(response.body, response);
// We set the URL manually here as it doesn't come across from the copy˛
Object.defineProperty(resp, "url", { value: response.url });
// process postBody function if relevant
if (this.postBodyFunction) {
this.postBodyFunction();
}
return resp;
}

Expand Down
24 changes: 24 additions & 0 deletions test/postBody.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test("TEST 1: postBody Function", async () => {
let count = 0;
const postBody = function () {
expect(count).toEqual(3);
count++;
return;
};
parser = new esi(config, undefined, undefined, postBody);
Expand Down Expand Up @@ -80,4 +81,27 @@ test("TEST 1: postBody Function", async () => {
expect(await res.text()).toEqual(
`1\nFRAGMENT: \n2\nFRAGMENT: 2\n3FRAGMENT: 3\n`,
);
expect(count).toEqual(4);
});

test("TEST 2: postBody Function non esi", async () => {
const url = `/post-body/test-2`;
let count = 0;
const postBody = function () {
expect(count).toEqual(1);
count++;
return;
};
parser = new esi(config, undefined, undefined, postBody);

routeHandler.add(url, function (req, res) {
count++;
res.writeHead(200);
res.end("hello i am a body");
});
const res = await makeRequest(url);
expect(res.ok).toBeTruthy();
expect(checkSurrogate(res)).toBeTruthy();
expect(await res.text()).toEqual(`hello i am a body`);
expect(count).toEqual(2);
});