smocks icon indicating copy to clipboard operation
smocks copied to clipboard

Server.select('<connection-label>') is not set correctly for multiple connections.

Open abhagupta opened this issue 8 years ago • 0 comments

When smocks is used as a PLUGIN on a hapi server instance which could have more than 1 connection, the individual connection properties are not set correctly. For example,

  var server = new hapi.Server();
  server.connection({ port: port, labels: 'http', host: 'localhost'});
  server.connection({ port: httpsPort, labels: 'https', tls: tls, host: 'localhost'});

  var plugin = SmocksHapi.toPlugin({
    onRegister: function (server, options, next) {
      return next();
    }
  });

In above scenario, the server object has 2 connections, represented by server.connections. which should inherit the properties of server using server.select(<connection>) of hapi api. http://hapijs.com/api/#serverconnections.

Looked at the smocks code and found that

if (mocker.connection()) {
    connection = server.select(mocker.connection());
  }

( https://github.com/jhudson8/smocks/blob/master/lib/admin/index.js#L18 ) is called but

connection: function(connection) {
    if (connection) {
      smocksInstance._connection = connection;
    }
    return smocksInstance._connection;
  }

in https://github.com/jhudson8/smocks/blob/master/lib/index.js#L22 is expecting a parameter, which is actually the connection. Since parameter is not sent, the object returned is undefined and intended connection is never applied in sever.select() . This causes connection to miss a lot of important properties.

abhagupta avatar May 26 '16 21:05 abhagupta