1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-24 08:18:48 -05:00
mail/test/unit/rest-dao-test.js

221 lines
6.8 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-10-07 14:32:23 -04:00
var RestDAO = require('../../src/js/dao/rest-dao');
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() {
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();
});
describe('contructor', function() {
it('should set default base uri', function() {
restDao = new RestDAO();
expect(restDao).to.exist;
expect(restDao._baseUri).to.exist;
});
2014-10-07 14:32:23 -04:00
it('should accept default base uri', function() {
var baseUri = 'http://custom.com';
restDao = new RestDAO(baseUri);
expect(restDao).to.exist;
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() {
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);
});
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-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);
});
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-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
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "text/plain"
}, 'foobar!');
});
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
});
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-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;
});
2014-10-07 14:32:23 -04: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;
});
2014-10-07 14:32:23 -04: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;
});
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() {
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);
});
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() {
it('should fail', function() {
restDao.remove('/asdf', function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500);
});
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);
});
2014-10-07 14:32:23 -04:00
expect(requests.length).to.equal(1);
requests[0].respond(200);
});
});
});