From e84948a7f4997d3de5465fa6ffc860aa87f309d1 Mon Sep 17 00:00:00 2001 From: Callum Loh Date: Fri, 27 Sep 2024 16:12:59 +0100 Subject: [PATCH] fix: also trigger postbody function on non esi responses --- src/index.ts | 4 ++++ test/postBody.spec.ts | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/index.ts b/src/index.ts index 4adff49..40625ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } diff --git a/test/postBody.spec.ts b/test/postBody.spec.ts index d81121e..ffed4a3 100644 --- a/test/postBody.spec.ts +++ b/test/postBody.spec.ts @@ -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); @@ -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); });