Skip to content

Commit

Permalink
Merge pull request #10 from golya/master
Browse files Browse the repository at this point in the history
handle different data types at request
  • Loading branch information
mekwall committed Sep 28, 2015
2 parents e539987 + 8b96b86 commit b3644d2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
19 changes: 19 additions & 0 deletions gulpfile.js
@@ -0,0 +1,19 @@
var gulp = require('gulp');
var mocha = require('gulp-mocha');

var paths = {
src: 'src/*.js',
test: 'test/*.js'
};

gulp.task('default', ['test']);

gulp.task('test', function () {
return gulp.src('test/*.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('watch', function () {
gulp.watch(paths.src, ['test']);
gulp.watch(paths.test, ['test']);
});
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -41,6 +41,8 @@
"istanbul": "git://github.com/mekwall/istanbul.git",
"koa": "^0.17.0",
"koa-generic-session": "^1.6.0",
"gulp": "^3.9.0",
"gulp-mocha": "^2.1.3",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.1",
"supertest": "^0.15.0"
Expand Down
18 changes: 16 additions & 2 deletions src/jsonrpc.js
@@ -1,6 +1,19 @@
var Request = require('./request');

module.exports = function (debug, socket, data) {
module.exports = function(debug, socket, data) {
try {
return request.call(this, debug, socket, data);
} catch (e){
console.error('Something went wrong: ', e.stack);
return;
}
};

function request(debug, socket, data) {
if (typeof data != 'string') {
data = data.data;
}

// If heartbeat, respond
if (data === '--thump--') {
debug('← Thump!');
Expand Down Expand Up @@ -85,4 +98,5 @@ module.exports = function (debug, socket, data) {
socket.error.apply(request, [-32601, 'Method not found']);
}

};
return;
}
3 changes: 3 additions & 0 deletions src/middleware.js
Expand Up @@ -22,6 +22,9 @@ module.exports = function (app, passedOptions) {
debug('Attaching server...')
app.server = oldListen.apply(app, arguments);
app.ws.listen(app.server);
app.address = function() {
return app.server.address();
}
return app;
};

Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Expand Up @@ -42,6 +42,13 @@ describe('koa-ws', function () {
expect(app).to.have.property('ws');
});

it('expect provide address', function () {
var address = app.listen().address();
expect(address.address).to.equal('::');
expect(address.family).to.be.a('string');
expect(address.port).to.be.a('number');
});

it('expect to be able to register a simple server method', function () {
app.ws.register('hello', function* () {
this.result('world');
Expand Down Expand Up @@ -234,3 +241,9 @@ describe('protocol', function () {
});

});

describe('http server', function () {
it('expect close connection', function () {
app.server.close();
});
});

0 comments on commit b3644d2

Please sign in to comment.