<commit_before>/* istanbul ignore next */
describe('forwarded-for', function () {
  'use strict';

  var middleware = require('./middleware')
    , assume = require('assume')
    , parser = require('./');

  it('is exposed as function', function () {
    assume(parser).is.a('function');
  });

  it('exposes the proxy array', function () {
    assume(parser.proxies).to.be.a('array');
  });

  it('exposes the Forwarded class', function () {
    assume(parser.Forwarded).to.be.a('function');
  });

  it('sorts the proxy array and sets the last match as first item', function () {
    var last = parser.proxies[parser.proxies.length - 1]
      , spec = { ip: '172.16.17.32', port: '4900', proto: 'https' };

    assume(parser.proxies[0].ip).to.not.equal(last.ip);

    var forwarded = parser({
      remoteAddress: '127.1.2.0',
      remotePort: 490
    }, Object.keys(spec).reduce(function reduce(headers, key) {
      headers[last[key]] = spec[key];
      return headers;
    }, {}));

    assume(parser.proxies[0].ip).to.equal(last.ip);
    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(4900);
    assume(forwarded.secure).to.equal(true);
  });

  it('extracts the `remoteAdress` and port from the given object', function() {
    var forwarded = parser({
      remoteAddress: '172.16.17.32',
      remotePort: 490
    });

    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(490);
    assume(forwarded.secure).to.equal(false);
  });

  it('`encrypted` is used to check connection is secure', function () {
    var forwarded = parser({
      remoteAddress: '172.16.17.32',
      remotePort: 490,
      encrypted: true
    });

    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(490);
    assume(forwarded.secure).to.equal(true);
  });

  describe('x-forwarded-* headers', function () {
    it('prefer over the supplied object', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0',
        remotePort: 490
      }, {
        'x-forwarded-for': '192.168.3.11',
        'x-forwarded-port': '9093',
        'x-forwarded-proto': 'https'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
      assume(forwarded.port).to.equal(9093);
      assume(forwarded.secure).to.equal(true);
    });

    it('ensure header supports list with no spaces', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11,10.0.1.1'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
    });

    it('ensure header supports list with spaces around commas', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11, 10.0.1.1'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
    });

    it('ensure header is disqualified if invalid', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11,999.0.1.1'
      });

      assume(forwarded.ip).to.equal('127.1.2.0');
    });
  });

  it('works when shuffling the proxies array', function() {
    var i = 0, forwarded, hs = [
      {headers: {'fastly-client-ip': '1.2.3.4'}, expected: '1.2.3.4'},
      {headers: {'x-forwarded-for': '9.9.9.9'}, expected: '9.9.9.9'},
      {headers: {'forwarded': '3.4.5.6'}, expected: '3.4.5.6'},
      {headers: {'x-real-ip': '172.16.31.10'}, expected: '172.16.31.10'},
    ];

    // Fisher-Yates shuffle
    function shuffle(o){ //v1.0
        for (var j, x, i = o.length; i;
          j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
    }

    for (; i < 8; i++) {
      hs = shuffle(hs);
      hs.forEach(function(v) {
        forwarded = parser({}, v.headers);
        assume(forwarded.ip).to.equal(v.expected);
      });
    }
  });

  describe('fastly.com', function () {
    it('extracts information from fastly headers', function () {
      var forwarded = parser({}, {
        'fastly-client-ip': '1.2.3.4',
        'fastly-ssl': '1'
      });<commit_msg>[test] Add failing test case<commit_after>/* istanbul ignore next */
describe('forwarded-for', function () {
  'use strict';

  var middleware = require('./middleware')
    , assume = require('assume')
    , parser = require('./');

  it('is exposed as function', function () {
    assume(parser).is.a('function');
  });

  it('exposes the proxy array', function () {
    assume(parser.proxies).to.be.a('array');
  });

  it('exposes the Forwarded class', function () {
    assume(parser.Forwarded).to.be.a('function');
  });

  it('sorts the proxy array and sets the last match as first item', function () {
    var last = parser.proxies[parser.proxies.length - 1]
      , spec = { ip: '172.16.17.32', port: '4900', proto: 'https' };

    assume(parser.proxies[0].ip).to.not.equal(last.ip);

    var forwarded = parser({
      remoteAddress: '127.1.2.0',
      remotePort: 490
    }, Object.keys(spec).reduce(function reduce(headers, key) {
      headers[last[key]] = spec[key];
      return headers;
    }, {}));

    assume(parser.proxies[0].ip).to.equal(last.ip);
    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(4900);
    assume(forwarded.secure).to.equal(true);
  });

  it('extracts the `remoteAdress` and port from the given object', function() {
    var forwarded = parser({
      remoteAddress: '172.16.17.32',
      remotePort: 490
    });

    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(490);
    assume(forwarded.secure).to.equal(false);
  });

  it('`encrypted` is used to check connection is secure', function () {
    var forwarded = parser({
      remoteAddress: '172.16.17.32',
      remotePort: 490,
      encrypted: true
    });

    assume(forwarded.ip).to.equal('172.16.17.32');
    assume(forwarded.port).to.equal(490);
    assume(forwarded.secure).to.equal(true);
  });

  describe('x-forwarded-* headers', function () {
    it('prefer over the supplied object', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0',
        remotePort: 490
      }, {
        'x-forwarded-for': '192.168.3.11',
        'x-forwarded-port': '9093',
        'x-forwarded-proto': 'https'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
      assume(forwarded.port).to.equal(9093);
      assume(forwarded.secure).to.equal(true);
    });

    it('ensure header supports list with no spaces', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11,10.0.1.1'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
    });

    it('ensure header supports list with spaces around commas', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11, 10.0.1.1, 172.16.17.32'
      });

      assume(forwarded.ip).to.equal('192.168.3.11');
    });

    it('ensure header is disqualified if invalid', function () {
      var forwarded = parser({
        remoteAddress: '127.1.2.0'
      }, {
        'x-forwarded-for': '192.168.3.11,99192.168.3.11'
      });

      assume(forwarded.ip).to.equal('127.1.2.0');
    });
  });

  it('works when shuffling the proxies array', function() {
    var i = 0, forwarded, hs = [
      {headers: {'fastly-client-ip': '1.2.3.4'}, expected: '1.2.3.4'},
      {headers: {'x-forwarded-for': '9.9.9.9'}, expected: '9.9.9.9'},
      {headers: {'forwarded': '3.4.5.6'}, expected: '3.4.5.6'},
      {headers: {'x-real-ip': '172.16.31.10'}, expected: '172.16.31.10'}
    ];

    // Fisher-Yates shuffle
    function shuffle(o) { //v1.0
      for (var j, x, i = o.length; i;
        j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
      return o;
    }

    for (; i < 8; i++) {
      hs = shuffle(hs);
      hs.forEach(function(v) {
        forwarded = parser({}, v.headers);
        assume(forwarded.ip).to.equal(v.expected);
      });
    }
  });

  describe('fastly.com', function () {
    it('extracts information from fastly headers', function () {
      var forwarded = parser({}, {
        'fastly-client-ip': '1.2.3.4',
        'fastly-ssl': '1'
      });
{'fraction_non_alphanumeric': 0.19743773946360152, 'fraction_numerical': 0.06321839080459771, 'mean_word_length': 2.548428207306712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 63, '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': '4363471', 'n_tokens_mistral': 3239, 'n_tokens_neox': 2941, 'n_words': 549}