Skip to content

Commit

Permalink
Issue postwait#445: Guard against unknown exchange name in queue.unbi…
Browse files Browse the repository at this point in the history
…nd().

Do not touch this._bindings but execute the command against the
connection anyway, assuming that the caller knows what they are
doing.
  • Loading branch information
Giso Deutschmann committed Jan 19, 2017
1 parent ca2e3b5 commit acd53e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,17 @@ Queue.prototype.unbind = function (exchange, routingKey) {

var exchangeName = exchange instanceof Exchange ? exchange.name : exchange;

// Decrement binding count.
this._bindings[exchangeName][routingKey]--;
if (!this._bindings[exchangeName][routingKey]) {
delete this._bindings[exchangeName][routingKey];
}
if(this._bindings[exchangeName]) {
// Decrement binding count.
this._bindings[exchangeName][routingKey]--;
if (!this._bindings[exchangeName][routingKey]) {
delete this._bindings[exchangeName][routingKey];
}

// If there are no more bindings to this exchange, delete the key for the exchange.
if (!_.keys(this._bindings[exchangeName]).length){
delete this._bindings[exchangeName];
// If there are no more bindings to this exchange, delete the key for the exchange.
if (!_.keys(this._bindings[exchangeName]).length){
delete this._bindings[exchangeName];
}
}

return this._taskPush(methods.queueUnbindOk, function () {
Expand Down
27 changes: 27 additions & 0 deletions test/test-unbind-unknown-exchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var conn = require('./harness').createConnection();

var caughtError = false;

var later = function(fun) {
setTimeout(fun, 500);
};
conn.once('ready', function () {
puts("connected to " + conn.serverProperties.product);

var q = conn.queue('node-simple-queue');

try {
q.unbind('unknown-exchange', 'routing-key');
} catch(e) {
caughtError = true;
}

later(function() {
conn.end();
process.exit();
});
});

process.addListener('exit', function () {
assert.equal(caughtError, false);
});

0 comments on commit acd53e9

Please sign in to comment.