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) => {