{"size": 1851, "ext": "js", "max_stars_repo_path": "test/backend/language-tests.js", "max_stars_repo_name": "coolwhite520/firefoxsend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/backend/language-tests.js", "max_issues_repo_name": "coolwhite520/firefoxsend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/backend/language-tests.js", "max_forks_repo_name": "coolwhite520/firefoxsend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.2205882353, "max_line_length": 80, "alphanum_fraction": 0.6337115073}
const assert = require('assert');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();

const config = {
  l10n_dev: false // prod configuration
};
const pkg = {
  availableLanguages: ['en-US', 'fr', 'it', 'es-ES']
};

function request(acceptLang) {
  return {
    headers: {
      'accept-language': acceptLang
    }
  };
}

const langMiddleware = proxyquire('../../server/middleware/language', {
  '../config': config,
  '../../package.json': pkg
});

describe('Language Middleware', function() {
  it('defaults to en-US when no header is present', function() {
    const req = request();
    const next = sinon.stub();
    langMiddleware(req, null, next);
    assert.equal(req.language, 'en-US');
    sinon.assert.calledOnce(next);
  });

  it('sets req.language to en-US when accept-language > 255 chars', function() {
    const accept = Array(257).join('a');
    assert.equal(accept.length, 256);
    const req = request(accept);
    const next = sinon.stub();
    langMiddleware(req, null, next);
    assert.equal(req.language, 'en-US');
    sinon.assert.calledOnce(next);
  });

  it('defaults to en-US when no accept-language is available', function() {
    const req = request('fa,cs,ja');
    const next = sinon.stub();
    langMiddleware(req, null, next);
    assert.equal(req.language, 'en-US');
    sinon.assert.calledOnce(next);
  });

  it('prefers higher q values', function() {
    const req = request('fa;q=0.5, it;q=0.9');
    const next = sinon.stub();
    langMiddleware(req, null, next);
    assert.equal(req.language, 'it');
    sinon.assert.calledOnce(next);
  });

  it('uses likely subtags', function() {
    const req = request('es-MX');
    const next = sinon.stub();
    langMiddleware(req, null, next);
    assert.equal(req.language, 'es-ES');
    sinon.assert.calledOn(next);
  });
});
