Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "koa-ws", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"keywords": [ | ||
@@ -43,7 +43,7 @@ "koa", | ||
"koa": "^0.17.0", | ||
"koa-generic-session": "^1.2.0", | ||
"koa-generic-session": "^1.6.0", | ||
"mocha": "^2.1.0", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"supertest": "^0.13.0" | ||
"supertest": "^0.15.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://secure.travis-ci.org/mekwall/koa-ws.png)](http://travis-ci.org/mekwall/koa-ws) [![Coverage Status](https://img.shields.io/coveralls/mekwall/koa-ws.svg)](https://coveralls.io/r/mekwall/koa-ws) [![Dependency Status](https://david-dm.org/mekwall/koa-ws.png)](https://david-dm.org/mekwall/koa-ws) | ||
[![Build Status](https://secure.travis-ci.org/mekwall/koa-ws.png)](http://travis-ci.org/mekwall/koa-ws) [![npm version](https://badge.fury.io/js/koa-ws.svg)](http://badge.fury.io/js/koa-ws) [![Coverage Status](https://img.shields.io/coveralls/mekwall/koa-ws.svg)](https://coveralls.io/r/mekwall/koa-ws) [![Dependency Status](https://david-dm.org/mekwall/koa-ws.png)](https://david-dm.org/mekwall/koa-ws) | ||
@@ -3,0 +3,0 @@ # koa-ws |
@@ -82,3 +82,3 @@ (function(){ | ||
}); | ||
}; | ||
} | ||
@@ -123,3 +123,3 @@ // Inherit prototype from EventEmitter | ||
if (this.socket.on) { | ||
this.socket.on(type, handler) | ||
this.socket.on(type, handler); | ||
} else if (!this.socket['on' + type]) { | ||
@@ -131,10 +131,17 @@ this.socket['on' + type] = handler; | ||
Client.prototype.disconnect = function (code, reason) { | ||
if (this.socket) { | ||
this.socket.close(code, reason); | ||
} | ||
}; | ||
// Register a client-side method | ||
Client.prototype.register = function (method, handler, expose) { | ||
var m; | ||
if (typeof method === 'object') { | ||
for (var m in method) { | ||
for (m in method) { | ||
this.register(m, method[m]); | ||
} | ||
} else if (typeof handler === 'object') { | ||
for (var m in handler) { | ||
for (m in handler) { | ||
this.register(method + ':' + m, handler[m]); | ||
@@ -141,0 +148,0 @@ } |
275
test/test.js
@@ -9,4 +9,5 @@ var fs = require('fs'); | ||
var session = require('koa-generic-session'); | ||
// Setup koa app | ||
var app = koa(); | ||
app.keys = ['foo', 'bar']; | ||
@@ -29,163 +30,209 @@ app.sessionStore = new session.MemoryStore(); | ||
var socket; | ||
describe('koa-ws', function () { | ||
it('expect to be able to attach middleware to app', function () { | ||
describe('server', function () { | ||
var socket; | ||
var client = require('../src/client'); | ||
var middleware = require('../src/middleware'); | ||
app.use(middleware(app, { | ||
heartbeatInterval: 500, | ||
heartbeat: false | ||
})); | ||
expect(app).to.have.property('ws'); | ||
}); | ||
it('expect to be able to register a simple server method', function () { | ||
app.ws.register('hello', function* () { | ||
this.result('world'); | ||
it('expect to be able to attach middleware to app', function () { | ||
app.use(middleware(app, { | ||
heartbeatInterval: 500, | ||
heartbeat: false | ||
})); | ||
expect(app).to.have.property('ws'); | ||
}); | ||
expect(app.ws._methods.hello).to.be.a('function'); | ||
}); | ||
it('expect to be able to register namespaced server methods', function () { | ||
app.ws.register('user', { | ||
create: function* () { this.result('ok'); }, | ||
update: function* () { this.result('ok'); }, | ||
delete: function* () { this.result('ok'); } | ||
it('expect to be able to register a simple server method', function () { | ||
app.ws.register('hello', function* () { | ||
this.result('world'); | ||
}); | ||
expect(app.ws._methods.hello).to.be.a('function'); | ||
}); | ||
expect(app.ws._methods['user:create']).to.be.a('function'); | ||
expect(app.ws._methods['user:update']).to.be.a('function'); | ||
expect(app.ws._methods['user:update']).to.be.a('function'); | ||
}); | ||
it('expect to be able to register namespaced server methods', function () { | ||
app.ws.register('user', { | ||
create: function* () { this.result('ok'); }, | ||
update: function* () { this.result('ok'); }, | ||
delete: function* () { this.result('ok'); } | ||
}); | ||
it('expect server to be able to listen to the same port as koa server', function (done) { | ||
app.listen(3000, function () { | ||
expect(app.ws.server._server).to.be.a('object'); | ||
done(); | ||
expect(app.ws._methods['user:create']).to.be.a('function'); | ||
expect(app.ws._methods['user:update']).to.be.a('function'); | ||
expect(app.ws._methods['user:update']).to.be.a('function'); | ||
}); | ||
// Connect and generate session | ||
request(app.server).get('/'); | ||
}); | ||
it('expect websocket client to be able to connect to server', function (done) { | ||
app.ws.server.on('connection', function (wss) { | ||
socket = wss; | ||
it('expect server to be able to listen to the same port as koa server', function (done) { | ||
app.listen(3000, function () { | ||
expect(app.ws.server._server).to.be.a('object'); | ||
done(); | ||
}); | ||
// Connect and generate session | ||
request(app.server).get('/'); | ||
}); | ||
client = require('../src/client'); | ||
client.connect(); | ||
client.on('open', function () { | ||
done(); | ||
it('expect server to handle a connection', function (done) { | ||
app.ws.server.once('connection', function (wss) { | ||
expect(wss).to.be.a('object'); | ||
socket = wss; | ||
done(); | ||
}); | ||
client.connect(); | ||
}); | ||
}); | ||
it('expect client to recieve result world when hello server method is called', function (done) { | ||
client.method('hello', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('world'); | ||
done(); | ||
}); | ||
}); | ||
it('expect to be able to register namespaced client methods', function () { | ||
client.register('user', { | ||
create: function () { this.result('ok'); }, | ||
update: function () { this.result('ok'); }, | ||
delete: function () { this.result('ok'); } | ||
}); | ||
it('excpect client to be able to call namespaced server methods', function (done) { | ||
client.method('user:create', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
expect(client._methods['user:create']).to.be.a('function'); | ||
expect(client._methods['user:update']).to.be.a('function'); | ||
expect(client._methods['user:update']).to.be.a('function'); | ||
}); | ||
client.method('user:update', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
it('expect server to be able to call namespaced client methods', function (done) { | ||
socket.method('user:create', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
}); | ||
socket.method('user:update', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
}); | ||
socket.method('user:delete', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
done(); | ||
}); | ||
}); | ||
client.method('user:delete', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
done(); | ||
it('expect to be able to serve the client library at /koaws.js', function (done) { | ||
request(app.server) | ||
.get('/koaws.js') | ||
.expect(200) | ||
.expect('Content-Type', 'application/javascript') | ||
.end(function (err, res) { | ||
if (err) throw err; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('expect to be able to register namespaced client methods', function () { | ||
client.register('user', { | ||
create: function () { this.result('ok'); }, | ||
update: function () { this.result('ok'); }, | ||
delete: function () { this.result('ok'); } | ||
it('expect heartbeat response from client', function (done) { | ||
socket.once('message', function (message) { | ||
if (message === '--thump--') { | ||
done(); | ||
} | ||
}); | ||
socket.send('--thump--'); | ||
}); | ||
expect(client._methods['user:create']).to.be.a('function'); | ||
expect(client._methods['user:update']).to.be.a('function'); | ||
expect(client._methods['user:update']).to.be.a('function'); | ||
}); | ||
it('expect server to be able to call namespaced client methods', function (done) { | ||
socket.method('user:create', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
describe('client', function () { | ||
var socket; | ||
var client = require('../src/client'); | ||
it('expect client to be able to connect to server', function (done) { | ||
app.ws.server.once('connection', function (wss) { | ||
expect(wss).to.be.a('object'); | ||
socket = wss; | ||
}); | ||
client.once('open', function () { | ||
done(); | ||
}); | ||
client.connect(); | ||
}); | ||
socket.method('user:update', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
it('expect client to be able to disconnect from server', function (done) { | ||
client.once('close', function () { | ||
client.connect(); | ||
done(); | ||
}); | ||
client.disconnect(); | ||
}); | ||
socket.method('user:delete', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
done(); | ||
it('expect client to recieve result world when hello server method is called', function (done) { | ||
client.method('hello', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('world'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('expect server to return error -32700 parse error', function (done) { | ||
client.once('message', function (payload) { | ||
var payload = JSON.parse(payload); | ||
expect(payload.error.code).to.be.equal(-32700); | ||
done(); | ||
it('expect client to be able to call namespaced server methods', function (done) { | ||
client.method('user:create', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
}); | ||
client.method('user:update', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
}); | ||
client.method('user:delete', function (err, payload) { | ||
expect(err).to.be.a('null'); | ||
expect(payload).to.equal('ok'); | ||
done(); | ||
}); | ||
}); | ||
client.socket.send("{ foo: 'bar' }"); | ||
}); | ||
it('expect server to return error -32600 invalid request', function (done) { | ||
client.once('message', function (payload) { | ||
var payload = JSON.parse(payload); | ||
expect(payload.error.code).to.be.equal(-32600); | ||
done(); | ||
it('expect heartbeat response from server', function (done) { | ||
client.once('message', function (message) { | ||
if (message === '--thump--') { | ||
done(); | ||
} | ||
}); | ||
client.socket.send('--thump--'); | ||
}); | ||
client.socket.send(JSON.stringify({ foo: 'bar' })); | ||
}); | ||
it('expect server to return error -32602 invalid params', function (done) { | ||
client.method('hello', 'world!', function (err, payload) { | ||
expect(err.code).to.be.equal(-32602); | ||
done(); | ||
}); | ||
describe('protocol', function () { | ||
describe('jsonrpc 2.0', function () { | ||
var client = require('../src/client'); | ||
it('expect server to return error -32700 parse error', function (done) { | ||
client.once('message', function (payload) { | ||
payload = JSON.parse(payload); | ||
expect(payload.error.code).to.be.equal(-32700); | ||
done(); | ||
}); | ||
client.socket.send("{ foo: 'bar' }"); | ||
}); | ||
}); | ||
it('expect server to return error -32601 method not found', function (done) { | ||
client.method('foo', function (err, payload) { | ||
expect(err.code).to.be.equal(-32601); | ||
done(); | ||
it('expect server to return error -32600 invalid request', function (done) { | ||
client.once('message', function (payload) { | ||
payload = JSON.parse(payload); | ||
expect(payload.error.code).to.be.equal(-32600); | ||
done(); | ||
}); | ||
client.socket.send(JSON.stringify({ foo: 'bar' })); | ||
}); | ||
}); | ||
it('expect to be able to serve the client library at /koaws.js', function (done) { | ||
request(app.server) | ||
.get('/koaws.js') | ||
.expect(200) | ||
.expect('Content-Type', 'application/javascript') | ||
.end(function (err, res) { | ||
if (err) throw err; | ||
it('expect server to return error -32602 invalid params', function (done) { | ||
client.method('hello', 'world!', function (err, payload) { | ||
expect(err.code).to.be.equal(-32602); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('expect heartbeat response from client', function (done) { | ||
socket.once('message', function (message) { | ||
if (message === '--thump--') { | ||
it('expect server to return error -32601 method not found', function (done) { | ||
client.method('foo', function (err, payload) { | ||
expect(err.code).to.be.equal(-32601); | ||
done(); | ||
} | ||
}); | ||
}); | ||
socket.send('--thump--'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36025
16
704