Skip to content

Commit

Permalink
feat: Add plain text body for redirects (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
onebytegone committed Apr 2, 2019
1 parent 3559c02 commit ebb18d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,23 @@ export default class Response {
path = args[0];
}

return this.status(code).location(path).end();
// NOTE: We must set the status and location before creating the response body. The
// status and location functions contain logic needed to properly create the body.
// (e.g. status message creation and 'back' handling)
this.status(code).location(path);

const target = _.first(this.getHeaders().Location);

// TODO: Once Request supports parsing the accept headers, add support for returning
// an HTML response (assuming the request accepts HTML back). See:
// https://github.com/expressjs/express/blob/3d10279826f59bf68e28995ce423f7bc4d2f11cf/lib/response.js#L930-L933
const body = this.getStatus().message + '. Redirecting to ' + target;

if (this._request.method !== 'HEAD') {
this._body = body;
}

return this.end();
}

/**
Expand Down
40 changes: 39 additions & 1 deletion tests/Response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,24 @@ describe('Response', () => {
});

describe('redirect', () => {
it('returns a redirect for the given path', () => {
const evt = albMultiValHeadersRequest(),
req = new Request(app, evt, handlerContext()),
target = 'https://en.wikipedia.org/wiki/HTTP_referer';

resp = new Response(app, req, cb);

const output = makeOutput(302, 'Found', 'Found. Redirecting to ' + target, {
'Location': [ target ],
});

resp.redirect(target);

addHeadersFromMultiValueHeaders(resp, output);
assert.calledOnce(cb);
expect(cb.firstCall.args).to.eql([ undefined, output ]);
});

const testWithBack = (referer: string | null | false, expectation: string, code?: number, msg?: string): void => {
it(`redirects - special value 'back', status ${code}, referer '${referer}'`, () => {
let evt;
Expand All @@ -880,7 +898,9 @@ describe('Response', () => {

resp = new Response(app, req, cb);

const output = makeOutput(code ? code : 302, msg ? msg : 'Found', '', {
const expectedBody = (msg ? msg : 'Found') + '. Redirecting to ' + expectation;

const output = makeOutput(code ? code : 302, msg ? msg : 'Found', expectedBody, {
'Location': [ expectation ],
});

Expand Down Expand Up @@ -920,6 +940,24 @@ describe('Response', () => {
testWithBack(false, '/', code, msg);
});

it('doesn\'t return a body for HEAD requests', () => {
const evt = _.extend(albMultiValHeadersRequest(), { httpMethod: 'HEAD' }),
req = new Request(app, evt, handlerContext()),
target = 'https://en.wikipedia.org/wiki/HTTP_referer';

resp = new Response(app, req, cb);

const output = makeOutput(302, 'Found', '', {
'Location': [ target ],
});

resp.redirect(target);

addHeadersFromMultiValueHeaders(resp, output);
assert.calledOnce(cb);
expect(cb.firstCall.args).to.eql([ undefined, output ]);
});

});

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

0 comments on commit ebb18d3

Please sign in to comment.