@studiolabs/strong-remoting
Version:
StrongLoop Remoting Module
70 lines (59 loc) • 2.19 kB
JavaScript
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: strong-remoting
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
;
var urlEncodedContext = require('./_urlencoded.context');
module.exports = function(ctx) {
suite('query string', urlEncodedContext(ctx, 'qs'));
suite('form data', urlEncodedContext(ctx, 'form'));
};
function suite(prefix, ctx) {
var EMPTY_QUERY = ctx.EMPTY_QUERY;
var ERROR_BAD_REQUEST = ctx.ERROR_BAD_REQUEST;
var verifyTestCases = ctx.verifyTestCases;
describe(prefix + ' - boolean - required', function() {
// See verifyTestCases' jsdoc for details about the format of test cases.
verifyTestCases({arg: 'arg', type: 'boolean', required: true}, [
// Valid values
['arg=false', false],
['arg=true', true],
['arg=0', false],
// Empty values should trigger ERROR_BAD_REQUEST
[EMPTY_QUERY, ERROR_BAD_REQUEST],
['arg', ERROR_BAD_REQUEST],
['arg=', ERROR_BAD_REQUEST],
// Empty-like values should trigger ERROR_BAD_REQUEST too
['arg=undefined', ERROR_BAD_REQUEST],
['arg=null', ERROR_BAD_REQUEST],
]);
});
describe(prefix + ' - boolean - optional', function() {
// See verifyTestCases' jsdoc for details about the format of test cases.
verifyTestCases({arg: 'arg', type: 'boolean'}, [
// Empty values
[EMPTY_QUERY, undefined],
['arg', undefined],
['arg=', undefined],
// Valid values
['arg=false', false],
['arg=true', true],
['arg=0', false],
['arg=1', true],
// values are case insensitive
['arg=FalsE', false],
['arg=TruE', true],
['arg=FALSE', false],
['arg=TRUE', true],
// Invalid values should trigger ERROR_BAD_REQUEST
['arg=undefined', ERROR_BAD_REQUEST],
['arg=null', ERROR_BAD_REQUEST],
['arg=2', ERROR_BAD_REQUEST],
['arg=text', ERROR_BAD_REQUEST],
['arg=[]', ERROR_BAD_REQUEST],
['arg=[1,2]', ERROR_BAD_REQUEST],
['arg={}', ERROR_BAD_REQUEST],
['arg={"a":true}', ERROR_BAD_REQUEST],
]);
});
}