http-socket-hang-up
Advanced tools
Comparing version 1.0.0 to 1.0.1
21
index.js
@@ -9,3 +9,3 @@ "use strict"; | ||
opts = opts || {}; | ||
if (typeof opts !== "object") throw new Error("Invalid options argument, it must be an object instance."); | ||
if (Array.isArray(opts) || typeof opts !== "object") throw new Error("Invalid options argument, it must be an object instance."); | ||
@@ -15,3 +15,4 @@ // clone options | ||
logger: opts.logger || fakeLogger, | ||
header: opts.header || "x-timeout" | ||
header: opts.header || "x-timeout", | ||
level: opts.level || "verbose" | ||
}; | ||
@@ -26,7 +27,11 @@ | ||
// valdate log level | ||
if (typeof options.header !== "string") throw new Error("Invalid 'options.header' property, it must be a string. Default is 'x-timeout'"); | ||
// valdate header name | ||
options.header = options.header; | ||
if (typeof options.header !== "string") throw new Error("Invalid 'options.header' property, it must be a string."); | ||
if (typeof options.level !== "string") throw new Error("Invalid 'options.level' property, it must be a string. Default is 'verbose'"); | ||
// create instance | ||
options.logger.info("http-socket-hang-up options were validated."); | ||
var instance = new TimeoutMiddleware(options); | ||
@@ -40,2 +45,4 @@ return instance.callback; | ||
var log = options.logger[options.level]; | ||
this.callback = function callback(req, res, next) { | ||
@@ -52,3 +59,3 @@ | ||
// nothing to do because TCP Keep alive is long enough | ||
options.logger.verbose("TCP Keep Alive was not updated", context); | ||
log("TCP Keep Alive was not updated", context); | ||
@@ -60,3 +67,3 @@ } else { | ||
context.newKeepAlive = context.timeout + context.keepAlive; | ||
options.logger.verbose("TCP Keep Alive was updated with a new value:", context); | ||
log("TCP Keep Alive was updated with a new value:", context); | ||
req.connection.setTimeout(context.newKeepAlive); | ||
@@ -66,3 +73,3 @@ | ||
res.on("finish", function () { | ||
options.logger.verbose("TCP keep alive was reseted to its original value.", context); | ||
log("TCP keep alive was reseted to its original value.", context); | ||
req.connection.setTimeout(context.keepAlive); | ||
@@ -69,0 +76,0 @@ }); |
{ | ||
"name": "http-socket-hang-up", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Handles 'socket hang up' exception when an HTTP request last more than 2 minutes'", | ||
@@ -34,8 +34,10 @@ "main": "index.js", | ||
"homepage": "https://github.com/silviom/node-http-socket-hang-up", | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"istanbul": "^0.3.14", | ||
"mocha": "^2.2.5" | ||
"mocha": "^2.2.5", | ||
"rewire": "^2.3.4", | ||
"sinon": "^1.15.4", | ||
"wired": "0.0.0" | ||
} | ||
} |
"use strict"; | ||
var assert = require("assert"); | ||
var sinon = require("sinon"); | ||
var rewire = require("rewire"); | ||
@@ -12,3 +14,50 @@ describe("index.js", function () { | ||
// more test comming soon :) | ||
describe("create method:", function() { | ||
it("should fails when options are invalid", function () { | ||
var middleware = require(".."); | ||
var invalidOptions = [ | ||
100, | ||
true, | ||
"foo", | ||
[] | ||
]; | ||
invalidOptions.forEach(function (options) { | ||
try { | ||
middleware.create(options); | ||
assert.fail("Did not fail!"); | ||
} catch (e) { | ||
assert.ok(e instanceof Error); | ||
assert.equal(e.message, "Invalid options argument, it must be an object instance."); | ||
} | ||
}); | ||
}); | ||
describe("logger option.", function () { | ||
it("It should set fakeLogger by default", function() { | ||
var middleware = rewire(".."); | ||
var fakeLogger = { | ||
info: sinon.stub() | ||
}; | ||
middleware.__set__("fakeLogger", fakeLogger); | ||
middleware.create(); | ||
assert.ok(fakeLogger.info.called); | ||
}); | ||
it("It should set the console as logger when the option was set to true", function() { | ||
var middleware = require(".."); | ||
var consoleStub = sinon.spy(console, "log"); | ||
middleware.create(); | ||
assert.ok(consoleStub); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5786
119
5