sails.socket 使用 npm 测试进行测试
sails.socket testing with npm test
我正在使用 sails.js v0.11.0,我刚刚开始进行单元测试。我可以通过 http 请求测试普通控制器,但我不知道从哪里开始通过套接字请求测试相同的调用。如果您有很好的资源或使用套接字进行的示例测试,那就太好了。
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);
});
});
});
});
});
});
});
这不能归功于我,但万一您偶然发现了这个问题并正在这里寻找答案。
在 bootstrap 中将 io 声明为全局:
// 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/';
然后像这样调用它们。
//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();
});
});
我发现 Sails 有我认为很好的关于这个主题的文档,但 没有 直接在他们的 normal Sails documentation on the subject (although it's good general knowledge material to have read first). Instead, I had to read their github sails.io.js project. Again, better than the README there is their example. Look in this file to see their setup and teardown for testing sockets, and this file 中展示测试本身。
我正在使用 sails.js v0.11.0,我刚刚开始进行单元测试。我可以通过 http 请求测试普通控制器,但我不知道从哪里开始通过套接字请求测试相同的调用。如果您有很好的资源或使用套接字进行的示例测试,那就太好了。
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);
});
});
});
});
});
});
});
这不能归功于我,但万一您偶然发现了这个问题并正在这里寻找答案。
在 bootstrap 中将 io 声明为全局:
// 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/';
然后像这样调用它们。
//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();
});
});
我发现 Sails 有我认为很好的关于这个主题的文档,但 没有 直接在他们的 normal Sails documentation on the subject (although it's good general knowledge material to have read first). Instead, I had to read their github sails.io.js project. Again, better than the README there is their example. Look in this file to see their setup and teardown for testing sockets, and this file 中展示测试本身。