From 726f0591a8890b0f286698e63413226e5f8b9667 Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Tue, 4 Feb 2020 10:46:07 +0100 Subject: [PATCH] feat(buffer): support buffer type arguments in res.send() (#31) --- lib/middleware.js | 4 ++++ test/middleware-test.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/middleware.js b/lib/middleware.js index 2946535..47e0cfa 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -34,9 +34,13 @@ module.exports = function middleware(config) { }; res.send = body => { + const ctype = res.get('Content-Type'); if(typeof body === 'string' && !req._esiProcessed) { esi.process(body, req.esiOptions).then(oldSend); } + else if(body instanceof Buffer && ctype && ctype.match(/(text\/)|(\/xml)/) && !req._esiProcessed) { + esi.process(body.toString(), req.esiOptions).then(oldSend); + } else { oldSend(body); } diff --git a/test/middleware-test.js b/test/middleware-test.js index 8b70f9e..7b4c73e 100644 --- a/test/middleware-test.js +++ b/test/middleware-test.js @@ -82,6 +82,28 @@ describe('A express middleware', () => { }).catch(done); }); + it.only('should fetch external component with a middleware when send is called (buffer version)', done => { + // given + server.addListener('request', (req, res) => { + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end('
test
'); + }); + app.use(middleware()); + app.get('/esi', (req, res) => { + res.type('text/html'); + res.send(Buffer.from('
')); + }); + + // when + const req = gg.get('http://localhost:' + expressPort + '/esi'); + + // then + req.then(response => { + assert.equal(response.body, '
test
'); + done(); + }).catch(done); + }); + it('should respect user defined callback in second parameter', done => { // given server.addListener('request', (req, res) => {