diff --git a/lib/responses/JSONF.js b/lib/responses/JSONF.js index f71c6ea..39d2f7f 100644 --- a/lib/responses/JSONF.js +++ b/lib/responses/JSONF.js @@ -12,9 +12,8 @@ var jsonF = { * @return {Object} JS object to send with res.send */ jsonFormater: function jsonFormater(req, res) { - if (!res.locals.data) res.locals.data = {}; - if (!res.locals.model) { + if (!res.locals.data) res.locals.data = {}; // set messages res.locals.data.messages = res.locals.messages; return res.send(res.locals.data); @@ -29,7 +28,9 @@ var jsonF = { } // check field privacity access - req.we.db.checkRecordsPrivacity(res.locals.data); + if (res.locals.data) { + req.we.db.checkRecordsPrivacity(res.locals.data); + } response.meta = res.locals.metadata; diff --git a/lib/responses/methods/index.js b/lib/responses/methods/index.js index 7644f24..e9aa3a6 100644 --- a/lib/responses/methods/index.js +++ b/lib/responses/methods/index.js @@ -132,6 +132,11 @@ module.exports = { we.freeResponseMemory(req, res); }); }, + /** + * Deleted response + * + * redirect for html responses + */ deleted: function deletedResponse() { var res = this.res; var req = this.req; @@ -153,7 +158,7 @@ module.exports = { } } - res.send(); + res.format(req.we.responses.formaters); we.freeResponseMemory(req, res); }); @@ -182,13 +187,14 @@ module.exports = { return res.end(); } - req.we.responses.formaters.html(req, res); + res.format(req.we.responses.formaters); req.we.freeResponseMemory(req, res); }, forbidden: function forbiddenResponse(data) { var res = this.res; var req = this.req; + var __ = res.locals.__ || req.we.i18n.__; if (typeof data == 'string') { res.addMessage('error', { @@ -198,32 +204,16 @@ module.exports = { data = null; } - if (!data) data = {}; - res.status(403); - res.locals.title = res.locals.__('response.forbidden.title'); + res.locals.title = __('response.forbidden.title'); if (req.accepts('html')) { res.locals.layoutName = 'fullwidth'; res.locals.template = '403'; - return res.view(data); } - if (!res.locals.model) { - return res.send({ - messages: res.locals.messages - }); - } - - var response = {}; - response[res.locals.model] = data; - response.meta = res.locals.metadata; - - // set messages - response.messages = res.locals.messages; - - res.send(response); + res.format(req.we.responses.formaters); req.we.freeResponseMemory(this, res); }, @@ -239,7 +229,7 @@ module.exports = { data = null; } - if (!data) data = {}; + res.locals.data = null; if (req.we.env == 'dev') { console.trace('404', req.path); @@ -254,12 +244,9 @@ module.exports = { if (req.accepts('html')) { res.locals.layoutName = 'fullwidth'; res.locals.template = '404'; - return res.view(data); } - data.messages = res.locals.messages; - - res.send(data); + res.format(req.we.responses.formaters); req.we.freeResponseMemory(req, res); }, @@ -277,19 +264,20 @@ module.exports = { res.locals.title = __('response.serveError.title'); - if (!data) data = {}; + if (!data) { + data = {}; + } else if (typeof data == 'string') { + res.addMessage('error', data); + } if (req.accepts('html')) { res.locals.template = '500'; res.locals.layoutName = 'fullwidth'; - return res.view(data); } - // set messages - data.messages = res.locals.messages; - - res.send(data); - + // send the response + res.format(req.we.responses.formaters); + // helper for unset variables this.req.we.freeResponseMemory(this, res); }, /** @@ -312,23 +300,12 @@ module.exports = { } if (req.accepts('html')) { + // if is html if (!res.locals.template) res.locals.template = '400'; - return res.view(data); - } - - if (!res.locals.model) { - // set messages - data.messages = res.locals.messages; - return res.send(data); } - var response = {}; - response[res.locals.model] = data; - response.meta = res.locals.metadata; - // set messages - response.messages = res.locals.messages; // send the response - res.send(response); + res.format(req.we.responses.formaters); req.we.freeResponseMemory(req, res); }, @@ -340,7 +317,6 @@ module.exports = { queryError: function queryError(err) { var res = this.res; var req = this.req; - var we = req.we; var isQueryError = true; if (err) { diff --git a/src/responses/JSONF.js b/src/responses/JSONF.js index e170da4..c851483 100644 --- a/src/responses/JSONF.js +++ b/src/responses/JSONF.js @@ -10,33 +10,34 @@ let jsonF = { * @return {Object} JS object to send with res.send */ jsonFormater: function jsonFormater (req, res) { - if (!res.locals.data) res.locals.data = {}; - if (!res.locals.model) { + if (!res.locals.data) res.locals.data = {} // set messages - res.locals.data.messages = res.locals.messages; - return res.send(res.locals.data); + res.locals.data.messages = res.locals.messages + return res.send(res.locals.data) } - var response = {}; + let response = {} if (req.we.config.sendNestedModels) { - response[res.locals.model] = res.locals.data; + response[res.locals.model] = res.locals.data } else { - response[res.locals.model] = parseRecord(req, res, res.locals.data); + response[res.locals.model] = parseRecord(req, res, res.locals.data) } // check field privacity access - req.we.db.checkRecordsPrivacity(res.locals.data); + if (res.locals.data) { + req.we.db.checkRecordsPrivacity(res.locals.data) + } - response.meta = res.locals.metadata; + response.meta = res.locals.metadata if (!isEmpty( res.locals.messages) ) { // set messages - response.messages = res.locals.messages; + response.messages = res.locals.messages } - res.send(response); + res.send(response) } } diff --git a/src/responses/methods/index.js b/src/responses/methods/index.js index b2aa4f9..e11e521 100644 --- a/src/responses/methods/index.js +++ b/src/responses/methods/index.js @@ -6,17 +6,17 @@ module.exports = { * * @param {Object} data opctional data */ - ok: function okResponse(data) { - var res = this.res; - var req = this.req; - var we = req.we; + ok: function okResponse (data) { + let res = this.res + let req = this.req + let we = req.we - res.status(200); + res.status(200) if (!data) { - data = res.locals.data || {}; + data = res.locals.data || {} } else { - if (!res.locals.data) res.locals.data = data; + if (!res.locals.data) res.locals.data = data } // use this hook in one we.js plugin to change a res.ok response @@ -25,18 +25,18 @@ module.exports = { res: res, data: data }, function (err) { - if (err) we.log.error(err); + if (err) we.log.error(err) if (req.accepts('html')) { if (req.method == 'POST' && res.locals.action == 'edit' && res.locals.redirectTo) { - return res.redirect(res.locals.redirectTo); + return res.redirect(res.locals.redirectTo) } } - res.format(we.responses.formaters); + res.format(we.responses.formaters) - we.freeResponseMemory(req, res); - }); + we.freeResponseMemory(req, res) + }) }, /** * Record created response @@ -45,17 +45,17 @@ module.exports = { * * @param {Object} data record data */ - created: function createdResponse(data) { - var res = this.res; - var req = this.req; - var we = req.we; + created: function createdResponse (data) { + let res = this.res + let req = this.req + let we = req.we - res.status(201); + res.status(201) if (!data) { - data = res.locals.data || {}; + data = res.locals.data || {} } else { - if (!res.locals.data) res.locals.data = data; + if (!res.locals.data) res.locals.data = data } // use this hook in one we.js plugin to change a res.ok response @@ -74,16 +74,16 @@ module.exports = { return res.redirect(res.locals.redirectTo); } else { // push id to paramsArray for use in urlTo - req.paramsArray.push(res.locals.data.id); + req.paramsArray.push(res.locals.data.id) // redirect to content after create - return res.redirect(we.router.urlTo(res.locals.model + '.findOne', req.paramsArray)); + return res.redirect(we.router.urlTo(res.locals.model + '.findOne', req.paramsArray)) } } - res.format(we.responses.formaters); + res.format(we.responses.formaters) - we.freeResponseMemory(req, res); - }); + we.freeResponseMemory(req, res) + }) }, /** * Record updated response @@ -92,17 +92,17 @@ module.exports = { * * @param {Object} data optional data */ - updated: function updatedResponse(data) { - var res = this.res; - var req = this.req; - var we = req.we; + updated: function updatedResponse (data) { + let res = this.res + let req = this.req + let we = req.we - res.status(200); + res.status(200) if (!data) { - data = res.locals.data || {}; + data = res.locals.data || {} } else { - if (!res.locals.data) res.locals.data = data; + if (!res.locals.data) res.locals.data = data } // use this hook in one we.js plugin to change a res.ok response @@ -125,17 +125,22 @@ module.exports = { } } - res.format(we.responses.formaters); + res.format(we.responses.formaters) - we.freeResponseMemory(req, res); + we.freeResponseMemory(req, res) }); }, - deleted: function deletedResponse() { - var res = this.res; - var req = this.req; - var we = req.we; + /** + * Deleted response + * + * redirect for html responses + */ + deleted: function deletedResponse () { + let res = this.res + let req = this.req + let we = req.we - res.status(204); + res.status(204) // use this hook in one we.js plugin to change a res.ok response we.hooks.trigger('we:before:send:deletedResponse', { @@ -147,26 +152,26 @@ module.exports = { res.locals.redirectTo && (we.router.urlTo(res.locals.model + '.findOne', req.paramsArray) != res.locals.redirectTo) ) { - return res.redirect(res.locals.redirectTo); + return res.redirect(res.locals.redirectTo) } else { - res.locals.deleteRedirectUrl = we.router.urlTo(res.locals.model + '.find', req.paramsArray); - return res.redirect((res.locals.deleteRedirectUrl || '/')); + res.locals.deleteRedirectUrl = we.router.urlTo(res.locals.model + '.find', req.paramsArray) + return res.redirect((res.locals.deleteRedirectUrl || '/')) } } - res.send(); + res.format(req.we.responses.formaters) - we.freeResponseMemory(req, res); + we.freeResponseMemory(req, res) }); }, - view: function viewResponse(data) { - var req = this.req; - var res = this.res; + view: function viewResponse (data) { + let req = this.req + let res = this.res if (!data) { - data = res.locals.data || {}; + data = res.locals.data || {} } else { - if (!res.locals.data) res.locals.data = data; + if (!res.locals.data) res.locals.data = data } if (req.haveAlias) { @@ -178,69 +183,54 @@ module.exports = { 'Expires': new Date(Date.now() + 345600000).toUTCString() }); - req.we.freeResponseMemory(req, res); + req.we.freeResponseMemory(req, res) - return res.end(); + return res.end() } - req.we.responses.formaters.html(req, res); + res.format(req.we.responses.formaters) - req.we.freeResponseMemory(req, res); + req.we.freeResponseMemory(req, res) }, forbidden: function forbiddenResponse(data) { - var res = this.res; - var req = this.req; + let res = this.res + let req = this.req + let __ = ( res.locals.__ || req.we.i18n.__ ) if (typeof data == 'string') { res.addMessage('error', { text: data }); - data = null; + data = null } - if (!data) data = {}; - - res.status(403); + res.status(403) - res.locals.title = res.locals.__('response.forbidden.title'); + res.locals.title = __('response.forbidden.title') if (req.accepts('html')) { - res.locals.layoutName = 'fullwidth'; - res.locals.template = '403'; - return res.view(data); - } - - if (!res.locals.model) { - return res.send({ - messages: res.locals.messages - }); + res.locals.layoutName = 'fullwidth' + res.locals.template = '403' } - var response = {}; - response[res.locals.model] = data; - response.meta = res.locals.metadata; - - // set messages - response.messages = res.locals.messages; + res.format(req.we.responses.formaters) - res.send(response); - - req.we.freeResponseMemory(this, res); + req.we.freeResponseMemory(this, res) }, - notFound: function notFoundResponse(data) { - var res = this.res; - var req = this.req; + notFound: function notFoundResponse (data) { + let res = this.res + let req = this.req if (typeof data == 'string') { res.addMessage('error', { text: data }); - data = null; + data = null } - if (!data) data = {}; + res.locals.data = null; if (req.we.env == 'dev') { console.trace('404', req.path); @@ -255,104 +245,90 @@ module.exports = { if (req.accepts('html')) { res.locals.layoutName = 'fullwidth'; res.locals.template = '404'; - return res.view(data); } - data.messages = res.locals.messages; - - res.send(data); + res.format(req.we.responses.formaters) - req.we.freeResponseMemory(req, res); + req.we.freeResponseMemory(req, res) }, /** * Server error response * * @param {Object} data the error */ - serverError: function serverErrorResponse(data) { - var res = this.res; - var req = this.req; - var __ = ( res.locals.__ || req.we.i18n.__ ); + serverError: function serverErrorResponse (data) { + let res = this.res + let req = this.req + let __ = ( res.locals.__ || req.we.i18n.__ ) - res.status(500); + res.status(500) - res.locals.title = __('response.serveError.title'); + res.locals.title = __('response.serveError.title') - if (!data) data = {}; + if (!data) { + data = {} + } else if (typeof data == 'string') { + res.addMessage('error', data) + } if (req.accepts('html')) { - res.locals.template = '500'; - res.locals.layoutName = 'fullwidth'; - return res.view(data); + res.locals.template = '500' + res.locals.layoutName = 'fullwidth' } - // set messages - data.messages = res.locals.messages; - - res.send(data); - - this.req.we.freeResponseMemory(this, res); + // send the response + res.format(req.we.responses.formaters) + // helper for unset variables + this.req.we.freeResponseMemory(this, res) }, /** * bad request response * * @param {Obejct|String} data message */ - badRequest: function badResponse(data) { - var res = this.res; - var req = this.req; + badRequest: function badResponse (data) { + let res = this.res + let req = this.req - res.status(400); + res.status(400) - if (req.we.env == 'dev') console.trace('400', req.path); + if (req.we.env == 'dev') console.trace('400', req.path) if (!data) { - data = {}; + data = {} } else if (typeof data == 'string') { - res.addMessage('warning', data); + res.addMessage('warning', data) } if (req.accepts('html')) { - if (!res.locals.template) res.locals.template = '400'; - return res.view(data); + // if is html + if (!res.locals.template) res.locals.template = '400' } - if (!res.locals.model) { - // set messages - data.messages = res.locals.messages; - return res.send(data); - } - - var response = {}; - response[res.locals.model] = data; - response.meta = res.locals.metadata; - // set messages - response.messages = res.locals.messages; // send the response - res.send(response); + res.format(req.we.responses.formaters) - req.we.freeResponseMemory(req, res); + req.we.freeResponseMemory(req, res) }, /** * Sequelize query error parser * * @param {Object} err The database error */ - queryError: function queryError(err) { - var res = this.res; - var req = this.req; - var we = req.we; - var isQueryError = true; + queryError: function queryError (err) { + let res = this.res + let req = this.req + let isQueryError = true if (err) { // parse all sequelize validation erros if (err.name === 'SequelizeValidationError') { // query validation error ... - res.locals.validationError = {}; + res.locals.validationError = {} if (req.accepts('html')) { err.errors.forEach(function (err) { if (!res.locals.validationError[err.path]) - res.locals.validationError[err.path] = []; + res.locals.validationError[err.path] = [] res.locals.validationError[err.path].push({ field: err.path, rule: err.type, @@ -363,15 +339,15 @@ module.exports = { } else if (err.name === 'SequelizeDatabaseError') { // parse sequelize database errors if (err.message) { - res.addMessage('error', err.message); + res.addMessage('error', err.message) } } else if (typeof err == 'string') { - res.addMessage('error', err); + res.addMessage('error', err) } else { // unknow error type - isQueryError = false; + isQueryError = false - console.error('responses.queryError:unknowError: ', req.path, err, err.name); + console.error('responses.queryError:unknowError: ', req.path, err, err.name) } // and for others errors @@ -379,20 +355,20 @@ module.exports = { err.errors.forEach(function (err) { res.addMessage('error', err.message, { field: err.path, rule: err.type - }); - }); + }) + }) } } if (isQueryError) { - res.status(400); + res.status(400) } else { - res.status(500); + res.status(500) } - res.format(req.we.responses.formaters); + res.format(req.we.responses.formaters) - req.we.freeResponseMemory(req, res); + req.we.freeResponseMemory(req, res) }, /** @@ -400,14 +376,14 @@ module.exports = { * @param {String} s response status * @param {String} p path */ - goTo: function goTo(s ,p) { + goTo: function goTo (s ,p) { // save locals messages to flash - this.res.moveLocalsMessagesToFlash(); + this.res.moveLocalsMessagesToFlash() // use default redirect if (p) { - this.res.redirect(s, p); + this.res.redirect(s, p) } else { - this.res.redirect(s); + this.res.redirect(s) } } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/test/tests/modules/response.methods.test.js b/test/tests/modules/response.methods.test.js index 6c22947..03588df 100644 --- a/test/tests/modules/response.methods.test.js +++ b/test/tests/modules/response.methods.test.js @@ -341,7 +341,7 @@ describe('we.responses.methods', function () { assert(!res.send.called); done(); }); - it('we.responses.methods.deleted should run res.send for json responses', function (done) { + it('we.responses.methods.deleted should run res.format for json responses', function (done) { var req = { method: 'POST', we: we, accepts: function(){ return false } }; var res = { locals: { @@ -351,11 +351,11 @@ describe('we.responses.methods', function () { action: 'deleted' }, status: function() {}, - send: function() {}, + format: function() {}, view: function() {}, redirect: function() {} }; - sinon.spy(res, 'send'); + sinon.spy(res, 'format'); sinon.spy(res, 'status'); sinon.spy(res, 'redirect'); sinon.spy(res, 'view'); @@ -367,8 +367,9 @@ describe('we.responses.methods', function () { assert(!res.redirect.called); assert(res.status.called); assert.equal(res.status.firstCall.args[0], 204); - assert(res.send.called); - assert(!res.send.firstCall.args[0]); + assert(res.format.called); + assert(res.format.firstCall.args[0]); + assert(!res.format.firstCall.args[1]); done(); }); }); @@ -376,7 +377,7 @@ describe('we.responses.methods', function () { describe('forbidden', function () { before(function (done) { done(); }); - it('we.responses.methods.forbidden should run res.view if responseType=html', function (done) { + it('we.responses.methods.forbidden should run res.format if responseType=html', function (done) { var req = { method: 'POST', we: we, accepts: function(){ return true } }; var res = { locals: { @@ -386,11 +387,11 @@ describe('we.responses.methods', function () { __: function() {} }, status: function() {}, - send: function() {}, + format: function() {}, view: function() {}, redirect: function() {} }; - sinon.spy(res, 'send'); + sinon.spy(res, 'format'); sinon.spy(res, 'status'); sinon.spy(res, 'redirect'); sinon.spy(res, 'view'); @@ -398,81 +399,11 @@ describe('we.responses.methods', function () { we.responses.methods.forbidden.bind({ req: req, res: res, we: we })({ message: 'hi' }); - assert(res.view.called); - assert.equal(res.view.firstCall.args[0].message, 'hi'); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 403); - assert(!res.send.called); - done(); - }); - - it('we.responses.methods.forbidden should run res.send only with messages if model not is set', function (done) { - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: null, - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.forbidden.bind({ - req: req, res: res, we: we - })(); - assert(!res.view.called); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 403); - - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - - done(); - }); - - it('we.responses.methods.forbidden should run res.send with data', function (done) { - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.forbidden.bind({ - req: req, res: res, we: we - })(post); - assert(!res.view.called); + assert(res.format.called); + assert(res.format.firstCall.args[0]); assert(!res.redirect.called); assert(res.status.called); assert.equal(res.status.firstCall.args[0], 403); - - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - assert.equal(res.send.firstCall.args[0].post.id, post.id); - done(); }); }); @@ -492,7 +423,7 @@ describe('we.responses.methods', function () { done(); }); - it('we.responses.methods.notFound should run res.view if responseType=html', function (done) { + it('we.responses.methods.notFound should run res.format if responseType=html', function (done) { var req = { method: 'POST', we: we, accepts: function(){ return true } }; var res = { @@ -503,90 +434,22 @@ describe('we.responses.methods', function () { __: function() {} }, status: function() {}, - send: function() {}, + format: function() {}, view: function() {}, redirect: function() {} }; - sinon.spy(res, 'send'); + sinon.spy(res, 'format'); sinon.spy(res, 'status'); sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); we.responses.methods.notFound.bind({ req: req, res: res, we: we })({ messages: 'hi' }); - assert(res.view.called); - assert.equal(res.view.firstCall.args[0].messages, 'hi'); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 404); - assert(!res.send.called); - - done(); - }); - - it('we.responses.methods.notFound should run res.view if responseType=html and set data={}', function (done) { - var req = { method: 'POST', we: we, accepts: function(){ return true } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.notFound.bind({ - req: req, res: res, we: we - })(); - assert(res.view.called); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 404); - assert(!res.send.called); - done(); - }); - - it('we.responses.methods.notFound should run res.send if responseType!=html', function (done) { - var req = { method: 'GET', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.notFound.bind({ - req: req, res: res, we: we - })(post); - assert(!res.view.called); assert(!res.redirect.called); assert(res.status.called); assert.equal(res.status.firstCall.args[0], 404); + assert(res.format.called); - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - - assert.equal(res.send.firstCall.args[0].id, post.id); done(); }); }); @@ -606,7 +469,7 @@ describe('we.responses.methods', function () { done(); }); - it('we.responses.methods.serverError should run res.view if responseType=html', function (done) { + it('we.responses.methods.serverError should run res.format if responseType=html', function (done) { var req = { method: 'POST', we: we, accepts: function(){ return true } }; var res = { locals: { @@ -616,11 +479,11 @@ describe('we.responses.methods', function () { __: function() {} }, status: function() {}, - send: function() {}, + format: function() {}, view: function() {}, redirect: function() {} }; - sinon.spy(res, 'send'); + sinon.spy(res, 'format'); sinon.spy(res, 'status'); sinon.spy(res, 'redirect'); sinon.spy(res, 'view'); @@ -628,47 +491,11 @@ describe('we.responses.methods', function () { we.responses.methods.serverError.bind({ req: req, res: res, we: we })({ messages: 'hi' }); - assert(res.view.called); - assert.equal(res.view.firstCall.args[0].messages, 'hi'); + assert(res.format.called); assert(!res.redirect.called); assert(res.status.called); assert.equal(res.status.firstCall.args[0], 500); - assert(!res.send.called); - done(); - }); - - it('we.responses.methods.serverError should run res.send if responseType!=html', function (done) { - we.env = 'dev'; - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.serverError.bind({ - req: req, res: res, we: we - })(); assert(!res.view.called); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 500); - - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - we.env = 'test'; done(); }); }); @@ -676,7 +503,7 @@ describe('we.responses.methods', function () { describe('badRequest', function () { before(function (done) { done(); }); - it('we.responses.methods.badRequest should run res.view if responseType=html', function (done) { + it('we.responses.methods.badRequest should run res.format if responseType=html', function (done) { var req = { method: 'POST', we: we, accepts: function(){ return true } }; var res = { locals: { @@ -686,11 +513,11 @@ describe('we.responses.methods', function () { __: function() {} }, status: function() {}, - send: function() {}, + format: function() {}, view: function() {}, redirect: function() {} }; - sinon.spy(res, 'send'); + sinon.spy(res, 'format'); sinon.spy(res, 'status'); sinon.spy(res, 'redirect'); sinon.spy(res, 'view'); @@ -698,117 +525,115 @@ describe('we.responses.methods', function () { we.responses.methods.badRequest.bind({ req: req, res: res, we: we })({ messages: 'hi' }); - assert(res.view.called); - assert.equal(res.view.firstCall.args[0].messages, 'hi'); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 400); - assert(!res.send.called); - done(); - }); - - it('we.responses.methods.badRequest should run res.send if responseType=json and send only messages', function (done) { - - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.badRequest.bind({ - req: req, res: res, we: we - })(); - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - assert(!res.redirect.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 400); - assert(!res.view.called); - - done(); - }); - - it('we.responses.methods.badRequest should run res.send if responseType=json and send data + messages', - function (done) { - - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - messages: 'hi', - __: function() {} - }, - status: function() {}, - send: function() {}, - view: function() {}, - redirect: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'redirect'); - sinon.spy(res, 'view'); - - we.responses.methods.badRequest.bind({ - req: req, res: res, we: we - })(post); - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - assert.equal(res.send.firstCall.args[0].post.id, post.id); + assert(res.format.called); assert(!res.redirect.called); assert(res.status.called); assert.equal(res.status.firstCall.args[0], 400); - assert(!res.view.called); - done(); }); - it('we.responses.methods.badRequest should run res.send if responseType=json and send addMessage', - function (done) { - - var req = { method: 'POST', we: we, accepts: function(){ return false } }; - var res = { - locals: { - Model: we.db.models.post, - model: 'post', - responseType: 'json', - messages: 'hi' - }, - status: function() {}, - send: function() {}, - view: function() {}, - addMessage: function() {} - }; - sinon.spy(res, 'send'); - sinon.spy(res, 'status'); - sinon.spy(res, 'addMessage'); - sinon.spy(res, 'view'); - - we.responses.methods.badRequest.bind({ - req: req, res: res, we: we - })('a error messsage'); - assert(res.send.called); - assert.equal(res.send.firstCall.args[0].messages, 'hi'); - assert(res.addMessage.called); - assert(res.status.called); - assert.equal(res.status.firstCall.args[0], 400); - assert(!res.view.called); - - done(); - }); + // it('we.responses.methods.badRequest should run res.send if responseType=json and send only messages', function (done) { + + // var req = { method: 'POST', we: we, accepts: function(){ return false } }; + // var res = { + // locals: { + // Model: we.db.models.post, + // responseType: 'json', + // messages: 'hi', + // __: function() {} + // }, + // status: function() {}, + // send: function() {}, + // view: function() {}, + // redirect: function() {} + // }; + // sinon.spy(res, 'send'); + // sinon.spy(res, 'status'); + // sinon.spy(res, 'redirect'); + // sinon.spy(res, 'view'); + + // we.responses.methods.badRequest.bind({ + // req: req, res: res, we: we + // })(); + // assert(res.send.called); + // assert.equal(res.send.firstCall.args[0].messages, 'hi'); + // assert(!res.redirect.called); + // assert(res.status.called); + // assert.equal(res.status.firstCall.args[0], 400); + // assert(!res.view.called); + + // done(); + // }); + + // it('we.responses.methods.badRequest should run res.send if responseType=json and send data + messages', + // function (done) { + + // var req = { method: 'POST', we: we, accepts: function(){ return false } }; + // var res = { + // locals: { + // Model: we.db.models.post, + // model: 'post', + // responseType: 'json', + // messages: 'hi', + // __: function() {} + // }, + // status: function() {}, + // send: function() {}, + // view: function() {}, + // redirect: function() {} + // }; + // sinon.spy(res, 'send'); + // sinon.spy(res, 'status'); + // sinon.spy(res, 'redirect'); + // sinon.spy(res, 'view'); + + // we.responses.methods.badRequest.bind({ + // req: req, res: res, we: we + // })(post); + // assert(res.send.called); + // assert.equal(res.send.firstCall.args[0].messages, 'hi'); + // assert.equal(res.send.firstCall.args[0].post.id, post.id); + // assert(!res.redirect.called); + // assert(res.status.called); + // assert.equal(res.status.firstCall.args[0], 400); + // assert(!res.view.called); + + // done(); + // }); + + // it('we.responses.methods.badRequest should run res.send if responseType=json and send addMessage', + // function (done) { + + // var req = { method: 'POST', we: we, accepts: function(){ return false } }; + // var res = { + // locals: { + // Model: we.db.models.post, + // model: 'post', + // responseType: 'json', + // messages: 'hi' + // }, + // status: function() {}, + // send: function() {}, + // view: function() {}, + // addMessage: function() {} + // }; + // sinon.spy(res, 'send'); + // sinon.spy(res, 'status'); + // sinon.spy(res, 'addMessage'); + // sinon.spy(res, 'view'); + + // we.responses.methods.badRequest.bind({ + // req: req, res: res, we: we + // })('a error messsage'); + // assert(res.send.called); + // assert.equal(res.send.firstCall.args[0].messages, 'hi'); + // assert(res.addMessage.called); + // assert(res.status.called); + // assert.equal(res.status.firstCall.args[0], 400); + // assert(!res.view.called); + + // done(); + // }); }); describe('queryError', function () { before(function (done) { done(); }); diff --git a/test/tests/requests/resource.test.js b/test/tests/requests/resource.test.js index 98b4f45..47d0df7 100644 --- a/test/tests/requests/resource.test.js +++ b/test/tests/requests/resource.test.js @@ -163,6 +163,7 @@ describe('resourceRequests', function() { .then(function (p) { request(http) .get('/post/'+p.id) + .set('Accept', 'application/json') .expect(200) .end(function (err, res) { if (err) throw err; @@ -182,6 +183,7 @@ describe('resourceRequests', function() { request(http) .get('/post/12321313123121311231231233') .expect(404) + .set('Accept', 'application/json') .end(function (err, res) { if (err) throw err; @@ -202,6 +204,7 @@ describe('resourceRequests', function() { }; request(http) .put('/post/'+p.id) + .set('Accept', 'application/json') .send(updateData) .expect(200) .end(function (err, res) { @@ -223,6 +226,7 @@ describe('resourceRequests', function() { .then(function (p) { request(http) .delete('/post/'+p.id) + .set('Accept', 'application/json') .expect(204) .end(function (err, res) { if (err) throw err;