From 38291f1fc3503a55e9a928fad2745c0fe3de93da Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 23 Oct 2017 18:02:07 +0300 Subject: [PATCH] test(rest) formatMsg --- server/rest.js | 21 +++++++++++++-------- test/server/rest.js | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 test/server/rest.js diff --git a/server/rest.js b/server/rest.js index 34240164..53470383 100644 --- a/server/rest.js +++ b/server/rest.js @@ -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); } diff --git a/test/server/rest.js b/test/server/rest.js new file mode 100644 index 00000000..4a791095 --- /dev/null +++ b/test/server/rest.js @@ -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(); +}); +