<commit_before>'use strict';

// external requires
var assert = require('chai').assert;
var restify = require('restify');
var restifyClients = require('restify-clients');


// local files
var helper = require('./lib/helper');
var plugins = require('../lib');

// local globals
var SERVER;
var CLIENT;
var PORT;
var MESSAGE = 'Malformed request syntax';

describe.only('request url validator', function () {

    beforeEach(function (done) {
        SERVER = restify.createServer({
            dtrace: helper.dtrace,
            log: helper.getLog('server')
        });

        SERVER.listen(PORT, '127.0.0.1', function () {
            PORT = SERVER.address().port;
            CLIENT = restifyClients.createJsonClient({
                url: 'http://127.0.0.1:' + PORT,
                dtrace: helper.dtrace,
                retry: false
            });

            done();
        });
    });

    afterEach(function (done) {
        CLIENT.close();
        SERVER.close(done);
    });

    it('should respond 200 without validation plugin', function (done) {

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.notEqual(req.params.name, 'josep&jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep&jorge',
            function (err, _, res) {

                assert.ifError(err);
                assert.equal(res.statusCode, 200);
                done();
            });
    });


    it('should respond 400 to malformed query param', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id', function (req, res, next) {
            res.send();
            next();
        });

        CLIENT.get('/query/foo?id=bar&name="josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond 400 without message opt', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator());

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id', function (req, res, next) {
            res.send();
            next();
        });

        CLIENT.get('/query/foo?id=bar&name="josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: ''
                });
                done();
            });
    });

    it('should respond 400 to query param with amp and plus', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep+&+jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond to invalid query param value with 400', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond to valid query param value with 200', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep+%26+jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 200);
                done();
            });
    });

    it('should respond 200 with scaped amp and \s', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep%20%26%20jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 200);
                done();
            });
    });
});
<commit_msg>rm only in test describe<commit_after>'use strict';

// external requires
var assert = require('chai').assert;
var restify = require('restify');
var restifyClients = require('restify-clients');


// local files
var helper = require('./lib/helper');
var plugins = require('../lib');

// local globals
var SERVER;
var CLIENT;
var PORT;
var MESSAGE = 'Malformed request syntax';

describe('request url validator', function () {

    beforeEach(function (done) {
        SERVER = restify.createServer({
            dtrace: helper.dtrace,
            log: helper.getLog('server')
        });

        SERVER.listen(PORT, '127.0.0.1', function () {
            PORT = SERVER.address().port;
            CLIENT = restifyClients.createJsonClient({
                url: 'http://127.0.0.1:' + PORT,
                dtrace: helper.dtrace,
                retry: false
            });

            done();
        });
    });

    afterEach(function (done) {
        CLIENT.close();
        SERVER.close(done);
    });

    it('should respond 200 without validation plugin', function (done) {

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.notEqual(req.params.name, 'josep&jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep&jorge',
            function (err, _, res) {

                assert.ifError(err);
                assert.equal(res.statusCode, 200);
                done();
            });
    });


    it('should respond 400 to malformed query param', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id', function (req, res, next) {
            res.send();
            next();
        });

        CLIENT.get('/query/foo?id=bar&name="josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond 400 without message opt', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator());

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id', function (req, res, next) {
            res.send();
            next();
        });

        CLIENT.get('/query/foo?id=bar&name="josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: ''
                });
                done();
            });
    });

    it('should respond 400 to query param with amp and plus', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep+&+jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond to invalid query param value with 400', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep&jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 400);
                assert.deepEqual(JSON.parse(res.body), {
                    code: 'BadRequestError',
                    message: MESSAGE
                });
                done();
            });
    });

    it('should respond to valid query param value with 200', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep+%26+jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 200);
                done();
            });
    });

    it('should respond 200 with scaped amp and \s', function (done) {


        SERVER.pre(plugins.pre.reqUrlValidator({
            message: MESSAGE
        }));

        SERVER.use(plugins.queryParser({
            mapParams: true,
            overrideParams: true
        }));

        SERVER.get('/query/:id',
            function (req, res, next) {

                assert.equal(req.params.id, 'bar');
                assert.equal(req.params.name, 'josep & jorge');
                assert.deepEqual(req.query, req.params);
                res.send();
                next();
            });

        CLIENT.get('/query/foo?id=bar&name=josep%20%26%20jorge',
            function (err, _, res) {

                assert.equal(typeof err, 'object');
                assert.equal(res.statusCode, 200);
                done();
            });
    });
});

{'fraction_non_alphanumeric': 0.139337112952464, 'fraction_numerical': 0.009012937927024277, 'mean_word_length': 1.456087111745805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7193913', 'n_tokens_mistral': 4017, 'n_tokens_neox': 3644, 'n_words': 700}