Skip to content

Commit

Permalink
Cleanup for #3178
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 5, 2016
1 parent 25db02a commit a15944d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
27 changes: 17 additions & 10 deletions lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ internals.transmit = function (response, callback) {

// Write headers

const res = internals.writeHead(response);
if (res) {
return process.nextTick(callback, res);
const error = internals.writeHead(response);
if (error) {
return Hoek.nextTick(callback)(error);
}

// Write payload
Expand Down Expand Up @@ -376,28 +376,35 @@ internals.transmit = function (response, callback) {
internals.writeHead = function (response) {

const res = response.request.raw.res;
const headers = Object.keys(response.headers);
let i = 0;

try {
const headers = Object.keys(response.headers);
for (let i = 0; i < headers.length; ++i) {
for (; i < headers.length; ++i) {
const header = headers[i];
const value = response.headers[header];
if (value !== undefined) {
res.setHeader(header, value);
}
}

res.writeHead(response.statusCode);
}
catch (err) {

// Reset headers and return err
for (--i; i >= 0; --i) {
res.setHeader(headers[i], null); // Undo headers
}

res._headers = null;
res._headerNames = {};
return Boom.wrap(err);
}

try {
res.writeHead(response.statusCode);
}
catch (err) {
return Boom.wrap(err);
}

return null;
};


Expand Down
3 changes: 2 additions & 1 deletion test/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2372,13 +2372,14 @@ describe('transmission', () => {

const handler = function (request, reply) {

return reply('ok').header('', 'test');
return reply('ok').header('x', '1').header('', 'test');
};

server.route({ method: 'GET', path: '/', handler: handler });
server.inject('/', (res) => {

expect(res.statusCode).to.equal(500);
expect(res.headers.x).to.not.exist();
done();
});
});
Expand Down

0 comments on commit a15944d

Please sign in to comment.