<commit_before>"use strict";

var http = require("http"),
    rateLimiter = require("./../../index.js"),
    request = require("request"),
    url = require('url');

var HTTP_OK = 200,
    HTTP_NOT_FOUND = 404,
    HTTP_TOO_MANY_REQUESTS = 429,
    HTTP_INTERNAL_SERVER_ERROR = 500,
    REQUEST_ID_HEADER = "X-RateLimiter-IntegrationTest-RequestId";

function IntegrationTester() {
    var _this = this;
    this.testBackendServer = http.createServer(function (req, res) {
        var sentMessage = _this.sentMessages[getRequestId(req)];

        _this.requestBuffer.push({
            req: req,
            res: res,
            setOnServedCallback: sentMessage.setOnServedCallback,
            setOnFailedCallback: sentMessage.setOnFailedCallback
        });
        sentMessage.onForwardedCallback();

    }).listen(8081);

    this.rateLimiter = rateLimiter.createRateLimiter({
        listenPort: 8080,
        forwardPort: 8081,
        forwardHost: 'localhost',
        configRefreshInterval: 10000,
        configEndpoint: 'file:./test/fixtures/example_configuration.json'
    })
}
exports.IntegrationTester = IntegrationTester;

IntegrationTester.prototype = {

    requestBuffer: [],
    sentMessages: {},
    requestId: 0,

    sendRequest: function (count, options) {
        if (typeof count === "undefined" || count === 0) {
            count = 1;
        }
        var lastMessage;
        while (count-- > 0) {
            lastMessage = new SentMessage(this, ++this.requestId, options);
        }
        return lastMessage;
    },

    serveRequests: function (howMany) {
        if (this.requestBuffer.length === 0) {
            throw "RequestBuffer empty, cannot serve any requests";
        }

        if (typeof howMany === "undefined" || howMany === 0) {
            howMany = this.requestBuffer.length;
        }

        var fromFirst = false;
        if (howMany < 0) {
            fromFirst = true;
            howMany *= -1;
        }

        if (howMany > this.requestBuffer.length) {
            throw "cannot serve more requests than the size of the request buffer (" + howMany + " > " + this.requestBuffer.length + ")";
        }

        var lastServedRequest;
        while (howMany-- > 0) {
            lastServedRequest = fromFirst ? this.requestBuffer.shift() : this.requestBuffer.pop();
            flushSingleRequest(HTTP_OK, lastServedRequest.req, lastServedRequest.res);
        }

        return new ServedRequestWrapper(lastServedRequest);
    },

    serveRequestWithStatusCode: function (statusCode) {
        if (this.requestBuffer.length === 0) {
            throw "RequestBuffer empty, cannot serve any requests";
        }

        var servedRequest = this.requestBuffer.pop();
        flushSingleRequest(statusCode, servedRequest.req, servedRequest.res);

        return new ServedRequestWrapper(servedRequest);
    },

    failRequest: function () {
        var lastServedRequest = this.requestBuffer.pop();
        var res = lastServedRequest.res;
        res.writeHead("invalid_status_code", {'Content-Type': 'text/plain'});
        res.end();
        return new FailedRequestWrapper(lastServedRequest);
    },

    reset: function (done) {
        this.requestId = 0;
        if (typeof done !== "function") {
            done = function () { /* noop */
            };
        }
        if (this.requestBuffer.length > 0) {
            this.serveRequests().onServed(done);
        } else {
            done();
        }
    },

    pendingRequestsCount: function () {
        return this.requestBuffer.length;
    }
};

function flushSingleRequest(statusCode, request, response) {
    response.writeHead(statusCode, {'Content-Type': 'text/plain'});
    response.write("Hello ratelimiter!");
    response.end();
}

function getRequestId(request) {
    return request.headers[REQUEST_ID_HEADER.toLowerCase()];
}

