Permalink
Browse files

Made the client extend EventEmitter and the http client emit the resp…

…onse object plus corresponding tests
  • Loading branch information...
1 parent 1748ff9 commit 98c1f58957cfdb6dc944cd7d149bf8c7ebf844f9 @tedeh committed Aug 18, 2012
Showing with 25 additions and 7 deletions.
  1. +6 −3 lib/client/http.js
  2. +9 −4 lib/client/index.js
  3. +10 −0 test/http.client-server.test.js
View
@@ -26,23 +26,25 @@ utils.inherits(HttpClient, Client);
module.exports = HttpClient;
HttpClient.prototype._request = function(request, callback) {
+ var self = this;
// copies options so object can be modified in this context
var options = utils.merge({}, this.options);
var body = '';
// stringifies JSON
try { body = JSON.stringify(request, options.replacer); } catch(err) { return callback(err); }
- options.method = 'POST';
- options.headers = {
+ options.method = options.method || 'POST';
+ options.headers = utils.merge({
"content-length": Buffer.byteLength(body, options.encoding),
"content-type": "application/json",
"accept": "application/json"
- };
+ }, options.headers || {});
var req = http.request(options);
req.on('response', function(res) {
+ self.emit('http response', res);
var data = '';
res.setEncoding(options.encoding);
res.on('data', function(chunk) { data += chunk; });
@@ -68,6 +70,7 @@ HttpClient.prototype._request = function(request, callback) {
});
req.on('error', function(err) {
+ self.emit('http error', err);
callback(err);
req.abort();
});
View
@@ -1,9 +1,11 @@
var Server = require('../server');
var utils = require('../utils');
+var events = require('events');
/**
* Constructor for a Jayson Client
* @class Jayson JSON-RPC Client
+ * @extends require('events').EventEmitter
* @param {Server} server An instance of Server
* @param {Object} [options] Optional hash of settings
* @return {Client}
@@ -25,6 +27,7 @@ var Client = function(server, options) {
if(server) this.server = server;
};
+utils.inherits(Client, events.EventEmitter);
module.exports = Client;
@@ -85,16 +88,18 @@ Client.prototype.request = function(method, params, id, callback) {
if(!response) return callback();
var isBatchResponse = Array.isArray(response);
+ var isError = function(res) { return typeof(res.error) !== 'undefined'; };
+ var invert = function(fn) { return function() { return !fn.apply(null, arguments); }; };
// different invocations of passed callback depending on its length
if(callback.length === 3) {
// if it was a batch, split successes and errors in two arrays
if(isBatchResponse) {
callback(null,
- response.filter(function(res) { return res.error; }),
- response.filter(function(res) { return res.hasOwnProperty('result'); })
+ response.filter(isError),
+ response.filter(invert(isError)) // antithesis of the above to ensure everything is returned
);
- } else if(response.error) {
+ } else if(isError(response)) {
callback(null, response.error);
} else {
callback(null, null, response.result);
@@ -109,7 +114,7 @@ Client.prototype.request = function(method, params, id, callback) {
};
/**
- * Executes a request on a directly bound server
+ * Executes a request on server bound directly
* @param {Object} request A JSON-RPC 2.0 request
* @param {Function} [callback] Request callback that will receive the server response as the second argument
* @return {void}
@@ -1,6 +1,7 @@
var should = require('should');
var jayson = require(__dirname + '/..');
var support = require('./support/client-server');
+var ClientResponse = require('http').IncomingMessage;
describe('jayson http', function() {
@@ -42,6 +43,15 @@ describe('jayson http', function() {
it('should be able to handle a batch request', support.clientBatch(client));
+ it('should emit an event with the http response', function(done) {
+ client.on('http response', function(res) {
+ should.exist(res);
+ res.should.be.instanceof(ClientResponse);
+ done();
+ });
+ client.request('add', [9, 4], function(err, response) {});
+ });
+
});
after(function() {

0 comments on commit 98c1f58

Please sign in to comment.