setup testling
This commit is contained in:
@ -7,6 +7,7 @@ var util = require('util');
|
||||
var MockWritableStream; // defined simply for 0.10+, in detail for older versions
|
||||
var Writable = require('stream').Writable;
|
||||
|
||||
|
||||
if (Writable) {
|
||||
// nice and simple for streams 2
|
||||
MockWritableStream = module.exports = function (opts) {
|
||||
@ -15,63 +16,63 @@ if (Writable) {
|
||||
this._write = function (chunk, encoding, cb) {};
|
||||
};
|
||||
util.inherits(MockWritableStream, Writable);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Node < 0.10 did not provide a usefull stream abstract
|
||||
var Stream = require('stream').Stream;
|
||||
module.exports = MockWritableStream = function (opts) {
|
||||
Stream.call(this);
|
||||
this.writable = true;
|
||||
};
|
||||
util.inherits(MockWritableStream, Stream);
|
||||
|
||||
// Node < 0.10 did not provide a usefull stream abstract
|
||||
var Stream = require('stream').Stream;
|
||||
module.exports = MockWritableStream = function (opts) {
|
||||
Stream.call(this);
|
||||
this.writable = true;
|
||||
};
|
||||
util.inherits(MockWritableStream, Stream);
|
||||
|
||||
MockWritableStream.prototype.write = function (data) {
|
||||
if (!this.writable) {
|
||||
this.emit('error', new Error('stream not writable'));
|
||||
return false;
|
||||
}
|
||||
|
||||
var cb;
|
||||
if (typeof(arguments[arguments.length - 1]) === 'function') {
|
||||
cb = arguments[arguments.length - 1];
|
||||
}
|
||||
|
||||
if (cb) {
|
||||
process.nextTick(cb);
|
||||
}
|
||||
};
|
||||
|
||||
MockWritableStream.prototype.end = function (data, encoding, cb) {
|
||||
if (typeof(data) === 'function') {
|
||||
cb = data;
|
||||
} else if (typeof(encoding) === 'function') {
|
||||
cb = encoding;
|
||||
this.write(data);
|
||||
} else if (arguments.length > 0) {
|
||||
this.write(data, encoding);
|
||||
}
|
||||
this.writable = false;
|
||||
};
|
||||
|
||||
MockWritableStream.prototype.destroy = function (cb) {
|
||||
var self = this;
|
||||
|
||||
if (!this.writable) {
|
||||
if (cb) {
|
||||
process.nextTick(function () { cb(null); });
|
||||
MockWritableStream.prototype.write = function (data) {
|
||||
if (!this.writable) {
|
||||
this.emit('error', new Error('stream not writable'));
|
||||
return false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.writable = false;
|
||||
|
||||
process.nextTick(function () {
|
||||
if (cb) {
|
||||
cb(null);
|
||||
var cb;
|
||||
if (typeof(arguments[arguments.length - 1]) === 'function') {
|
||||
cb = arguments[arguments.length - 1];
|
||||
}
|
||||
self.emit('close');
|
||||
});
|
||||
};
|
||||
|
||||
// There is no shutdown() for files.
|
||||
MockWritableStream.prototype.destroySoon = MockWritableStream.prototype.end;
|
||||
if (cb) {
|
||||
process.nextTick(cb);
|
||||
}
|
||||
};
|
||||
|
||||
MockWritableStream.prototype.end = function (data, encoding, cb) {
|
||||
if (typeof(data) === 'function') {
|
||||
cb = data;
|
||||
} else if (typeof(encoding) === 'function') {
|
||||
cb = encoding;
|
||||
this.write(data);
|
||||
} else if (arguments.length > 0) {
|
||||
this.write(data, encoding);
|
||||
}
|
||||
this.writable = false;
|
||||
};
|
||||
|
||||
MockWritableStream.prototype.destroy = function (cb) {
|
||||
var self = this;
|
||||
|
||||
if (!this.writable) {
|
||||
if (cb) {
|
||||
process.nextTick(function () { cb(null); });
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.writable = false;
|
||||
|
||||
process.nextTick(function () {
|
||||
if (cb) {
|
||||
cb(null);
|
||||
}
|
||||
self.emit('close');
|
||||
});
|
||||
};
|
||||
|
||||
// There is no shutdown() for files.
|
||||
MockWritableStream.prototype.destroySoon = MockWritableStream.prototype.end;
|
||||
|
||||
}
|
||||
20369
test/unit/angular-1.2.5.js
Normal file
20369
test/unit/angular-1.2.5.js
Normal file
File diff suppressed because it is too large
Load Diff
62
test/unit/browser_test_angular_build.js
Normal file
62
test/unit/browser_test_angular_build.js
Normal file
@ -0,0 +1,62 @@
|
||||
/* jshint browser:true */
|
||||
/* global angular */
|
||||
|
||||
var should = require('should');
|
||||
var Client = require('../../src/lib/client');
|
||||
|
||||
describe('Angular esFactory', function () {
|
||||
before(function (done) {
|
||||
// inject angular
|
||||
var scr = document.createElement('script');
|
||||
scr.src = '/test/unit/angular-1.2.5.js';
|
||||
scr.async = true;
|
||||
scr.onload = function () {
|
||||
require('../../src/elasticsearch.angular.js');
|
||||
done();
|
||||
};
|
||||
scr.type = 'text/javascript';
|
||||
document.getElementsByTagName('head')[0].appendChild(scr);
|
||||
});
|
||||
|
||||
var uuid = (function () { var i = 0; return function () { return ++i; }; }());
|
||||
function directive(makeDirective) {
|
||||
var root = document.createElement('div');
|
||||
var id = uuid();
|
||||
root.setAttribute('test-directive-' + id, 'test-directive');
|
||||
document.body.appendChild(root);
|
||||
|
||||
after(function () {
|
||||
document.body.removeChild(root);
|
||||
root = null;
|
||||
});
|
||||
|
||||
angular.module('mod' + id, ['elasticsearch']).directive('testDirective' + id, makeDirective);
|
||||
angular.bootstrap(root, ['mod' + id]);
|
||||
}
|
||||
|
||||
it('is available in the elasticsearch module', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory.should.have.type('function');
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
it('has Transport and ConnectionPool properties', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory.should.have.property('Transport');
|
||||
esFactory.should.have.property('ConnectionPool');
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
it('returns a new client when it is called', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory({ log: null }).should.be.an.instanceOf(Client);
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
62
test/unit/browser_test_generic_build.js
Normal file
62
test/unit/browser_test_generic_build.js
Normal file
@ -0,0 +1,62 @@
|
||||
/* jshint browser:true */
|
||||
/* global angular */
|
||||
|
||||
var should = require('should');
|
||||
var Client = require('../../src/lib/client');
|
||||
|
||||
describe('Angular esFactory', function () {
|
||||
before(function (done) {
|
||||
// inject angular
|
||||
var scr = document.createElement('script');
|
||||
scr.src = '/test/unit/angular-1.2.5.js';
|
||||
scr.async = true;
|
||||
scr.onload = function () {
|
||||
require('../../src/elasticsearch.angular.js');
|
||||
done();
|
||||
};
|
||||
scr.type = 'text/javascript';
|
||||
document.getElementsByTagName('head')[0].appendChild(scr);
|
||||
});
|
||||
|
||||
var uuid = (function () { var i = 0; return function () { return ++i; }; }());
|
||||
function directive(makeDirective) {
|
||||
var root = document.createElement('div');
|
||||
var id = uuid();
|
||||
root.setAttribute('test-directive-' + id, 'test-directive');
|
||||
document.body.appendChild(root);
|
||||
|
||||
after(function () {
|
||||
document.body.removeChild(root);
|
||||
root = null;
|
||||
});
|
||||
|
||||
angular.module('mod' + id, ['elasticsearch']).directive('testDirective' + id, makeDirective);
|
||||
angular.bootstrap(root, ['mod' + id]);
|
||||
}
|
||||
|
||||
it('is available in the elasticsearch module', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory.should.have.type('function');
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
it('has Transport and ConnectionPool properties', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory.should.have.property('Transport');
|
||||
esFactory.should.have.property('ConnectionPool');
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
it('returns a new client when it is called', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
esFactory({ log: null }).should.be.an.instanceOf(Client);
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
0
test/unit/browser_test_jquery_build.js
Normal file
0
test/unit/browser_test_jquery_build.js
Normal file
30
test/unit/run_browser_builds.js
Normal file
30
test/unit/run_browser_builds.js
Normal file
@ -0,0 +1,30 @@
|
||||
/* jshint browser:true */
|
||||
|
||||
// hard requires so that browserify can grab them
|
||||
require('./test_abstract_logger');
|
||||
require('./test_client');
|
||||
require('./test_client_action');
|
||||
require('./test_connection_abstract');
|
||||
require('./test_connection_pool');
|
||||
require('./test_console_logger');
|
||||
require('./test_errors');
|
||||
// require('./test_file_logger');
|
||||
require('./test_host');
|
||||
// require('./test_http_connector');
|
||||
require('./test_json_serializer');
|
||||
require('./test_log');
|
||||
require('./test_nodes_to_host_callback');
|
||||
require('./test_random_selector');
|
||||
require('./test_round_robin_selector');
|
||||
// require('./test_stdio_logger');
|
||||
// require('./test_stream_logger');
|
||||
// require('./test_tracer_logger');
|
||||
require('./test_transport');
|
||||
// require('./test_transport_with_server');
|
||||
require('./test_utils');
|
||||
|
||||
|
||||
// browser build tests
|
||||
require('./browser_test_generic_build');
|
||||
require('./browser_test_angular_build');
|
||||
require('./browser_test_jquery_build');
|
||||
@ -4,7 +4,6 @@ var errors = require('../../src/lib/errors');
|
||||
var when = require('when');
|
||||
|
||||
var sinon = require('sinon');
|
||||
var nock = require('../mocks/server.js');
|
||||
var should = require('should');
|
||||
var _ = require('lodash');
|
||||
var nodeList = require('../fixtures/short_node_list.json');
|
||||
@ -491,215 +490,7 @@ describe('Transport Class', function () {
|
||||
it('responds when there are no retries', testRetries(0));
|
||||
});
|
||||
|
||||
describe('server responds', function () {
|
||||
var serverMock;
|
||||
|
||||
before(function () {
|
||||
serverMock = nock('http://localhost')
|
||||
.get('/give-me-400')
|
||||
.reply(400, 'sorry bub')
|
||||
|
||||
.get('/give-me-404')
|
||||
.times(2)
|
||||
.reply(404, 'nothing here')
|
||||
|
||||
.get('/give-me-500')
|
||||
.reply(500, 'ah shit')
|
||||
|
||||
.get('/exists?')
|
||||
.reply(200, {
|
||||
status: 200
|
||||
})
|
||||
|
||||
.get('/give-me-someth')
|
||||
.reply(200, '{"not":"valid', {
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
'the answer': 42
|
||||
})
|
||||
|
||||
.get('/huh?')
|
||||
.reply(530, 'boo')
|
||||
|
||||
.get('/hottie-threads')
|
||||
.reply(200, [
|
||||
'he said',
|
||||
'she said',
|
||||
'he said',
|
||||
'she said',
|
||||
'he said',
|
||||
'she said'
|
||||
].join('\n'), {
|
||||
'Content-Type': 'text/plain'
|
||||
});
|
||||
});
|
||||
|
||||
after(function () {
|
||||
serverMock.done();
|
||||
});
|
||||
|
||||
describe('with a 400 status code', function () {
|
||||
it('passes back a 400/BadRequest error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-400'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[400]);
|
||||
err.should.be.an.instanceOf(errors.BadRequest);
|
||||
body.should.eql('sorry bub');
|
||||
status.should.eql(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 404 status code', function () {
|
||||
describe('and castExists is set', function () {
|
||||
it('sends back false', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-404',
|
||||
castExists: true
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql(false);
|
||||
status.should.eql(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('and the castExists param is not set', function () {
|
||||
it('sends back a 404/NotFound error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-404'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[404]);
|
||||
err.should.be.an.instanceOf(errors.NotFound);
|
||||
body.should.eql('nothing here');
|
||||
status.should.eql(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 500 status code', function () {
|
||||
it('passes back a 500/InternalServerError error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-500'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[500]);
|
||||
err.should.be.an.instanceOf(errors.InternalServerError);
|
||||
body.should.eql('ah shit');
|
||||
status.should.eql(500);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 530 status code', function () {
|
||||
it('passes back a Generic error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/huh?'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors.Generic);
|
||||
body.should.eql('boo');
|
||||
status.should.eql(530);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 200 status code', function () {
|
||||
describe('and the castExists param is set', function () {
|
||||
it('sends back true', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/exists?',
|
||||
castExists: true
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql(true);
|
||||
status.should.eql(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('with a partial response body', function () {
|
||||
it('sends back a serialization error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-someth',
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors.Serialization);
|
||||
body.should.eql('{"not":"valid');
|
||||
status.should.eql(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('with a valid response body', function () {
|
||||
it('sends back the body and status code with no error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/',
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql({
|
||||
'the answer': 42
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with plain text', function () {
|
||||
it('notices the content-type header and returns the text', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/hottie-threads',
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.match(/s?he said/g);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('return value', function () {
|
||||
it('returns an object with an abort() method when a callback is sent', function () {
|
||||
@ -733,24 +524,6 @@ describe('Transport Class', function () {
|
||||
ret.should.be.exactly(fakePromise);
|
||||
ret.abort.should.have.type('function');
|
||||
});
|
||||
it('resolves the promise it with the response body', function (done) {
|
||||
var serverMock = nock('http://esbox.1.com')
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
good: 'day'
|
||||
});
|
||||
|
||||
var tran = new Transport({
|
||||
hosts: 'http://esbox.1.com'
|
||||
});
|
||||
|
||||
tran.request({}).then(function (resp) {
|
||||
resp.should.eql({
|
||||
good: 'day'
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('aborting', function () {
|
||||
@ -833,48 +606,8 @@ describe('Transport Class', function () {
|
||||
});
|
||||
clock.restore();
|
||||
});
|
||||
it('clears the timeout when the request is complete', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
|
||||
var server = nock('http://localhost:9200')
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
i: 'am here'
|
||||
});
|
||||
|
||||
tran.request({}, function (err, resp, status) {
|
||||
should.not.exist(err);
|
||||
resp.should.eql({ i: 'am here' });
|
||||
status.should.eql(200);
|
||||
Object.keys(clock.timeouts).should.have.length(0);
|
||||
clock.restore();
|
||||
});
|
||||
});
|
||||
it('timeout responds with a requestTimeout error', function (done) {
|
||||
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
|
||||
var server = nock('http://localhost:9200')
|
||||
.get('/')
|
||||
.delay(1000)
|
||||
.reply(200, {
|
||||
i: 'am here'
|
||||
});
|
||||
|
||||
tran.request({
|
||||
requestTimeout: 25
|
||||
}, function (err, resp, status) {
|
||||
err.should.be.an.instanceOf(errors.RequestTimeout);
|
||||
// Object.keys(clock.timeouts).should.have.length(0);
|
||||
// clock.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
[false, 0, null].forEach(function (falsy) {
|
||||
it('skips the timeout when it is ' + falsy, function () {
|
||||
var clock = sinon.useFakeTimers();
|
||||
|
||||
307
test/unit/test_transport_with_server.js
Normal file
307
test/unit/test_transport_with_server.js
Normal file
@ -0,0 +1,307 @@
|
||||
var Transport = require('../../src/lib/transport');
|
||||
var Host = require('../../src/lib/host');
|
||||
var errors = require('../../src/lib/errors');
|
||||
var when = require('when');
|
||||
|
||||
var sinon = require('sinon');
|
||||
var nock = require('../mocks/server.js');
|
||||
var should = require('should');
|
||||
var _ = require('lodash');
|
||||
var nodeList = require('../fixtures/short_node_list.json');
|
||||
var stub = require('./auto_release_stub').make();
|
||||
|
||||
/**
|
||||
* Allows the tests call #request() without it doing anything past trying to select
|
||||
* a connection.
|
||||
* @param {Transport} tran - the transport to neuter
|
||||
*/
|
||||
function shortCircuitRequest(tran, delay) {
|
||||
stub(tran.connectionPool, 'select', function (cb) {
|
||||
setTimeout(cb, delay);
|
||||
});
|
||||
}
|
||||
|
||||
function getConnection(transport, status) {
|
||||
return transport.connectionPool.getConnections(status || 'alive', 1).pop();
|
||||
}
|
||||
|
||||
describe('Transport + Mock server', function () {
|
||||
describe('#request', function () {
|
||||
describe('server responds', function () {
|
||||
var serverMock;
|
||||
|
||||
before(function () {
|
||||
serverMock = nock('http://localhost')
|
||||
.get('/give-me-400')
|
||||
.reply(400, 'sorry bub')
|
||||
|
||||
.get('/give-me-404')
|
||||
.times(2)
|
||||
.reply(404, 'nothing here')
|
||||
|
||||
.get('/give-me-500')
|
||||
.reply(500, 'ah shit')
|
||||
|
||||
.get('/exists?')
|
||||
.reply(200, {
|
||||
status: 200
|
||||
})
|
||||
|
||||
.get('/give-me-someth')
|
||||
.reply(200, '{"not":"valid', {
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
'the answer': 42
|
||||
})
|
||||
|
||||
.get('/huh?')
|
||||
.reply(530, 'boo')
|
||||
|
||||
.get('/hottie-threads')
|
||||
.reply(200, [
|
||||
'he said',
|
||||
'she said',
|
||||
'he said',
|
||||
'she said',
|
||||
'he said',
|
||||
'she said'
|
||||
].join('\n'), {
|
||||
'Content-Type': 'text/plain'
|
||||
});
|
||||
});
|
||||
|
||||
after(function () {
|
||||
serverMock.done();
|
||||
});
|
||||
|
||||
describe('with a 400 status code', function () {
|
||||
it('passes back a 400/BadRequest error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-400'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[400]);
|
||||
err.should.be.an.instanceOf(errors.BadRequest);
|
||||
body.should.eql('sorry bub');
|
||||
status.should.eql(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 404 status code', function () {
|
||||
describe('and castExists is set', function () {
|
||||
it('sends back false', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-404',
|
||||
castExists: true
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql(false);
|
||||
status.should.eql(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('and the castExists param is not set', function () {
|
||||
it('sends back a 404/NotFound error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-404'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[404]);
|
||||
err.should.be.an.instanceOf(errors.NotFound);
|
||||
body.should.eql('nothing here');
|
||||
status.should.eql(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 500 status code', function () {
|
||||
it('passes back a 500/InternalServerError error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-500'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors[500]);
|
||||
err.should.be.an.instanceOf(errors.InternalServerError);
|
||||
body.should.eql('ah shit');
|
||||
status.should.eql(500);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 530 status code', function () {
|
||||
it('passes back a Generic error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/huh?'
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors.Generic);
|
||||
body.should.eql('boo');
|
||||
status.should.eql(530);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a 200 status code', function () {
|
||||
describe('and the castExists param is set', function () {
|
||||
it('sends back true', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/exists?',
|
||||
castExists: true
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql(true);
|
||||
status.should.eql(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('with a partial response body', function () {
|
||||
it('sends back a serialization error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/give-me-someth',
|
||||
}, function (err, body, status) {
|
||||
err.should.be.an.instanceOf(errors.Serialization);
|
||||
body.should.eql('{"not":"valid');
|
||||
status.should.eql(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('with a valid response body', function () {
|
||||
it('sends back the body and status code with no error', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/',
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.eql({
|
||||
'the answer': 42
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with plain text', function () {
|
||||
it('notices the content-type header and returns the text', function (done) {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
|
||||
trans.request({
|
||||
path: '/hottie-threads',
|
||||
}, function (err, body, status) {
|
||||
should.not.exist(err);
|
||||
body.should.match(/s?he said/g);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('return value', function () {
|
||||
it('resolves the promise it with the response body', function (done) {
|
||||
var serverMock = nock('http://esbox.1.com')
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
good: 'day'
|
||||
});
|
||||
|
||||
var tran = new Transport({
|
||||
hosts: 'http://esbox.1.com'
|
||||
});
|
||||
|
||||
tran.request({}).then(function (resp) {
|
||||
resp.should.eql({
|
||||
good: 'day'
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('timeout', function () {
|
||||
it('clears the timeout when the request is complete', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
|
||||
var server = nock('http://localhost:9200')
|
||||
.get('/')
|
||||
.reply(200, {
|
||||
i: 'am here'
|
||||
});
|
||||
|
||||
tran.request({}, function (err, resp, status) {
|
||||
should.not.exist(err);
|
||||
resp.should.eql({ i: 'am here' });
|
||||
status.should.eql(200);
|
||||
Object.keys(clock.timeouts).should.have.length(0);
|
||||
clock.restore();
|
||||
});
|
||||
});
|
||||
|
||||
it('timeout responds with a requestTimeout error', function (done) {
|
||||
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
|
||||
var server = nock('http://localhost:9200')
|
||||
.get('/')
|
||||
.delay(1000)
|
||||
.reply(200, {
|
||||
i: 'am here'
|
||||
});
|
||||
|
||||
tran.request({
|
||||
requestTimeout: 25
|
||||
}, function (err, resp, status) {
|
||||
err.should.be.an.instanceOf(errors.RequestTimeout);
|
||||
// Object.keys(clock.timeouts).should.have.length(0);
|
||||
// clock.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user