'use strict';

var _mocha = require('mocha');

var _chai = require('chai');

var _supertest = require('supertest');

var _supertest2 = _interopRequireDefault(_supertest);

var _integrationSpecInt = require('../_integration.spec-int.js');

var _integrationSpecInt2 = _interopRequireDefault(_integrationSpecInt);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('BehaviorSetVariable', function () {

    (0, _mocha.describe)('set variable by expression containing var', function () {

        var request = (0, _supertest2.default)(_integrationSpecInt2.default.urlPrefix);

        (0, _mocha.it)('should result in a variable value with the subvar swapped', function (done) {

            request.get(_integrationSpecInt2.default.testUrl('setVariable.expression.papi.json')).expect(200).end(function (err, res) {
                (0, _chai.expect)(res.headers[_integrationSpecInt2.default.variableCheckHeaderName.toLowerCase()]).to.equal('hostname is localhost', 'generator processing failed');
                done();
            });
        });
    });

    (0, _mocha.describe)('set variable by random generator', function () {

        var request = (0, _supertest2.default)(_integrationSpecInt2.default.urlPrefix);

        var opts = void 0;
        (0, _mocha.before)(function (done) {
            _integrationSpecInt2.default.config().then(function (papiOpts) {
                opts = papiOpts.behavior['setVariable.generate.papi.json'];
                done();
            });
        });

        (0, _mocha.it)('should result in random number in the correct range', function (done) {

            request.get(_integrationSpecInt2.default.testUrl('setVariable.generate.papi.json')).expect(200).end(function (err, res) {
                var rand = parseInt(res.headers[_integrationSpecInt2.default.variableCheckHeaderName.toLowerCase()]);
                (0, _chai.expect)(rand).to.be.within(parseInt(opts.minRandomNumber), parseInt(opts.maxRandomNumber), 'random number out of range');
                done();
            });
        });
    });

    (0, _mocha.describe)('set variable by querystring extract + transform upper', function () {

        var request = (0, _supertest2.default)(_integrationSpecInt2.default.urlPrefix);

        var opts = void 0;
        (0, _mocha.before)(function (done) {
            _integrationSpecInt2.default.config().then(function (papiOpts) {
                opts = papiOpts.behavior['setVariable.extract-qs-upper.papi.json'];
                done();
            });
        });

        (0, _mocha.it)('should set variable from querystring and transform to upper', function (done) {

            request.get(_integrationSpecInt2.default.testUrl('setVariable.extract-qs-upper.papi.json') + '&' + opts.queryParameterName + '=abc').expect(200).end(function (err, res) {
                (0, _chai.expect)(res.headers[_integrationSpecInt2.default.variableCheckHeaderName.toLowerCase()]).to.equal('ABC', 'querystring not set as upper var');
                done();
            });
        });
    });

    (0, _mocha.describe)('set variable by header extract + transform add', function () {

        var request = (0, _supertest2.default)(_integrationSpecInt2.default.urlPrefix);

        var opts = void 0;
        (0, _mocha.before)(function (done) {
            _integrationSpecInt2.default.config().then(function (papiOpts) {
                opts = papiOpts.behavior['setVariable.extract-header-add.papi.json'];
                done();
            });
        });

        (0, _mocha.it)('should set variable from header and add operandOne', function (done) {

            request.get(_integrationSpecInt2.default.testUrl('setVariable.extract-header-add.papi.json')).set(opts.headerName, 5).expect(200).end(function (err, res) {
                (0, _chai.expect)(res.headers[_integrationSpecInt2.default.variableCheckHeaderName.toLowerCase()]).to.equal('10', 'header calculation failed');
                done();
            });
        });
    });

    (0, _mocha.describe)('set variable by querystring extract + transform stringindex', function () {

        var request = (0, _supertest2.default)(_integrationSpecInt2.default.urlPrefix);

        var opts = void 0;
        (0, _mocha.before)(function (done) {
            _integrationSpecInt2.default.config().then(function (papiOpts) {
                opts = papiOpts.behavior['setVariable.extract-qs-stringindex.papi.json'];
                done();
            });
        });

        (0, _mocha.it)('should find the correct string index', function (done) {

            request.get(_integrationSpecInt2.default.testUrl('setVariable.extract-qs-stringindex.papi.json') + '&' + opts.queryParameterName + '=this target').expect(200).end(function (err, res) {
                (0, _chai.expect)(parseInt(res.headers[_integrationSpecInt2.default.variableCheckHeaderName.toLowerCase()])).to.equal(5, 'incorrect string index returned');
                done();
            });
        });
    });
});