Code coverage report for lib/services/coapUtils.js

Statements: 94.59% (35 / 37)      Branches: 78.57% (11 / 14)      Functions: 100% (3 / 3)      Lines: 94.59% (35 / 37)     

All files » lib/services/ » coapUtils.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                              1   1                             1 61   61   1 239   239   239     61       61 61   61         61 1   60                   1 60   60   60 180   180 711 711 177         180 3       60 3 3   3 3   57       1 1
/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-lwm2m-lib.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */
 
'use strict';
 
var errors = require('../errors'),
    logger = require('logops'),
    context = {
        op: 'LWM2MLib.COAPUtils'
    };
 
/**
 * Extract the query parameters from a COAP request, creating a JS Object with them. The function can be executed both
 * synchronously (if no callback is provided) or asynchronously.
 *
 * @param {Object}   req        COAP Request to process.
 * @param {Function} callback   Callback function (optional). The second parameter contains the query object.
 *
 * @returns {Object}            Query parameters object.
 */
function extractQueryParams(req, callback) {
    var queryParams;
 
    logger.debug(context, 'Extracting query parameters from request');
 
    function extractAsObject(previous, current) {
        var fields = current.split('=');
 
        previous[fields[0]] = fields[1];
 
        return previous;
    }
 
    Iif (!req.urlObj) {
        req.urlObj = require('url').parse(req.url);
    }
 
    Eif (req.urlObj.query) {
        logger.debug(context, 'Processing query [%s]', req.urlObj.query);
 
        queryParams = req.urlObj.query.split('&');
    } else {
        queryParams = {};
    }
 
    if (callback) {
        callback(null, queryParams.reduce(extractAsObject, {}));
    } else {
        return queryParams.reduce(extractAsObject, {});
    }
}
 
/**
 * Checks that all the mandatory query parameters are present in the Query Parameters object. If any parameter is not
 * present, the callback is invoked with a BadRequestError, indicating the missing parameters.
 *
 * @param {Object} queryParams          Query Parameters object.
 */
function checkMandatoryQueryParams(mandatoryQueryParams, queryParams, callback) {
    var missing = [];
 
    logger.debug(context, 'Checking for the existence of the following parameters [%j]', mandatoryQueryParams);
 
    for (var p in mandatoryQueryParams) {
        var found = false;
 
        for (var i in queryParams) {
            Eif (queryParams.hasOwnProperty(i)) {
                if (i === mandatoryQueryParams[p]) {
                    found = true;
                }
            }
        }
 
        if (!found) {
            missing.push(mandatoryQueryParams[p]);
        }
    }
 
    if (missing.length !== 0) {
        var error = new errors.BadRequestError('Missing query params: ');
        error.code = '4.00';
 
        logger.debug(context, 'Missing parameters found [%j]', missing);
        callback(error);
    } else {
        callback();
    }
}
 
exports.extractQueryParams = extractQueryParams;
exports.checkMandatoryQueryParams = checkMandatoryQueryParams;