Skip to content

Commit

Permalink
Preserve original response status on error after write, fix status me…
Browse files Browse the repository at this point in the history
…ssage. Close #4182 (#4191)
  • Loading branch information
devinivy authored Nov 23, 2020
1 parent 75e9014 commit d76ceb5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,21 @@ internals.Server = class {
const res = await Shot.inject(needle, settings);
const custom = res.raw.res[Config.symbol];
if (custom) {
res.result = custom.result;
res.request = custom.request;
delete res.raw.res[Config.symbol];

res.request = custom.request;

if (custom.result !== undefined) {
res.result = custom.result;
}

if (custom.statusCode !== undefined) {
res.statusCode = custom.statusCode;
}

if (custom.statusMessage !== undefined) {
res.statusMessage = custom.statusMessage;
}
}

if (res.result === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ internals.end = function (env, event, err) {
request._setResponse(error);

if (request.raw.res[Config.symbol]) {
request.raw.res.statusCode = error.statusCode;
request.raw.res[Config.symbol].statusCode = error.statusCode;
request.raw.res[Config.symbol].statusMessage = error.source.error;
request.raw.res[Config.symbol].result = error.source; // Force injected response to error
}

Expand Down
13 changes: 11 additions & 2 deletions test/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,11 @@ describe('transmission', () => {

const res = await server.inject('/');
expect(res.statusCode).to.equal(500);
expect(res.statusMessage).to.equal('Internal Server Error');
expect(res.result.message).to.equal('An internal server error occurred');
expect(res.raw.res.statusCode).to.equal(200);
expect(res.raw.res.statusMessage).to.equal('OK');
expect(res.rawPayload.toString()).to.equal('success');

const [request] = await log;
expect(request.response.statusCode).to.equal(500);
Expand All @@ -747,7 +751,7 @@ describe('transmission', () => {
this.isDone = true;

this.push('something');
this.emit('error', new Error());
setImmediate(() => this.emit('error', new Error()));
};

return stream;
Expand All @@ -758,10 +762,11 @@ describe('transmission', () => {
server.route({ method: 'GET', path: '/', handler });

await server.start();
await expect(Wreck.request('GET', 'http://localhost:' + server.info.port + '/')).to.reject();
const err = await expect(Wreck.get('http://localhost:' + server.info.port + '/')).to.reject();
await server.stop();

const [request] = await log;
expect(err.data.res.statusCode).to.equal(200);
expect(request.response.statusCode).to.equal(500);
expect(request.info.completed).to.be.above(0);
expect(request.info.responded).to.equal(0);
Expand Down Expand Up @@ -1223,6 +1228,10 @@ describe('transmission', () => {

const res = await server.inject({ url: '/stream', headers: { 'Accept-Encoding': 'gzip' } });
expect(res.statusCode).to.equal(499);
expect(res.statusMessage).to.equal('Unknown');
expect(res.raw.res.statusCode).to.equal(204);
expect(res.raw.res.statusMessage).to.equal('No Content');
expect(res.rawPayload.toString()).to.equal('here is the response');

const [request] = await log;
expect(request.response.statusCode).to.equal(499);
Expand Down

0 comments on commit d76ceb5

Please sign in to comment.