Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve original response status on error after write, fix status message #4191

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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