<commit_before>const RequestError = require('qnode-error').RequestError;
const BaseServer = require('./BaseServer');
const RuleEngine = require('./RuleEngine');
const Logger = require('qnode-log');

module.exports = class MockServer extends BaseServer {

    constructor(config) {
        super();
        this._name = 'mockServer_' + config.port;
        this._logger = new Logger(this._name);
        this._config = config;
    }

    prepare() {
        this._koa.use(this.mock.bind(this));

        this._engines = {};

        const vhostConfigs = this._config.vhosts;
        for (let vhostName in vhostConfigs) {
            //TODO: const vhostConfig = vhostConfigs[vhostName];
            this._engines[vhostName] = new RuleEngine('ruleEngine_' + vhostName);
        }
    }

    static normalizeRequest(req) {
        return {
            header: req.header,
            method: req.method.toLowerCase(),
            //length: req.length,
            url: req.url,
            path: req.path,
            //type: req.type,
            charset: req.charset.toLowerCase(),
            query: req.query,
            protocol: req.protocol.toLowerCase(),
            ip: req.ip,
            body: req.body
        };
    }

    putRule(vhostName, rule) {
        const engine = this._engines[vhostName];
        engine.put(rule);
    }

    static parseHost(hostHeader) {
        const pos = hostHeader.indexOf(':');
        if (pos > 0) return hostHeader.substring(0, pos);
        return hostHeader;
    }

    async mock(ctx, next) {
        const request = MockServer.normalizeRequest(ctx.request);
        const host = MockServer.parseHost(request.header.host);

        const engine = this._engines[host];
        if (!engine) {
            throw RequestError('TODO');
        }

        const response = ctx.response;
        await engine.mock(request, response);

        await next();
    }
}<commit_msg>return 404/SERVICE_NOT_FOUND when host is not found<commit_after>const RequestError = require('qnode-error').RequestError;
const BaseServer = require('./BaseServer');
const RuleEngine = require('./RuleEngine');
const Logger = require('qnode-log');

module.exports = class MockServer extends BaseServer {

    constructor(config) {
        super();
        this._name = 'mockServer_' + config.port;
        this._logger = new Logger(this._name);
        this._config = config;
    }

    prepare() {
        this._koa.use(this.mock.bind(this));

        this._engines = {};

        const vhostConfigs = this._config.vhosts;
        for (let vhostName in vhostConfigs) {
            //TODO: const vhostConfig = vhostConfigs[vhostName];
            this._engines[vhostName] = new RuleEngine('ruleEngine_' + vhostName);
        }
    }

    static normalizeRequest(req) {
        return {
            header: req.header,
            method: req.method.toLowerCase(),
            //length: req.length,
            url: req.url,
            path: req.path,
            //type: req.type,
            charset: req.charset.toLowerCase(),
            query: req.query,
            protocol: req.protocol.toLowerCase(),
            ip: req.ip,
            body: req.body
        };
    }

    putRule(vhostName, rule) {
        const engine = this._engines[vhostName];
        engine.put(rule);
    }

    static parseHost(hostHeader) {
        const pos = hostHeader.indexOf(':');
        if (pos > 0) return hostHeader.substring(0, pos);
        return hostHeader;
    }

    async mock(ctx, next) {
        const request = MockServer.normalizeRequest(ctx.request);
        const host = MockServer.parseHost(request.header.host);

        const engine = this._engines[host];
        if (!engine) {
            throw RequestError('SERVICE_NOT_FOUND', host);
        }

        const response = ctx.response;
        await engine.mock(request, response);

        await next();
    }
}
{'fraction_non_alphanumeric': 0.12124325712817878, 'fraction_numerical': 0.0017980991523246853, 'mean_word_length': 2.0782608695652174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5128980', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1100, 'n_words': 235}