javascriptobjectrandomfuzzing

Random object generator in JavaScript


I need a random object generator in JavaScript that generates a variety of objects with different fields and values. Any ideas where I can find such tool?

I need to generate random objects with various complexity.. My goal is to use JSON in order to serialize these objects and fuzz test my application http api.


Solution

  • function createRandomObj(fieldCount, allowNested)
    {
        var generatedObj = {};
    
        for(var i = 0; i < fieldCount; i++) {
            var generatedObjField;
    
            switch(randomInt(allowNested ? 6 : 5)) {
    
                case 0:
                generatedObjField = randomInt(1000);
                break;
    
                case 1:
                generatedObjField = Math.random();
                break;
    
                case 2:
                generatedObjField = Math.random() < 0.5 ? true : false;
                break;
    
                case 3:
                generatedObjField = randomString(randomInt(4) + 4);
                break;
    
                case 4:
                generatedObjField = null;
                break;
    
                case 5:
                generatedObjField = createRandomObj(fieldCount, allowNested);
                break;
            }
            generatedObj[randomString(8)] = generatedObjField;
        }
        return generatedObj;
    }
    
    // helper functions
    
    function randomInt(rightBound)
    {
        return Math.floor(Math.random() * rightBound);
    }
    
    function randomString(size)
    {
        var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var generatedString = '';
        for(var i = 0; i < size; i++) {
            generatedString += alphaChars[randomInt(alphaChars.length)];
        }
    
        return generatedString;
    }
    

    It will create a obj with X paramenters, all with a integer, float, string, boolean or null value.
    I just made it :B