"use strict";
/*
 * @adonisjs/core
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestHttpServer = void 0;
const utils_1 = require("../../utils");
/**
 * The test HTTP server to handle and serve HTTP request
 */
class TestHttpServer {
    constructor(application) {
        this.application = application;
    }
    /**
     * Creates the HTTP server to handle incoming requests. The server is just
     * created but not listening on any port.
     */
    create(serverCallback) {
        this.server = this.application.container.use('Adonis/Core/Server');
        (0, utils_1.createHttpServer)(this.application, this.server, serverCallback);
    }
    /**
     * Start the HTTP server to listen for incoming requests
     */
    listen() {
        const host = this.application.env.get('HOST', '0.0.0.0');
        const port = Number(this.application.env.get('PORT', '3333'));
        return new Promise((resolve) => {
            this.server.instance.listen(port, host, () => {
                this.application.logger.info('started server on %s:%s', host, port);
                resolve();
            });
        });
    }
    /**
     * Close HTTP server from listening to any requests
     */
    close() {
        return new Promise((resolve) => this.server.instance.close(() => resolve()));
    }
    /**
     * Start the test HTTP server
     */
    async start(serverCallback) {
        this.create(serverCallback);
        await this.listen();
        return () => this.close();
    }
}
exports.TestHttpServer = TestHttpServer;
