'use strict';

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _mocha = require('mocha');

var _downstreamCache = require('../../src/behaviors/downstreamCache.js');

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

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

(0, _mocha.describe)('BehaviorDownstreamCache', function () {
    (0, _mocha.describe)('bust', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/downstreamCache.bust.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }
                var opts = JSON.parse(options);

                var expected = ['aka_downstream_headers["Cache-Control"] = {"SET", "max-age=0, no-cache, no-store"}', 'aka_downstream_headers["Pragma"] = {"SET", "no-cache"}', 'aka_downstream_headers["Expires"] = {"SET", "1 January, 1970 00:00:01 GMT"}'];

                var actual = new _downstreamCache.BehaviorDownstreamCache(opts).process();

                _assert2.default.equal(actual[0], expected[0]);
                _assert2.default.equal(actual[1], expected[1]);
                _assert2.default.equal(actual[2], expected[2]);
                done();
            });
        });
    });
    (0, _mocha.describe)('tunnel origin', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/downstreamCache.tunnel.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }
                var opts = JSON.parse(options);

                var expected = '-- let cache headers pass though from origin';

                var actual = new _downstreamCache.BehaviorDownstreamCache(opts).process();

                _assert2.default.equal(actual, expected);
                done();
            });
        });
    });
    (0, _mocha.describe)('allow max age', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/downstreamCache.allowMaxAge.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }
                var opts = JSON.parse(options);

                var expected = ['aka_downstream_headers["Cache-Control"] = {"SET", "max-age=" .. ngx.var.aka_cache_ttl_seconds}', 'aka_downstream_headers["Expires"] = {"SET", expiryDateString(ngx.var.aka_cache_ttl_seconds)}', '-- let cache headers pass though', 'ngx.header["Cache-Control"] = "private"'];

                var actual = new _downstreamCache.BehaviorDownstreamCache(opts).process();

                _assert2.default.equal(actual[0], expected[0]);
                _assert2.default.equal(actual[1], expected[1]);
                _assert2.default.equal(actual[2], expected[2]);
                _assert2.default.equal(actual[3], expected[3]);
                done();
            });
        });
    });
    (0, _mocha.describe)('allow from value', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/downstreamCache.allowFromValue.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }
                var opts = JSON.parse(options);

                var expected = ['aka_downstream_headers["Cache-Control"] = {"SET", "max-age=" .. 7776000}', 'aka_downstream_headers["Expires"] = {"SET", expiryDateString(7776000)}'];

                var actual = new _downstreamCache.BehaviorDownstreamCache(opts).process();

                _assert2.default.equal(actual[0], expected[0]);
                _assert2.default.equal(actual[1], expected[1]);
                done();
            });
        });
    });
    (0, _mocha.describe)('must revalidate', function () {
        (0, _mocha.it)('should return expected lua', function (done) {
            _fs2.default.readFile(__dirname + '/downstreamCache.mustRevalidate.papi.json', 'utf8', function (err, options) {
                if (err) {
                    throw err;
                }
                var opts = JSON.parse(options);

                var expected = 'aka_downstream_headers["Cache-Control"] = {"SET", "no-cache"}';

                var actual = new _downstreamCache.BehaviorDownstreamCache(opts).process();

                _assert2.default.equal(actual, expected);
                done();
            });
        });
    });
});