Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
empty interfaces should not be invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
namgk committed Feb 26, 2017
1 parent 1c0c6d3 commit dfdf509
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 93 deletions.
10 changes: 5 additions & 5 deletions lib/options.js
Expand Up @@ -259,11 +259,11 @@ function validate(opts, validationOptions) {
var result = validator.validate(opts, '/Options', validationOptions);

// check empty interfaces
if (opts.hasOwnProperty('interfaces')) {
if (opts.interfaces.length === 0) {
result.addError('no interfaces were defined');
}
}
// if (opts.hasOwnProperty('interfaces')) {
// if (opts.interfaces.length === 0) {
// result.addError('no interfaces were defined');
// }
// }

// check required credentials
if (opts.hasOwnProperty('interfaces')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Expand Up @@ -162,6 +162,7 @@ function Server(opts, callback) {
// initialize servers list
this.servers = [];


steed.series([

// steed.series: wait for ascoltatore
Expand Down Expand Up @@ -236,7 +237,6 @@ function Server(opts, callback) {
}
});


that.on("clientConnected", function(client) {
if(that.modernOpts.publishNewClient) {
that.publish({
Expand Down
161 changes: 74 additions & 87 deletions test/server_websocket_attach.js
@@ -1,4 +1,4 @@
var mqtt = require('mqtt')
var mqtt = require('mqtt');
var websocket = require('ws');
var http = require('http');

Expand All @@ -10,166 +10,130 @@ var ping = 'ping';
var pong = 'pong';

describe("mosca.Server - Mqtt-over-WS attached to existing http server", function() {

var mqttServ
var server
var server, mqttServ;

afterEach(function(done){
beforeEach(function(){
server = http.createServer();
mqttServ = new mosca.Server({interfaces:[]});
});

afterEach(function(){
server.close();
mqttServ.close(done);
})
});

it("should be able to do mqtt over WebSocket", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({});
it("should not occupy 1883 port while attached to http server", function(done) {
mqttServ.attachHttpServer(server);
server.listen(1883, done);
});

it("should be able to do mqtt over WebSocket", function(done) {
mqttServ.attachHttpServer(server);
server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
done();
});
client.publish(mqttTopic, ping);
});
});

it("should be able to do mqtt over WebSocket on specific path", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
done();
});
client.publish(mqttTopic, ping);
});
});

it("should not be able to do mqtt over WebSocket on different path", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({port:3333});// async test, preventing intefere with other tests that spawn on 1883 port
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + '/junk');
client.subscribe(mqttTopic);
var failed = false;
var failed = false;// ensuring done is called once
client.on("message", function(topic, payload) {
failed = true;
done(failed)
done(failed);
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
done()
done();
}
}, 2000);
}, 3000);
});
});

it("should not be able to do mqtt over WebSocket on root path", function(done) {
var server = http.createServer();
var mqttServ = new mosca.Server({port:3333});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port);
client.subscribe(mqttTopic);
var failed = false;
client.on("message", function(topic, payload) {
failed = true;
mqttServ.close(function(){
done(failed);
});
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
server.close();
mqttServ.close(done);
}
}, 2000);
});
});

//TODO: this test failed, what does the spec say?
xit("should not be able to do mqtt over WebSocket on a specific path without attaching to any path", function(done) {
var server = http.createServer();
var mqttServ = new mosca.Server({port:3333});
mqttServ.attachHttpServer(server);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
var failed = false;
client.on("message", function(topic, payload) {
failed = true;
mqttServ.close(function(){
done(failed);
});
done(failed);
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
server.close();
mqttServ.close(done);
done();
}
}, 2000);
});
});
});

describe("mosca.Server - Websocket and Mqtt-over-WS attached to the same http server", function() {
it("ws client should not connect when mqtt is attached to http server without path", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
var server, mqttServ, wss;

beforeEach(function(){
server = http.createServer();
mqttServ = new mosca.Server({interfaces:[]});

wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
});
var mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server);
});

afterEach(function(){
server.close();
});

it("ws client should not connect when mqtt is attached to http server without path", function(done) {
mqttServ.attachHttpServer(server);
server.listen(port, function(){
var ws = new websocket('ws://localhost:' + port + path, {
perMessageDeflate: false
});

ws.on('error', function(e) {
expect(e).to.not.be.undefined;
server.close();
mqttServ.close(done);
done();
});
});
});

it("ws client should be able to connect when specific path is used", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
});
mqttServ.attachHttpServer(server, mqttPath);
wss.on('connection', function(conn){
conn.on('message', function(msg){
expect(msg).to.equal(ping);
conn.send(pong);
});
});

var mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var ws = new websocket('ws://localhost:' + port + path, {
perMessageDeflate: false
Expand All @@ -181,34 +145,57 @@ describe("mosca.Server - Websocket and Mqtt-over-WS attached to the same http se

ws.on('message', function(msg){
expect(msg).to.equal(pong);
server.close();
mqttServ.close(done);
done();
});
});
});


it("mqtt client should be able to connect as well", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
mqttServ.attachHttpServer(server, mqttPath);
server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
done();
});
client.publish(mqttTopic, ping);
});
});

var mqttServ = new mosca.Server({});
it("both ws and mqtt client should be able to connect at the same time", function(done) {
mqttServ.attachHttpServer(server, mqttPath);
wss.on('connection', function(conn){
conn.on('message', function(msg){
expect(msg).to.equal(ping);
conn.send(pong);
});
});

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
var ws = new websocket('ws://localhost:' + port + path, {
perMessageDeflate: false
});

client.on('connect', function () {
client.subscribe(mqttTopic);
setTimeout(function(){// wait for ws to connect
ws.send(ping);
}, 2000);
});

ws.on('message', function(msg){
expect(msg).to.equal(pong);
client.publish(mqttTopic, ping);
});

client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
done();
});
client.publish(mqttTopic, ping);
});
});
});

0 comments on commit dfdf509

Please sign in to comment.