function ServedRequestWrapper(servedRequest) {
    this.onServed = function (callback) {
        servedRequest.setOnServedCallback(callback);<commit_msg>Add function to close test backend in integration-tester<commit_after>"use strict";

var http = require("http"),
    rateLimiter = require("./../../index.js"),
    request = require("request"),
    url = require('url');

var HTTP_OK = 200,
    HTTP_NOT_FOUND = 404,
    HTTP_TOO_MANY_REQUESTS = 429,
    HTTP_INTERNAL_SERVER_ERROR = 500,
    REQUEST_ID_HEADER = "X-RateLimiter-IntegrationTest-RequestId";

function IntegrationTester() {
    var _this = this;
    this.testBackendServer = http.createServer(function (req, res) {
        var sentMessage = _this.sentMessages[getRequestId(req)];

        _this.requestBuffer.push({
            req: req,
            res: res,
            setOnServedCallback: sentMessage.setOnServedCallback,
            setOnFailedCallback: sentMessage.setOnFailedCallback
        });
        sentMessage.onForwardedCallback();

    }).listen(8081);

    this.rateLimiter = rateLimiter.createRateLimiter({
        listenPort: 8080,
        forwardPort: 8081,
        forwardHost: 'localhost',
        configRefreshInterval: 10000,
        configEndpoint: 'file:./test/fixtures/example_configuration.json'
    })
}
exports.IntegrationTester = IntegrationTester;

IntegrationTester.prototype = {

    requestBuffer: [],
    sentMessages: {},
    requestId: 0,

    sendRequest: function (count, options) {
        if (typeof count === "undefined" || count === 0) {
            count = 1;
        }
        var lastMessage;
        while (count-- > 0) {
            lastMessage = new SentMessage(this, ++this.requestId, options);
        }
        return lastMessage;
    },

    serveRequests: function (howMany) {
        if (this.requestBuffer.length === 0) {
            throw "RequestBuffer empty, cannot serve any requests";
        }

        if (typeof howMany === "undefined" || howMany === 0) {
            howMany = this.requestBuffer.length;
        }

        var fromFirst = false;
        if (howMany < 0) {
            fromFirst = true;
            howMany *= -1;
        }

        if (howMany > this.requestBuffer.length) {
            throw "cannot serve more requests than the size of the request buffer (" + howMany + " > " + this.requestBuffer.length + ")";
        }

        var lastServedRequest;
        while (howMany-- > 0) {
            lastServedRequest = fromFirst ? this.requestBuffer.shift() : this.requestBuffer.pop();
            flushSingleRequest(HTTP_OK, lastServedRequest.req, lastServedRequest.res);
        }

        return new ServedRequestWrapper(lastServedRequest);
    },

    serveRequestWithStatusCode: function (statusCode) {
        if (this.requestBuffer.length === 0) {
            throw "RequestBuffer empty, cannot serve any requests";
        }

        var servedRequest = this.requestBuffer.pop();
        flushSingleRequest(statusCode, servedRequest.req, servedRequest.res);

        return new ServedRequestWrapper(servedRequest);
    },

    failRequest: function () {
        var lastServedRequest = this.requestBuffer.pop();
        var res = lastServedRequest.res;
        res.writeHead("invalid_status_code", {'Content-Type': 'text/plain'});
        res.end();
        return new FailedRequestWrapper(lastServedRequest);
    },

    reset: function (done) {
        this.requestId = 0;
        if (typeof done !== "function") {
            done = function () { /* noop */
            };
        }
        if (this.requestBuffer.length > 0) {
            this.serveRequests().onServed(done);
        } else {
            done();
        }
    },

    closeTestBackendServer: function (done) {
        this.testBackendServer.close(function () {
            done();
        });
    },

    pendingRequestsCount: function () {
        return this.requestBuffer.length;
    }
};

function flushSingleRequest(statusCode, request, response) {
    response.writeHead(statusCode, {'Content-Type': 'text/plain'});
    response.write("Hello ratelimiter!");
    response.end();
}

function getRequestId(request) {
    return request.headers[REQUEST_ID_HEADER.toLowerCase()];
}

function ServedRequestWrapper(servedRequest) {
    this.onServed = function (callback) {
        servedRequest.setOnServedCallback(callback);
{'fraction_non_alphanumeric': 0.11693842784132392, 'fraction_numerical': 0.009978096860550012, 'mean_word_length': 2.4708614864864864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '2634723', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2269, 'n_words': 477}