2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
var RestDAO = require('../../../src/js/service/rest');
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('Rest DAO unit tests', function() {
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
var restDao, xhrMock, requests;
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
restDao = new RestDAO();
|
|
|
|
xhrMock = sinon.useFakeXMLHttpRequest();
|
|
|
|
requests = [];
|
|
|
|
|
|
|
|
xhrMock.onCreate = function(xhr) {
|
|
|
|
requests.push(xhr);
|
|
|
|
};
|
|
|
|
});
|
2014-02-28 14:14:32 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {
|
|
|
|
xhrMock.restore();
|
|
|
|
});
|
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
describe('setBaseUri', function() {
|
|
|
|
it('should accept base uri', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
var baseUri = 'http://custom.com';
|
2014-11-21 09:06:29 -05:00
|
|
|
restDao = new RestDAO();
|
|
|
|
expect(restDao._baseUri).to.not.exist;
|
|
|
|
restDao.setBaseUri(baseUri);
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(restDao._baseUri).to.equal(baseUri);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get', function() {
|
|
|
|
it('should work with json as default type', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf'
|
|
|
|
}, function(err, data, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(data.foo).to.equal('bar');
|
|
|
|
var req = requests[0];
|
|
|
|
expect(req.requestHeaders.Accept).to.equal('application/json');
|
|
|
|
expect(status).to.equal(200);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}, '{"foo": "bar"}');
|
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work with jsonz', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf',
|
|
|
|
type: 'json'
|
|
|
|
}, function(err, data, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(data.foo).to.equal('bar');
|
|
|
|
var req = requests[0];
|
|
|
|
expect(req.requestHeaders.Accept).to.equal('application/json');
|
|
|
|
expect(status).to.equal(200);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}, '{"foo": "bar"}');
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work with plain text', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf',
|
|
|
|
type: 'text'
|
|
|
|
}, function(err, data, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(data).to.equal('foobar!');
|
|
|
|
var req = requests[0];
|
|
|
|
expect(req.requestHeaders.Accept).to.equal('text/plain');
|
|
|
|
expect(status).to.equal(200);
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'foobar!');
|
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work with xml', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf',
|
|
|
|
type: 'xml'
|
|
|
|
}, function(err, data, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(data).to.equal('<foo>bar</foo>');
|
|
|
|
var req = requests[0];
|
|
|
|
expect(req.requestHeaders.Accept).to.equal('application/xml');
|
|
|
|
expect(status).to.equal(200);
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "application/xml"
|
|
|
|
}, '<foo>bar</foo>');
|
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail for missing uri parameter', function() {
|
|
|
|
restDao.get({}, function(err, data) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(400);
|
|
|
|
expect(data).to.not.exist;
|
2013-11-08 03:29:04 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail for unhandled data type', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf',
|
|
|
|
type: 'snafu'
|
|
|
|
}, function(err, data) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(400);
|
|
|
|
expect(data).to.not.exist;
|
2013-11-08 03:29:04 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail for server error', function() {
|
|
|
|
restDao.get({
|
|
|
|
uri: '/asdf'
|
|
|
|
}, function(err, data) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(500);
|
|
|
|
expect(data).to.not.exist;
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(500, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'Internal error');
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('post', function() {
|
|
|
|
it('should fail', function() {
|
|
|
|
restDao.post('/asdf', {}, function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(500);
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(500, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'Internal error');
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function() {
|
|
|
|
restDao.post('/asdf', {}, function(err, res, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(res).to.equal('');
|
|
|
|
expect(status).to.equal(201);
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(201);
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('put', function() {
|
|
|
|
it('should fail', function() {
|
|
|
|
restDao.put('/asdf', {}, function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(500);
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
2013-11-20 06:17:21 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(500, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'Internal error');
|
|
|
|
});
|
2014-02-28 14:14:32 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function() {
|
|
|
|
restDao.put('/asdf', {}, function(err, res, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(res).to.equal('');
|
|
|
|
expect(status).to.equal(201);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(201);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('remove', function() {
|
|
|
|
it('should fail', function() {
|
|
|
|
restDao.remove('/asdf', function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal(500);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(500, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'Internal error');
|
|
|
|
});
|
2014-02-28 14:14:32 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function() {
|
|
|
|
restDao.remove('/asdf', function(err, res, status) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(res).to.equal('');
|
|
|
|
expect(status).to.equal(200);
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200);
|
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|