From 1a843b9bada4fa66df9930c7e0cf60bf0d40c3c4 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 15 Apr 2015 10:45:53 +0100 Subject: [PATCH] Do not emit error when callback is passed --- lib/advertisement.js | 13 +++++--- package.json | 2 +- tests/test_advert.js | 71 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 tests/test_advert.js diff --git a/lib/advertisement.js b/lib/advertisement.js index ebc0c60c..467febd8 100644 --- a/lib/advertisement.js +++ b/lib/advertisement.js @@ -40,15 +40,20 @@ function Advertisement(serviceType, port, options, callback) { serviceType, domain, context) { var error = dns_sd.buildException(errorCode); - if (callback) { - callback.call(self, error, { + var service; + + if (!error) { + service = { name: name , type: makeServiceType(serviceType) , domain: domain , flags: flags - }, context); + }; } - if (error) { + + if (callback) { + callback(error, service, context); + } else if (error) { self.emit('error', error); } } diff --git a/package.json b/package.json index d447253d..7db742fd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ , "ncp": "*" , "minimatch": "*" , "proxyquire": "~0.5" + , "sinon": "~1.14.1" } , "repository": { "type": "git" @@ -61,4 +62,3 @@ } , "gypfile": true } - diff --git a/tests/test_advert.js b/tests/test_advert.js new file mode 100644 index 00000000..9fcd890d --- /dev/null +++ b/tests/test_advert.js @@ -0,0 +1,71 @@ +var st = require('../lib/service_type.js') + , dns_sd = require('../lib/dns_sd') + , proxyquire = require('proxyquire') + , sinon = require('sinon') + ; + +//=== Browser =========================================================== + +module.exports["Advertisement"] = { + "Should pass error to callback when error occurs during service registration": function(test) { + var callback = null + , invocations = 0 + , threwException = false + , serviceType = "foo" + , port = "bar" + , options = "baz" + , error = new Error("Fail!") + , code = 10; + + var mock_dns_sd = { + buildException: sinon.stub().withArgs(code).returns(error), + DNSServiceRegister: sinon.stub().callsArgWithAsync(9, null, null, code, null, null, null, null) + }; + + var mock_service_type = { + makeServiceType: sinon.stub() + } + + // create the browser with our mock of dns_sd + var ad = proxyquire('../lib/advertisement.js', { + './dns_sd': mock_dns_sd, + './service_type': mock_service_type + }).Advertisement.create(serviceType, port, options, function(er) { + test.equal(error, er); + + test.done(); + }); + }, + + "Should emit an error when when error occurs during service registration and no callbacked is passed": function(test) { + var callback = null + , invocations = 0 + , threwException = false + , serviceType = "foo" + , port = "bar" + , options = "baz" + , error = new Error("Fail!") + , code = 10; + + var mock_dns_sd = { + buildException: sinon.stub().withArgs(code).returns(error), + DNSServiceRegister: sinon.stub().callsArgWithAsync(9, null, null, code, null, null, null, null) + }; + + var mock_service_type = { + makeServiceType: sinon.stub() + } + + // create the browser with our mock of dns_sd + var ad = proxyquire('../lib/advertisement.js', { + './dns_sd': mock_dns_sd, + './service_type': mock_service_type + }).Advertisement.create(serviceType, port, options) + + ad.on('error', function(er) { + test.equal(error, er); + + test.done(); + }); + } +}