<commit_before>var expect = require('expect.js'),
    rawRequestBody = require('../fixtures').requestData,
    RequestBody = require('../../lib/index.js').RequestBody,
    QueryParam = require('../../lib/index.js').QueryParam,
    PropertyList = require('../../lib/index.js').PropertyList;

/* global describe, it */
describe('RequestBody', function () {
    it('should be jsonified properly', function () {
        var reqData = new RequestBody(rawRequestBody),
            jsonified = reqData.toJSON();
        expect(jsonified).to.eql(rawRequestBody);
    });

    it('should be stringified properly', function () {
        var reqData = new RequestBody(rawRequestBody),
            expectedQueryData = QueryParam.unparse(rawRequestBody.urlencoded);

        // Set the mode to URL encoded
        reqData.mode = 'urlencoded';
        expect(reqData.toString()).to.eql(expectedQueryData);
    });

    it('should support file mode', function () {
        var rawBody = {
                mode: 'formdata',
                formdata: [
                    {
                        key: 'hiya',
                        value: 'heyo'
                    },
                    {
                        key: 'alpha',
                        value: 'beta'
                    }
                ],
                raw: 'abhijitkane',
                urlencoded: [
                    {
                        key: 'haha',
                        value: 'somevalue'
                    }
                ],
                file: {
                    src: '/somewhere/in/the/file/system'
                }
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.file).to.have.property('src', rawBody.file.src);
    });

    it('should convert file reference to object, if only the reference is given', function () {
        var rawBody = {
                mode: 'file',
                file: '/omg/where/is/this/file'
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.file).to.have.property('src', rawBody.file);
    });

    it('should allow converting form parameters to an object', function () {
        var rawBody = {
                mode: 'formdata',
                formdata: [
                    {
                        key: 'hiya',
                        value: 'heyo'
                    },
                    {
                        key: 'alpha',
                        value: 'beta'
                    }
                ]
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.formdata.toObject()).to.eql({
            hiya: 'heyo',
            alpha: 'beta'
        });
    });

    describe('sanity', function () {
        it('should be parsed properly', function () {
            var reqData = new RequestBody(rawRequestBody);
            expect(reqData).to.be.ok();
            expect(reqData).to.have.property('mode', rawRequestBody.mode);

            // Raw Request body
            expect(reqData).to.have.property('raw', rawRequestBody.raw);

            // Form data
            expect(reqData.formdata).to.be.a(PropertyList);
            expect(reqData.formdata.count()).to.eql(rawRequestBody.formdata.length);

            // URL Encoded parameters
            expect(reqData.urlencoded).to.be.a(PropertyList);
            expect(reqData.urlencoded.count()).to.eql(rawRequestBody.urlencoded.length);
        });
    });

    describe('isEmpty', function () {
        it('should return true if no request body is set', function () {
            var body = new RequestBody();
            expect(body.isEmpty()).to.be(true);
        });<commit_msg>test(unit): Added RequestBody unit tests :white_check_mark:<commit_after>var expect = require('expect.js'),
    rawRequestBody = require('../fixtures').requestData,
    RequestBody = require('../../lib/index.js').RequestBody,
    QueryParam = require('../../lib/index.js').QueryParam,
    PropertyList = require('../../lib/index.js').PropertyList;

/* global describe, it */
describe('RequestBody', function () {
    it('should be jsonified properly', function () {
        var reqData = new RequestBody(rawRequestBody),
            jsonified = reqData.toJSON();
        expect(jsonified).to.eql(rawRequestBody);
    });

    it('should be stringified properly', function () {
        var reqData = new RequestBody(rawRequestBody),
            expectedQueryData = QueryParam.unparse(rawRequestBody.urlencoded);

        // Set the mode to URL encoded
        reqData.mode = 'urlencoded';
        expect(reqData.toString()).to.eql(expectedQueryData);
    });

    it('should support file mode', function () {
        var rawBody = {
                mode: 'formdata',
                formdata: [
                    {
                        key: 'hiya',
                        value: 'heyo'
                    },
                    {
                        key: 'alpha',
                        value: 'beta'
                    }
                ],
                raw: 'abhijitkane',
                urlencoded: [
                    {
                        key: 'haha',
                        value: 'somevalue'
                    }
                ],
                file: {
                    src: '/somewhere/in/the/file/system'
                }
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.file).to.have.property('src', rawBody.file.src);
    });

    it('should convert file reference to object, if only the reference is given', function () {
        var rawBody = {
                mode: 'file',
                file: '/omg/where/is/this/file'
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.file).to.have.property('src', rawBody.file);
    });

    it('should allow converting form parameters to an object', function () {
        var rawBody = {
                mode: 'formdata',
                formdata: [
                    {
                        key: 'hiya',
                        value: 'heyo'
                    },
                    {
                        key: 'alpha',
                        value: 'beta'
                    }
                ]
            },
            reqData = new RequestBody(rawBody);
        expect(reqData.formdata.toObject()).to.eql({
            hiya: 'heyo',
            alpha: 'beta'
        });
    });

    describe('.update', function () {
        it('should use the raw request body by if an invalid mode is specified', function () {
            var reqData = new RequestBody({
                mode: 'random',
                raw: 'This is supposed to be a raw body. Do not cook it.'
            });
            expect(reqData).to.be.ok();
            expect(reqData).to.have.property('mode', 'raw');
        });

        it('should support stringified urlencoded bodies', function () {
            var reqData = new RequestBody({
                mode: 'urlencoded',
                urlencoded: 'alpha=foo&beta=bar'
            });

            expect(reqData.urlencoded.reference).to.eql({
                alpha: {
                    key: 'alpha',
                    value: 'foo'
                },
                beta: {
                    key: 'beta',
                    value: 'bar'
                }
            });
        });

        it('should handle missing formdata bodies correctly', function () {
            var reqData = new RequestBody({ mode: 'formdata' });

            expect(reqData.formdata.reference).to.be.empty();
        });
    });

    describe('.toString', function () {
        it('should return an empty string for formdata or files', function () {
            var rBody = new RequestBody({ mode: 'formdata' });
            expect(rBody.toString()).to.be('');

            rBody.update({ mode: 'file' });
            expect(rBody.toString()).to.be('');
        });

        it('should correctly stringify urlencoded bodies', function () {
            var rBody = new RequestBody({
                mode: 'urlencoded',
                urlencoded: [{ key: 'foo', value: 'bar' }]
            });

            expect(rBody.toString()).to.be('foo=bar');
        });

        it('should handle malformed urlencoded request bodies gracefully', function () {
            var rBody = new RequestBody({ mode: 'urlencoded' });

            rBody.urlencoded = ['a', 'b']; // This ensures that there is an inbuilt fallback to .unparse
            expect(rBody.toString()).to.be('a,b');

            rBody.urlencoded = Object.create(null); // This ensures that there is an inbuilt fallback to .unparse
            expect(rBody.toString()).to.be('');
        });

        it('should handle raw bodies correctly', function () {
            var rBody = new RequestBody({ mode: 'raw', raw: 'Arbitrary raw request body' });

            expect(rBody.toString()).to.be('Arbitrary raw request body');
            rBody.update({ mode: 'raw' }); // clear the request body
            expect(rBody.toString()).to.be('');
        });

        it('should return an empty string for arbitrary request body modes', function () {
            var rBody = new RequestBody();

            rBody.mode = 'random';
            expect(rBody.toString()).to.be('');
        });
    });

    describe('sanity', function () {
        it('should be parsed properly', function () {
            var reqData = new RequestBody(rawRequestBody);
            expect(reqData).to.be.ok();
            expect(reqData).to.have.property('mode', rawRequestBody.mode);

            // Raw Request body
            expect(reqData).to.have.property('raw', rawRequestBody.raw);

            // Form data
            expect(reqData.formdata).to.be.a(PropertyList);
            expect(reqData.formdata.count()).to.eql(rawRequestBody.formdata.length);

            // URL Encoded parameters
            expect(reqData.urlencoded).to.be.a(PropertyList);
            expect(reqData.urlencoded.count()).to.eql(rawRequestBody.urlencoded.length);
        });
    });

    describe('isEmpty', function () {
        it('should return true if no request body is set', function () {
            var body = new RequestBody();
            expect(body.isEmpty()).to.be(true);
        });
{'fraction_non_alphanumeric': 0.13200934579439252, 'fraction_numerical': 0.0, 'mean_word_length': 1.4892173491640417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1185706', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2564, 'n_words': 571}