Skip to content

Commit 1440532

Browse files
committed
Merge branch 'bencxr-allowStringIds'
2 parents 3a1acc6 + efb6c03 commit 1440532

File tree

3 files changed

+87
-54
lines changed

3 files changed

+87
-54
lines changed

package.json

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
{
2-
"name": "json-rpc2",
3-
"version": "0.8.1",
4-
"description": "JSON-RPC 2.0 server and client library, with HTTP, TCP and Websocket endpoints",
5-
"main": "./src/jsonrpc.js",
6-
"keywords": [
7-
"json",
8-
"rpc",
9-
"rpc2",
10-
"json-rpc",
11-
"json-rpc2",
12-
"jsonrpc",
13-
"jsonrpc2",
14-
"server",
15-
"client",
16-
"tcp",
17-
"websocket",
18-
"http"
19-
],
20-
"author": "Eric Florenzano <floguy@gmail.com> (eflorenzano.com)",
21-
"dependencies": {
22-
"jsonparse": "0.x.x",
23-
"debug": "1.x.x",
24-
"lodash": "2.x.x",
25-
"es5class": "2.x.x",
26-
"faye-websocket": "0.x.x",
27-
"eventemitter3": "0.x.x"
28-
},
29-
"engines": {
30-
"node": "0.10.x"
31-
},
32-
"contributors": [
33-
"Bill Casarin <bill@casarin.ca> (jb55.com)",
34-
"Stefan Thomas <justmoon@members.fsf.org> (justmoon.net)",
35-
"Paulo Cesar <email@pocesar.e4ward.com> (github.com/pocesar)"
36-
],
37-
"repository": {
38-
"type": "git",
39-
"url": "git://github.com/pocesar/node-jsonrpc2.git"
40-
},
41-
"devDependencies": {
42-
"mocha": "1.x.x",
43-
"expect.js": "0.x.x",
44-
"jshint": "2.x.x",
45-
"istanbul": "0.x.x"
46-
},
47-
"scripts": {
48-
"test": "jshint examples src test && mocha test/*.js",
49-
"coverage": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -t 5000 test/jsonrpc-test.js"
50-
}
51-
}
1+
{
2+
"name": "json-rpc2",
3+
"version": "0.8.1",
4+
"description": "JSON-RPC 2.0 server and client library, with HTTP, TCP and Websocket endpoints",
5+
"main": "./src/jsonrpc.js",
6+
"keywords": [
7+
"json",
8+
"rpc",
9+
"rpc2",
10+
"json-rpc",
11+
"json-rpc2",
12+
"jsonrpc",
13+
"jsonrpc2",
14+
"server",
15+
"client",
16+
"tcp",
17+
"websocket",
18+
"http"
19+
],
20+
"author": "Eric Florenzano <floguy@gmail.com> (eflorenzano.com)",
21+
"dependencies": {
22+
"jsonparse": "0.x.x",
23+
"debug": "1.x.x",
24+
"lodash": "2.x.x",
25+
"es5class": "2.x.x",
26+
"faye-websocket": "0.x.x",
27+
"eventemitter3": "0.x.x"
28+
},
29+
"engines": {
30+
"node": "0.10.x"
31+
},
32+
"contributors": [
33+
"Bill Casarin <bill@casarin.ca> (jb55.com)",
34+
"Stefan Thomas <justmoon@members.fsf.org> (justmoon.net)",
35+
"Paulo Cesar <email@pocesar.e4ward.com> (github.com/pocesar)"
36+
],
37+
"repository": {
38+
"type": "git",
39+
"url": "git://github.com/pocesar/node-jsonrpc2.git"
40+
},
41+
"devDependencies": {
42+
"mocha": "1.x.x",
43+
"expect.js": "0.x.x",
44+
"jshint": "2.x.x",
45+
"istanbul": "0.x.x"
46+
},
47+
"scripts": {
48+
"test": "jshint examples src test && mocha test/*.js",
49+
"coverage": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -t 5000 test/jsonrpc-test.js"
50+
}
51+
}

src/event-emitter.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ module.exports = function (classes){
1313
return msg;
1414
},
1515
/**
16-
* Check if current request has an integer id
16+
* Check if current request has an id adn it is of type integer (non fractional) or string.
17+
*
1718
* @param {Object} request
1819
* @return {Boolean}
1920
*/
20-
hasId : function (request){
21-
return request && typeof request['id'] !== 'undefined' && /^\-?\d+$/.test(request['id']);
21+
hasId : function (request) {
22+
return request && typeof request['id'] !== 'undefined' &&
23+
(
24+
(typeof(request['id']) === 'number' && /^\-?\d+$/.test(request['id'])) ||
25+
(typeof(request['id']) === 'string') || (request['id'] === null)
26+
);
2227
}
2328
}).$inherit(require('eventemitter3').EventEmitter, []);
2429

test/jsonrpc-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ module.exports = {
174174
expect(decoded.result).to.equal('Hello, World!');
175175
},
176176

177+
'Simple synchronous echo with id as null': function (){
178+
var testJSON = '{ "method": "echo", "params": ["Hello, World!"], "id": null }';
179+
var req = new MockRequest('POST');
180+
var res = new MockResponse();
181+
server.handleHttp(req, res);
182+
req.emit('data', testJSON);
183+
req.emit('end');
184+
expect(res.httpCode).to.equal(200);
185+
var decoded = JSON.parse(res.httpBody);
186+
expect(decoded.id).to.equal(null);
187+
expect(decoded.error).to.equal(undefined);
188+
expect(decoded.result).to.equal('Hello, World!');
189+
},
190+
191+
'Simple synchronous echo with string as id': function (){
192+
var testJSON = '{ "method": "echo", "params": ["Hello, World!"], "id": "test" }';
193+
var req = new MockRequest('POST');
194+
var res = new MockResponse();
195+
server.handleHttp(req, res);
196+
req.emit('data', testJSON);
197+
req.emit('end');
198+
expect(res.httpCode).to.equal(200);
199+
var decoded = JSON.parse(res.httpBody);
200+
expect(decoded.id).to.equal('test');
201+
expect(decoded.error).to.equal(undefined);
202+
expect(decoded.result).to.equal('Hello, World!');
203+
},
204+
177205
'Using promise': function (){
178206
// Expose a function that just returns a promise that we can control.
179207
var callbackRef = null;

0 commit comments

Comments
 (0)