Skip to content

Commit

Permalink
fix: also trigger postbody function on non esi responses
Browse files Browse the repository at this point in the history
  • Loading branch information
cdloh committed Sep 27, 2024
1 parent 0c9d7dc commit e84948a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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);
});

0 comments on commit e84948a

Please sign in to comment.