Permalink
Browse files

Client emits response and corresponding tests plus add jquery client …

…functionality to generate the id
  • Loading branch information...
1 parent 7be19f8 commit aec6f0717c83ef2908dabdbc6ac2e809d8630972 @tedeh committed Sep 21, 2012
Showing with 30 additions and 6 deletions.
  1. +4 −0 lib/client.js
  2. +8 −4 lib/client/jquery.js
  3. +18 −2 test/client.test.js
View
4 lib/client.js
@@ -56,6 +56,8 @@ Client.fork = require('./client/fork');
* @api public
*/
Client.prototype.request = function(method, params, id, callback) {
+ var self = this;
+
// is this a batch request?
var isBatch = Array.isArray(method) && typeof(params) === 'function';
@@ -91,6 +93,8 @@ Client.prototype.request = function(method, params, id, callback) {
this._request(request, function(err, response) {
if(err) return callback(err);
+ self.emit('response', request, response);
+
if(!response) return callback();
var isBatchResponse = Array.isArray(response);
var isError = function(res) { return typeof(res.error) !== 'undefined'; };
View
12 lib/client/jquery.js
@@ -15,6 +15,7 @@
dataType: 'json',
type: 'POST',
processData: false,
+ generator: generateId,
headers: { 'content-type': 'application/json' }
};
@@ -51,7 +52,7 @@
$.fn.jayson = function(options) {
options = options || {};
var client = new JqueryClient(options);
- client.request(options.method, options.params, options.id, function(err, data) {
+ client.request(options.method, options.params, options.id, {generator: options.generator}, function(err, data) {
if(err) {
if($.isFunction(options.error)) {
return options.error.apply(options.error, err);
@@ -77,7 +78,7 @@
* Generates a JSON-RPC 2.0 request
* @see Utils.request
*/
- function generateRequest(method, params, id) {
+ function generateRequest(method, params, id, options) {
if($.type(method) !== 'string') {
throw new TypeError(method + ' must be a string');
}
@@ -86,15 +87,18 @@
throw new TypeError(params + ' must be an object or an array');
}
+ options = options || {};
+
var request = {
jsonrpc: "2.0",
params: params,
method: method
- }
+ };
// if id was left out, generate one (null means explicit notification)
if($.type(id) === 'undefined') {
- request.id = generateId();
+ var generator = typeof(options.generator) === 'function' ? options.generator : generateId;
+ request.id = generator(request);
} else {
request.id = id;
}
View
20 test/client.test.js
@@ -66,12 +66,28 @@ describe('jayson client instance', function() {
});
});
- it('should emit request just as the request is dispatched', function(done) {
+ it('should emit "request" when a request is dispatched', function(done) {
var a = 6, b = 9, hasFired = false;
client.once('request', function(request) {
hasFired = true;
should.exist(request);
- request.params.should.include(6).and.include(9).and.have.lengthOf(2);
+ request.params.should.include(a).and.include(b).and.have.lengthOf(2);
+ });
+ client.request('add', [a, b], function(err) {
+ if(err) return done(err);
+ hasFired.should.be.ok;
+ done();
+ });
+ });
+
+ it('should emit "response" when a response is received', function(done) {
+ var a = 8, b = 10, hasFired = false;
+ client.once('response', function(request, response) {
+ hasFired = true;
+ should.exist(request);
+ request.params.should.include(a).and.include(b).and.have.lengthOf(2);
+ should.exist(response);
+ response.should.have.property('result', a + b);
});
client.request('add', [a, b], function(err) {
if(err) return done(err);

0 comments on commit aec6f07

Please sign in to comment.