test(rest) formatMsg

This commit is contained in:
coderaiser 2017-10-23 18:02:07 +03:00
parent ecbf8f2fe5
commit 38291f1fc3
2 changed files with 37 additions and 8 deletions

View file

@ -363,15 +363,20 @@ function getWin32RootMsg() {
return error;
}
function formatMsg(msgParam, dataParam, status) {
let data;
const isObj = typeof dataParam === 'object';
function parseData(data) {
const isObj = typeof data === 'object';
if (isObj)
data = json.stringify(dataParam);
else
data = dataParam;
if (!isObj)
return data;
return CloudFunc.formatMsg(msgParam, data, status);
return json.stringify(data);
}
module.exports._formatMsg = formatMsg;
function formatMsg(msg, data, status) {
const value = parseData(data);
return CloudFunc.formatMsg(msg, value, status);
}

24
test/server/rest.js Normal file
View file

@ -0,0 +1,24 @@
'use strict';
const test = require('tape');
const rest = require('../../server/rest');
const {
_formatMsg,
} = rest;
test('rest: formatMsg', (t) => {
const result = _formatMsg('hello', 'world');
t.equal(result, 'hello: ok("world")');
t.end();
});
test('rest: formatMsg: json', (t) => {
const result = _formatMsg('hello', {
name: 'world',
});
t.equal(result, 'hello: ok("{"name":"world"}")');
t.end();
});