Merge branch 'pr', closes #138

This commit is contained in:
Spencer Alger
2014-09-04 14:26:59 -07:00
3 changed files with 95 additions and 6 deletions

View File

@ -53,7 +53,7 @@ Check out the [Browser Builds](http://www.elasticsearch.org/guide/en/elasticsear
## Supported Elasticsearch Versions ## Supported Elasticsearch Versions
[![Supporting Elasticsearch Version 0.90 to 1.3](http://img.shields.io/badge/elasticsearch-0.90--1.3-green.svg)](http://build.elasticsearch.com/job/es-js_nightly/) [![Supporting Elasticsearch Version 0.90 to 1.3](http://img.shields.io/badge/elasticsearch-0.90--1.3-green.svg)](http://build-eu-1.elasticsearch.com/job/es-js_nightly/)
Elasticsearch.js provides support for, and is regularly tested against, Elasticsearch releases 0.90.12 and greater. We also test against the latest changes in several branches in the Elasticsearch repository. To tell the client which version of Elastisearch you are using, and therefore the API it should provide, set the `apiVersion` config param. [More info](http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/configuration.html#_config_options) Elasticsearch.js provides support for, and is regularly tested against, Elasticsearch releases 0.90.12 and greater. We also test against the latest changes in several branches in the Elasticsearch repository. To tell the client which version of Elastisearch you are using, and therefore the API it should provide, set the `apiVersion` config param. [More info](http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/configuration.html#_config_options)

View File

@ -16,6 +16,7 @@ var _ = require('../utils');
var qs = require('querystring'); var qs = require('querystring');
var ForeverAgent = require('./_custom_agent'); var ForeverAgent = require('./_custom_agent');
var ConnectionAbstract = require('../connection'); var ConnectionAbstract = require('../connection');
var zlib = require('zlib');
/** /**
* Connector used to talk to an elasticsearch node via HTTP * Connector used to talk to an elasticsearch node via HTTP
@ -123,8 +124,9 @@ HttpConnector.prototype.request = function (params, cb) {
var request; var request;
var response; var response;
var status = 0; var status = 0;
var headers; var headers = {};
var log = this.log; var log = this.log;
var buffers = [];
var reqParams = this.makeReqParams(params); var reqParams = this.makeReqParams(params);
@ -144,7 +146,21 @@ HttpConnector.prototype.request = function (params, cb) {
if (err) { if (err) {
cb(err); cb(err);
} else { } else {
response = Buffer.concat(buffers);
var zipHdr = headers['content-encoding'];
if (zipHdr && (zipHdr.match(/gzip/i) || zipHdr.match(/deflate/i))) {
zlib.unzip(response, function(gzErr, uncompressedResponse) {
if(gzErr) {
err = gzErr;
response = response.toString('binary');
} else {
response = uncompressedResponse.toString('utf8');
}
cb(err, response, status, headers); cb(err, response, status, headers);
});
} else {
cb(err, response.toString('utf8'), status, headers);
}
} }
}, this); }, this);
@ -152,11 +168,10 @@ HttpConnector.prototype.request = function (params, cb) {
incoming = _incoming; incoming = _incoming;
status = incoming.statusCode; status = incoming.statusCode;
headers = incoming.headers; headers = incoming.headers;
incoming.setEncoding('utf8');
response = ''; response = '';
buffers = [];
incoming.on('data', function (d) { incoming.on('data', function (d) {
response += d; buffers.push(new Buffer(d));
}); });
incoming.on('error', cleanUp); incoming.on('error', cleanUp);

View File

@ -17,6 +17,8 @@ describe('Http Connector', function () {
var expectSubObject = require('../../utils/expect_sub_object'); var expectSubObject = require('../../utils/expect_sub_object');
var MockRequest = require('../../mocks/request'); var MockRequest = require('../../mocks/request');
var MockIncommingMessage = require('../../mocks/incomming_message'); var MockIncommingMessage = require('../../mocks/incomming_message');
var zlib = require('zlib');
var estr = require('event-stream');
nock.disableNetConnect(); nock.disableNetConnect();
@ -302,6 +304,78 @@ describe('Http Connector', function () {
}); });
}); });
it('collects the whole request body (gzip compressed)', function (done) {
var server = nock('http://esjs.com:9200');
var con = new HttpConnection(new Host('http://esjs.com:9200'));
var elements = [];
for(var i = 0; i < 500; i++) {
elements.push({ "USER": "doc" });
}
var body = JSON.stringify(elements);
zlib.gzip(body, function(err, compressedBody) {
server
.get('/users/1')
.reply(200, compressedBody, {'Content-Encoding': 'gzip'});
con.request({
method: 'GET',
path: '/users/1'
}, function (err, resp, status) {
expect(err).to.be(undefined);
expect(resp).to.eql(body);
expect(status).to.eql(200);
server.done();
done();
});
});
});
it('collects the whole request body (deflate compressed)', function (done) {
var server = nock('http://esjs.com:9200');
var con = new HttpConnection(new Host('http://esjs.com:9200'));
var elements = [];
for(var i = 0; i < 500; i++) {
elements.push({ "USER": "doc" });
}
var body = JSON.stringify(elements);
zlib.deflate(body, function(err, compressedBody) {
server
.get('/users/1')
.reply(200, compressedBody, {'Content-Encoding': 'deflate'});
con.request({
method: 'GET',
path: '/users/1'
}, function (err, resp, status) {
expect(err).to.be(undefined);
expect(resp).to.eql(body);
expect(status).to.eql(200);
server.done();
done();
});
});
});
it('Can handle uncompress errors', function (done) {
var server = nock('http://esjs.com:9200');
var con = new HttpConnection(new Host('http://esjs.com:9200'));
var body = 'blah';
server
.get('/users/1')
.reply(200, body, {'Content-Encoding': 'gzip'});
con.request({
method: 'GET',
path: '/users/1'
}, function (err, resp, status) {
expect(err.errno).to.be(-3);
expect(resp).to.eql(body);
expect(status).to.eql(200);
server.done();
done();
});
});
it('Ignores serialization errors', function (done) { it('Ignores serialization errors', function (done) {
var server = nock('http://esjs.com:9200'); var server = nock('http://esjs.com:9200');
var con = new HttpConnection(new Host('http://esjs.com:9200')); var con = new HttpConnection(new Host('http://esjs.com:9200'));