"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let assert = require('assert');
const request = require("supertest");
const utils_1 = require("../utils");
let rfs = require('rotating-file-stream');
let base = require('../index');
let promBundle = require("express-prom-bundle");
let options = {
    morgan: {
        useStdout: false,
        logStream: {
            path: "logs",
            logFile: "access.log"
        }
    },
    swagger: {
        apis: ["../../../lib/test/test-express.js"],
        allowedDomains: ["domain.com", "localhost"]
    }
};
describe('test express', function () {
    it('health method', function (done) {
        let web = base.express(options);
        base.setProjectFunctionality(function () {
            return true;
        });
        base.setProjectStatus(function () {
            return {
                "extra": "linked"
            };
        });
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/health')
            .set('Content-Type', 'application/json')
            .expect(200, function (err, res) {
            assert.notEqual(res, null);
            assert.notEqual(res.body, null);
            assert.equal(res.body.status, 200);
            assert.notEqual(res.body.details, null);
            assert.notEqual(res.body.details.base, null);
            assert.equal(res.body.details.base.cassandra, "inactive");
            assert.equal(res.body.details.base.elastic, "inactive");
            assert.equal(res.body.details.base.redis, "inactive");
            assert.notEqual(res.body.details.personal, null);
            assert.equal(res.body.details.personal.extra, "linked");
            //Test with unfunctional case
            base.setProjectFunctionality(function () {
                return false;
            });
            request(app).get('/health')
                .set('Content-Type', 'application/json')
                .expect(500, done);
        });
    });
    /**
     * @swagger
    * /test_swagger:
    *   get:
    *     summary: GET
    *     description:
    *       "I get this text"
    */
    it('swagger.json method', function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/swagger.json')
            .set('Content-Type', 'application/json')
            .expect(200, function (err, res) {
            if (err)
                done(err);
            assert.notEqual(res, null);
            assert.notEqual(res.body, null);
            assert.notEqual(res.body.paths, null);
            assert.notEqual(res.body.paths['/test_swagger'], null);
            assert.notEqual(res.body.paths['/test_swagger'].get, null);
            assert.equal(res.body.paths['/test_swagger'].get.summary, "GET");
            assert.equal(res.body.paths['/test_swagger'].get.description, "I get this text");
            done();
        });
    });
    it('swagger autorization for https://test.domain.com:7892', function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.all('/*', function (req, res, next) {
            done("Cant get here any option request");
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).options('/test')
            .set('origin', 'https://test.domain.com:7892')
            .expect(200, function (err, res) {
            if (err)
                done(err);
            assert.notEqual(res, null);
            assert.notEqual(res.headers, null);
            assert.equal(res.headers['access-control-allow-origin'], "https://test.domain.com:7892");
            assert.equal(res.headers['access-control-allow-headers'], "Origin, X-Requested-With, Content-Type, Accept, Authorization");
            assert.equal(res.headers['access-control-allow-methods'], "GET, POST, OPTIONS");
            done();
        });
    });
    it('swagger autorization for localhost', function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.all('/*', function (req, res, next) {
            done("Cant get here any option request");
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).options('/test')
            .set('origin', 'localhost')
            .expect(200, function (err, res) {
            if (err)
                done(err);
            assert.notEqual(res, null);
            assert.notEqual(res.headers, null);
            assert.equal(res.headers['access-control-allow-origin'], "localhost");
            assert.equal(res.headers['access-control-allow-headers'], "Origin, X-Requested-With, Content-Type, Accept, Authorization");
            assert.equal(res.headers['access-control-allow-methods'], "GET, POST, OPTIONS");
            done();
        });
    });
    it('test morgan', function (done) {
        let web = base.express.mock(options);
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        web.handleWriteStream(function (chunk, encoding, callback) {
            assert.notEqual(chunk.indexOf("GET"), -1);
            assert.notEqual(chunk.indexOf("/test?val=Test1"), -1);
            done();
        });
        request(app).get('/test')
            .query({ val: 'Test1' })
            .set('Content-Type', 'application/json')
            .expect(500, function (err, res) { });
    });
    it('test morgan custom logstream', function (done) {
        let logStream = rfs("test_name", {
            interval: "1d",
            maxFiles: 1,
            compress: "gzip",
            path: options.morgan != null && options.morgan.logStream != null && options.morgan.logStream.path != null ? options.morgan.logStream.path : "logs"
        });
        logStream.write = function (chunk, encoding, callback) {
            assert.notEqual(chunk.indexOf("GET"), -1);
            assert.notEqual(chunk.indexOf("/test?val=Test1"), -1);
            done();
        };
        let web = base.express(options);
        web.setLogStream(logStream);
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/test')
            .query({ val: 'Test1' })
            .set('Content-Type', 'application/json')
            .expect(500, function (err, res) { });
    });
    it('test morgan custom log name generator', function (done) {
        let web = base.express.mock(options);
        web.setLogFileGenerator(function (time, index) {
            return "test_name";
        });
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        web.handleWriteStream(function (chunk, encoding, callback) {
            let path = options.morgan != null && options.morgan.logStream != null && options.morgan.logStream.path != null ? options.morgan.logStream.path : "logs";
            path = utils_1.default.getFullRouteFrom(path);
            assert.equal(this.name, path + "/test_name");
            done();
        });
        request(app).get('/test')
            .query({ val: 'Test1' })
            .set('Content-Type', 'application/json')
            .expect(500, function (err, res) { });
    });
    it('test customGenerator', function (done) {
        let web = base.express.mock(options);
        let logFile = options.morgan != null && options.morgan.logStream != null && options.morgan.logStream.logFile != null ? options.morgan.logStream.logFile : "access.log";
        assert.equal(web.mockCustomGenerator(new Date('July 20, 69 00:20:18'), 1), "19690720-0020-1-" + logFile + ".gz");
        assert.equal(web.mockCustomGenerator(null, null), logFile);
        done();
    });
    it('test allowedDomainsRegex', function (done) {
        let web = base.express.mock(options);
        web.initialize();
        let expected = new RegExp("^((https?:\/\/)?([^:\/]+.)*((domain.com)|(localhost))(:[0-9]+)?)$");
        assert.equal(web.allowedDomains.toString(), expected.toString());
        let options2 = {
            logStream: {
                path: "logs",
                logFile: "access.log"
            },
            swagger: {
                apis: ["./lib/test/test-express.js"],
                allowedDomains: "domain.com"
            }
        };
        let web2 = base.express.mock(options2);
        web2.initialize();
        let expected2 = new RegExp("^$");
        assert.equal(web2.allowedDomains.toString(), expected2.toString());
        let options3 = {
            logStream: {
                path: "logs",
                logFile: "access.log"
            },
            swagger: {
                apis: ["./lib/test/test-express.js"],
                allowedDomains: {}
            }
        };
        let web3 = base.express.mock(options3);
        web3.initialize();
        let expected3 = new RegExp("^$");
        assert.equal(web3.allowedDomains.toString(), expected3.toString());
        done();
    });
    it('test body parser', function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.post('/body_parser', function (req, res) {
            assert.notEqual(req, null);
            assert.notEqual(req.body, null);
            assert.equal(req.body.username, "test name");
            assert.equal(req.body.password, "secret");
            res.status(200);
            res.send(JSON.stringify("Done"));
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).post('/body_parser')
            .set('Content-Type', 'application/json')
            .send({
            username: 'test name',
            password: 'secret'
        })
            .expect(200, done);
    });
    it('test zipkin set ids', function (done) {
        options.zipkin = {
            isActive: true
        };
        let web = base.express(options);
        let app = web.initialize();
        app.get('/test', function (req, res) {
            assert.notEqual(res, null);
            assert.notEqual(res.locals, null);
            assert.notEqual(res.locals.spanId, null);
            assert.notEqual(res.locals.traceId, null);
            res.status(200);
            res.send(JSON.stringify("Done"));
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/test')
            .set('Content-Type', 'application/json')
            .expect(200, done);
    });
    it('test zipkin dont set ids', function (done) {
        options.zipkin = {
            isActive: false
        };
        let web = base.express(options);
        let app = web.initialize();
        app.get('/test', function (req, res) {
            if (res != null && res.locals != null) {
                assert.equal(res.locals.spanId, null);
                assert.equal(res.locals.traceId, null);
            }
            res.status(200);
            res.send(JSON.stringify("Done"));
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/test')
            .set('Content-Type', 'application/json')
            .expect(200, done);
    });
});
//# sourceMappingURL=test-express.js.map