Skip to content

Commit

Permalink
Handle signals in onRequest. Closes #3884
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jan 6, 2019
1 parent 2f64d40 commit 0c592b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/response.js
Expand Up @@ -70,7 +70,9 @@ exports = module.exports = internals.Response = class {

static wrap(result, request) {

if (result instanceof internals.Response) {
if (result instanceof internals.Response ||
typeof result === 'symbol') {

return result;
}

Expand Down
51 changes: 50 additions & 1 deletion test/toolkit.js
Expand Up @@ -289,10 +289,11 @@ describe('Toolkit', () => {

describe('abandon', () => {

it('abandon request with manual response', async () => {
it('abandon request with manual response (handler)', async () => {

const handler = (request, h) => {

request.raw.res.setHeader('content-type', 'text/plain');
request.raw.res.end('manual');
return h.abandon;
};
Expand All @@ -303,6 +304,54 @@ describe('Toolkit', () => {
const res = await server.inject('/');
expect(res.result).to.equal('manual');
});

it('abandon request with manual response (onRequest)', async () => {

const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: () => null });

server.ext('onRequest', (request, h) => {

request.raw.res.setHeader('content-type', 'text/plain');
request.raw.res.end('manual');
return h.abandon;
});

const res = await server.inject('/');
expect(res.result).to.equal('manual');
});

it('abandon request with manual response (lifecycle)', async () => {

const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: () => null });

server.ext('onPreHandler', (request, h) => {

request.raw.res.setHeader('content-type', 'text/plain');
request.raw.res.end('manual');
return h.abandon;
});

const res = await server.inject('/');
expect(res.result).to.equal('manual');
});

it('abandon request with manual response (post cycle)', async () => {

const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: () => null });

server.ext('onPreResponse', (request, h) => {

request.raw.res.setHeader('content-type', 'text/plain');
request.raw.res.end('manual');
return h.abandon;
});

const res = await server.inject('/');
expect(res.result).to.equal('manual');
});
});

describe('close', () => {
Expand Down

0 comments on commit 0c592b7

Please sign in to comment.