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

Fix: Cannot read property 'statusCodes' of undefined #303

Merged
merged 2 commits into from
Feb 17, 2015
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
8 changes: 5 additions & 3 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internals.NetworkMonitor.prototype._onResponse = function (request) {

var msec = Date.now() - request.info.received;
var port = request.connection.info.port;
var statusCode = request.response.statusCode;
var statusCode = request.response && request.response.statusCode;

this._responseTimes[port] = this._responseTimes[port] || { count: 0, total: 0, max: 0 };
this._responseTimes[port].count++;
Expand All @@ -47,8 +47,10 @@ internals.NetworkMonitor.prototype._onResponse = function (request) {
this._responseTimes[port].max = msec;
}

this._requests[port].statusCodes[statusCode] = this._requests[port].statusCodes[statusCode] || 0;
this._requests[port].statusCodes[statusCode]++;
if (statusCode) {
this._requests[port].statusCodes[statusCode] = this._requests[port].statusCodes[statusCode] || 0;
this._requests[port].statusCodes[statusCode]++;
}
};


Expand Down
46 changes: 45 additions & 1 deletion test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Network Monitor', function () {

while (port) {

expect(network._requests[port]).to.exist();
expect(network._requests[port]).to.exist();
expect(network._requests[port].total).to.equal(20);
expect(network._requests[port].statusCodes[200]).to.equal(20);

Expand Down Expand Up @@ -322,4 +322,48 @@ describe('Network Monitor', function () {
done();
});
});

it('does not throw if request.response is null', function (done) {

var server = new Hapi.Server();
server.connection({ host: 'localhost' });

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) { reply(); }
});

// force response to be null to mimic client disconnect
server.on('response', function (request) {

request.response = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after function.

});

var network = new NetworkMonitor.Monitor(server);
var agent = new Http.Agent({ maxSockets: Infinity });
var conn = server.connections[0];
var usedPorts = [];

server.start(function () {

Http.get({
path: '/',
host: conn.info.host,
port: conn.info.port,
agent: agent
}, Hoek.ignore);

setTimeout(function () {

expect(network._requests[conn.info.port]).to.exist();
expect(network._requests[conn.info.port].total).to.equal(1);
expect(network._requests[conn.info.port].statusCodes[200]).to.not.exist();

expect(network._responseTimes[conn.info.port]).to.exist();

done();
}, 500);
});
});
});