1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 20:32:15 -05:00
mail/test/unit/service/rest-dao-test.js

205 lines
6.0 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var RestDAO = require('../../../src/js/service/rest');
2014-10-07 14:32:23 -04:00
describe('Rest DAO unit tests', function() {
2014-10-07 14:32:23 -04:00
var restDao, xhrMock, requests;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
2014-12-10 11:20:55 -05:00
restDao = new RestDAO(window.qMock);
2014-10-07 14:32:23 -04:00
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();
});
describe('setBaseUri', function() {
it('should accept base uri', function() {
2014-10-07 14:32:23 -04:00
var baseUri = 'http://custom.com';
2014-12-11 12:12:37 -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);
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('get', function() {
2014-12-10 11:20:55 -05:00
it('should work with json as default type', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf'
2014-12-10 11:20:55 -05:00
}).then(function(data) {
2014-10-07 14:32:23 -04:00
expect(data.foo).to.equal('bar');
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json');
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{"foo": "bar"}');
});
2014-12-10 11:20:55 -05:00
it('should work with jsonz', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf',
type: 'json'
2014-12-10 11:20:55 -05:00
}).then(function(data) {
2014-10-07 14:32:23 -04:00
expect(data.foo).to.equal('bar');
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json');
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{"foo": "bar"}');
});
2014-12-10 11:20:55 -05:00
it('should work with plain text', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf',
type: 'text'
2014-12-10 11:20:55 -05:00
}).then(function(data) {
2014-10-07 14:32:23 -04:00
expect(data).to.equal('foobar!');
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('text/plain');
2014-12-10 11:20:55 -05:00
done();
2014-02-28 14:14:32 -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!');
});
2014-12-10 11:20:55 -05:00
it('should work with xml', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf',
type: 'xml'
2014-12-10 11:20:55 -05:00
}).then(function(data) {
2014-10-07 14:32:23 -04:00
expect(data).to.equal('<foo>bar</foo>');
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/xml');
2014-12-10 11:20:55 -05:00
done();
2014-02-28 14:14:32 -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>');
});
2014-12-10 11:20:55 -05:00
it('should fail for missing uri parameter', function(done) {
restDao.get({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(400);
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-12-10 11:20:55 -05:00
it('should fail for unhandled data type', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf',
type: 'snafu'
2014-12-10 11:20:55 -05:00
}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(400);
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-12-10 11:20:55 -05:00
it('should fail for server error', function(done) {
2014-10-07 14:32:23 -04:00
restDao.get({
uri: '/asdf'
2014-12-10 11:20:55 -05:00
}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(500);
2014-12-10 11:20:55 -05:00
done();
});
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-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('post', function() {
2014-12-10 11:20:55 -05:00
it('should fail', function(done) {
restDao.post('/asdf', {}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(500);
2014-12-10 11:20:55 -05:00
done();
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-12-10 11:20:55 -05:00
it('should work', function(done) {
restDao.post('/asdf', {}).then(function(res) {
2014-10-07 14:32:23 -04:00
expect(res).to.equal('');
2014-12-10 11:20:55 -05:00
done();
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() {
2014-12-10 11:20:55 -05:00
it('should fail', function(done) {
restDao.put('/asdf', {}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(500);
2014-12-10 11:20:55 -05:00
done();
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-12-10 11:20:55 -05:00
it('should work', function(done) {
restDao.put('/asdf', {}).then(function(res) {
2014-10-07 14:32:23 -04:00
expect(res).to.equal('');
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(201);
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('remove', function() {
2014-12-10 11:20:55 -05:00
it('should fail', function(done) {
restDao.remove('/asdf').catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.code).to.equal(500);
2014-12-10 11:20:55 -05:00
done();
});
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-12-10 11:20:55 -05:00
it('should work', function(done) {
restDao.remove('/asdf').then(function(res) {
2014-10-07 14:32:23 -04:00
expect(res).to.equal('');
2014-12-10 11:20:55 -05:00
done();
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(200);
});
});
});