mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-21 02:29:23 +00:00
test(rest) formatMsg
This commit is contained in:
parent
ecbf8f2fe5
commit
38291f1fc3
2 changed files with 37 additions and 8 deletions
|
|
@ -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
24
test/server/rest.js
Normal 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();
|
||||
});
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue