Skip to content

Commit

Permalink
fix(MockServer): use proper address, actually send messages
Browse files Browse the repository at this point in the history
MockServer was totally broken and somehow still passing tests. We
were using the test config's server which is used explicitly for
integration tests, so we now use the address provided by the mock
server. Also, the client.test.js test had an invalid response
sequence which has been fixed
  • Loading branch information
mbroadst committed Oct 11, 2015
1 parent 48a9606 commit 505bf5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 5 additions & 8 deletions test/unit/client.test.js
Expand Up @@ -5,8 +5,8 @@ var AMQPClient = require('../../lib').Client,

constants = require('../../lib/constants'),

SaslFrames = require('../../lib/frames/sasl_frame'),
OpenFrame = require('../../lib/frames/open_frame'),
BeginFrame = require('../../lib/frames/begin_frame'),
CloseFrame = require('../../lib/frames/close_frame'),

DefaultPolicy = require('../../lib/policies/default_policy'),
Expand All @@ -16,7 +16,6 @@ var AMQPClient = require('../../lib').Client,

DefaultPolicy.connect.options.containerId = 'test';


describe('Client', function() {
describe('#connect()', function() {

Expand All @@ -38,17 +37,15 @@ describe('Client', function() {

it('should connect then disconnect', function() {
test.server.setResponseSequence([
constants.saslVersion,
[
new SaslFrames.SaslMechanisms(['PLAIN']),
new SaslFrames.SaslOutcome({code: constants.saslOutcomes.ok})
],
constants.amqpVersion,
new OpenFrame(DefaultPolicy.connect.options),
new BeginFrame({
remoteChannel: 1, nextOutgoingId: 0, incomingWindow: 2147483647, outgoingWindow: 2147483647, handleMax: 4294967295
}),
new CloseFrame(new AMQPError(AMQPError.ConnectionForced, 'test'))
]);

return test.client.connect(test.config.address)
return test.client.connect(test.server.address())
.then(function() {
return test.client.disconnect();
});
Expand Down
15 changes: 11 additions & 4 deletions test/unit/mocks/server.js
Expand Up @@ -10,18 +10,23 @@ var _ = require('lodash'),

tu = require('../testing_utils');


function MockServer(options) {
this._server = null;
this._client = null;
this._responses = [];

_.defaults(this, options, {
hostname: '0.0.0.0',
port: 4321,
serverGoesFirst: false
});
}

MockServer.prototype.address = function() {
if (!this.server) throw new Error('no server');
return 'amqp://' + this.server.address().address + ':' + this.server.address().port;
};

MockServer.prototype.setup = function() {
var self = this;
return new Promise(function(resolve, reject) {
Expand All @@ -47,7 +52,7 @@ MockServer.prototype.setup = function() {
reject(err);
});

self.server.listen(self.port, function() {
self.server.listen(self.port, self.hostname, function() {
debug('server listening on ' + self.port);
resolve();
});
Expand Down Expand Up @@ -86,7 +91,7 @@ MockServer.prototype.setResponseSequence = function(responses) {

MockServer.prototype._sendNextResponse = function() {
var self = this,
response = this._responses.unshift();
response = this._responses.shift();

if (Array.isArray(response)) {
response.forEach(function(r) { self._sendResponse(r); });
Expand All @@ -102,7 +107,9 @@ MockServer.prototype._sendResponse = function(response) {
}

if (typeof response !== 'string') {
this._client.write(response);
this._client.write(response, function() {
debug('wrote: ', response.toString('hex'));
});
return;
}

Expand Down

0 comments on commit 505bf5e

Please sign in to comment.