Skip to content

Commit

Permalink
Added missing test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mekwall committed Aug 8, 2014
1 parent c777b15 commit ee76dca
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--require co-mocha
--reporter spec
--ui bdd
--globals callback*
--timeout 5000
8 changes: 8 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
<script src="/koaws.js" />
</head>
<body>
</body>
</html>
81 changes: 81 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var fs = require('fs');
var chai = require('chai');
var expect = chai.expect;

var koa = require('koa');
var middleware = require('../src/middleware');
var app = koa();

app.use(function* (next) {
if (this.path === '/') {
this.set('Content-Type', 'text/html');
this.body = fs.createReadStream(__dirname + '/test.html');
} else {
yield next;
}
});

describe('koa-ws', function () {

it ('should be able to attach middleware to app', function () {
app.use(middleware(app));
expect(app).to.have.property('ws');
});

it ('should be able to register a simple route', function () {
app.ws.register('hello', function* () {
this.result('world');
});
expect(app.ws.methods.hello).to.be.a('function');
});

it ('should be able to register namespaced routes', function () {
app.ws.register('user', {
create: function* () { this.respond('ok'); },
update: function* () { this.respond('ok'); },
delete: function* () { this.respond('ok'); }
});

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 ('should 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();
});
});

it ('websocket client should be able to connect to server', function (done) {
client = require('../src/client');
client.on('open', function () {
done();
});
});

it ('should get world when hello method is called', function (done) {
client.method('hello', function (err, payload) {
expect(err).to.be.a('null');
expect(payload).to.equal('world');
done();
});
});

it ('should be able to call namespaced events', function (done) {
client.method('user:create', function (err, payload) {
expect(err).to.be.a('null');
});

client.method('user:update', function (err, payload) {
expect(err).to.be.a('null');
});

client.method('user:delete', function (err, payload) {
expect(err).to.be.a('null');
done();
});
});

});

0 comments on commit ee76dca

Please sign in to comment.