Testing sails.socket with npm checkout
I am using sails.js v0.11.0 and I am just doing unit testing. I can test normal controllers over HTTP requests, but I don't know where to start testing the same calls over socket requests. If you have a good resource or sample test using sockets that would be fantastic.
var assert = require('assert');
var request = require('supertest');
describe('Auth Controller', function () {
describe('#callback()', function () {
it ('passport-local authentication should succeed if email and password valid', function (done) {
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'existing.user@email.com',
password: 'admin1234'
})
.expect(200)
.end(function(err) {
done(err);
});
});
it ('passport-local authentication should fail and return error code if email is invalid', function (done) {
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'invalid@email.com',
password: 'admin1234'
})
.expect(403)
.end(function(err) {
done(err);
});
});
it ('passport-local authentication should fail and return error code if password is invalid', function (done) {
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'existing.user@email.com',
password: 'invalid1235'
})
.expect(403)
.end(function(err) {
done(err);
});
});
//Test with Web Sockets from sails.io
describe('sails.socket', function () {
describe('With default settings', function() {
describe('once connected, socket', function () {
it ('passport-local authentication via web socket should succeed if email and password valid', function (done) {
//Socket version?
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'existing.user@email.com',
password: 'admin1234'
})
.expect(200)
.end(function(err) {
done(err);
});
});
it ('passport-local authentication via web socket should fail and return error code if email is invalid', function (done) {
//Socket version?
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'invalid@email.com',
password: 'admin1234'
})
.expect(403)
.end(function(err) {
done(err);
});
});
it ('passport-local authentication via web socket should fail and return error code if password is invalid', function (done) {
//Socket version?
request(sails.hooks.http.app)
.post('/auth/local')
.send({
identifier: 'existing.user@email.com',
password: 'invalid1235'
})
.expect(403)
.end(function(err) {
done(err);
});
});
});
});
});
});
});
source to share
I can't pretend to be this, but if you stumbled upon this question and are looking for an answer here.
Declare io as global in bootstrap:
// test/boostrap.test.js
var client = require('../assets/js/dependencies/sails.io.js');
global.io = new client(require('socket.io-client'));
io.sails.url = 'http://localhost:1337/';
Then call them so that they are used.
//test/callbacks.test.js
describe('socket request', function () {
it ('passport-local authentication should succeed if email and password valid', function (done) {
io.socket.post('/auth/local', { identifier: 'existing.user@email.com', password: 'admin1234' }, function (data, jwres) {
assert.equal(jwres.statusCode, 200);
done();
});
});
source to share
I found that Sails has what I consider to be good documentation on the subject, but not directly in its normal Sails documentation on the subject (although this is a good general reference). I had to read the sails.io.js github project instead . Again, better than the README, there is an example of them. Take a look at this file to see how to configure and disable them for socket testing, and this file which shows the tests themselves.
source to share