<commit_before>var http = require('http');
var test = require('tape');
var request = require('request');
var requestIp = require('../index.js');

// Setup local server for testing
var serverInfo = {
    host: '127.0.0.1'
};

function serverFactory() {
    return http.createServer(function(req, res) {
        // res.writeHead(200, {'Content-Type': 'text/plain'});
        var ip = requestIp.getClientIp(req);
        res.end(ip);
    });
}

test('x-client-ip', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-client-ip': '172.16.58.3'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    // node listens on a random port when using 0
    // http://stackoverflow.com/questions/9901043/how-does-node-js-choose-random-ports
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        // we can't make the request URL until we get the port number from the new server
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            t.equal(options.headers['x-client-ip'], body);
            server.close();
        }
    }
});

test('x-forwarded-for', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-forwarded-for': '192.168.127.12, 192.168.127.12, 172.16.58.3'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            var firstIp = options.headers['x-forwarded-for'].split(',')[0];
            t.equal(firstIp, body);
            server.close();
        }
    }
});

test('x-real-ip', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-real-ip': '192.168.127.12'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            t.equal(options.headers['x-real-ip'], body);
            server.close();
        }
    }
});
<commit_msg>fix the x-forwarded-for tests<commit_after>var http = require('http');
var test = require('tape');
var request = require('request');
var requestIp = require('../index.js');

// Setup local server for testing
var serverInfo = {
    host: '127.0.0.1'
};

function serverFactory() {
    return http.createServer(function(req, res) {
        // res.writeHead(200, {'Content-Type': 'text/plain'});
        var ip = requestIp.getClientIp(req);
        res.end(ip);
    });
}

test('x-client-ip', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-client-ip': '172.16.58.3'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    // node listens on a random port when using 0
    // http://stackoverflow.com/questions/9901043/how-does-node-js-choose-random-ports
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        // we can't make the request URL until we get the port number from the new server
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            t.equal(options.headers['x-client-ip'], body);
            server.close();
        }
    }
});

test('x-forwarded-for', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-forwarded-for': '192.168.127.12, 192.168.127.12, 172.16.58.3'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            var firstIp = options.headers['x-forwarded-for'].split(',')[0];
            t.equal(firstIp, body);
            server.close();
        }
    }
});

test('x-real-ip', function(t) {
    t.plan(1);
    var options = {
        url: '',
        headers: {
            'x-real-ip': '192.168.127.12'
        }
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            t.equal(options.headers['x-real-ip'], body);
            server.close();
        }
    }
});

test('req.connection.remoteAddress', function(t) {
    t.plan(1);
    var options = {
        url: ''
    };
    // create new server for each test so we can easily close it after the test is done
    // prevents tests from hanging and competing against closing a global server
    var server = new serverFactory();
    server.listen(0, serverInfo.host);
    server.on('listening', function() {
        options.url = 'http://' + serverInfo.host + ':' + server.address().port;;
        request(options, callback);
    });

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            // make sure response ip is the same as the one we passed in
            // t.equal(options.headers['x-real-ip'], body);
            console.log(body);
            server.close();
        }
    }
});

{'fraction_non_alphanumeric': 0.14323642639054826, 'fraction_numerical': 0.022434621000929245, 'mean_word_length': 2.2502157031924073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, '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': '6045816', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2161, 'n_words': 654}