Skip to content

Commit

Permalink
feat(buffer): support buffer type arguments in res.send() (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister authored Feb 4, 2020
1 parent 5d889ac commit 726f059
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div>test</div>');
});
app.use(middleware());
app.get('/esi', (req, res) => {
res.type('text/html');
res.send(Buffer.from('<section><esi:include src="http://localhost:' + port + '"></esi:include></section>'));
});

// when
const req = gg.get('http://localhost:' + expressPort + '/esi');

// then
req.then(response => {
assert.equal(response.body, '<section><div>test</div></section>');
done();
}).catch(done);
});

it('should respect user defined callback in second parameter', done => {
// given
server.addListener('request', (req, res) => {
Expand Down

0 comments on commit 726f059

Please sign in to comment.