<commit_before>var fs = require('fs');
var path = require('path');
var should = require('should');
var request = require('supertest');

var indexStatic = require('./index-static-singleton');
var app = require('../app');

describe('app', function() {
  beforeEach(indexStatic.build);
  beforeEach(function() {
    app.updateIndexStatic(indexStatic.get());
  });

  it('reports when server-side bundle is regenerating', function(done) {
    app.updateIndexStatic(null);

    request(app)
      .get('/')
      .expect(200)
      .expect('Please wait while the server-side bundle regenerates.')
      .end(done);
  });

  it('returns 200 at all public HTML pages', function(done) {
    this.timeout(20000);

    var redirects = Object.keys(indexStatic.get().REDIRECTS);
    var urls = indexStatic.get().URLS.filter(function(route) {
      return redirects.indexOf(route) === -1;
    }).map(function(url) {
      if (url === '/') return '/';
      return '/' + url + '/';
    });

    function nextRequest(lastErr) {
      if (lastErr) {
        return done(lastErr);
      }
      if (urls.length === 0) {
        return done();
      }

      var url = urls.pop();
      request(app)
      .get(url)
      .expect(200)
      .expect('Content-Type', /html/)
      .expect(/\<title\>/)
      .end(nextRequest);
    }

    urls.length.should.not.equal(0);
    nextRequest();
  });

  it('serves static files', function(done) {
    this.timeout(10000);

    var filename = path.join(app.DIST_DIR, 'hi.txt');
    var str = 'hi ' + Date.now();

    fs.writeFileSync(filename, str);
    request(app)
      .get('/hi.txt')
      .expect(200)
      .expect(str)
      .end(function(err) {
        fs.unlinkSync(filename);
        done(err);
      });
  });

  it('redirects old routes to new ones', function(done) {
    request(app)
      .get('/clubs/curriculum/')
      .expect('Location', '/activities/web-lit-basics/')
      .expect(302)
      .end(done);
  });

  it('redirects to a locale', function(done) {
    request(app)
      .get('/foobar')
      .expect(function(res) {
       if(res.headers.location !== "/en-US/foobar") throw new Error("Doesn't redirect to locale");
      })
      .end(done);
  });

  it('preserves query params when forwarding to a locale', function(done) {
    request(app)
      .get('/foobar/?key=value&key2=value2')
      .expect(function(res) {
       if(res.headers.location !== "/en-US/foobar/?key=value&key2=value2") throw new Error("Doesn't preserve query params");
      })
      .end(done);
  });

  it('redirects to a 404', function(done) {
    request(app)
      .get('/asdfasdfasdf')
      .redirects(1)
      .expect(404)
      .end(done);
  });

  it('reports 404s without redirecting when given a locale', function(done) {
    this.timeout(10000);
    request(app)
      .get('/en-US/asdfasdfasdf')<commit_msg>Updating test to add accept-language header<commit_after>var fs = require('fs');
var path = require('path');
var should = require('should');
var request = require('supertest');

var indexStatic = require('./index-static-singleton');
var app = require('../app');

describe('app', function() {
  beforeEach(indexStatic.build);
  beforeEach(function() {
    app.updateIndexStatic(indexStatic.get());
  });

  it('reports when server-side bundle is regenerating', function(done) {
    app.updateIndexStatic(null);

    request(app)
      .get('/')
      .expect(200)
      .expect('Please wait while the server-side bundle regenerates.')
      .end(done);
  });

  it('returns 200 at all public HTML pages', function(done) {
    this.timeout(30000);

    var redirects = Object.keys(indexStatic.get().REDIRECTS);
    var urls = indexStatic.get().URLS.filter(function(route) {
      return redirects.indexOf(route) === -1;
    }).map(function(url) {
      if (url === '/') return '/';
      return '/' + url + '/';
    });

    function nextRequest(lastErr) {
      if (lastErr) {
        return done(lastErr);
      }
      if (urls.length === 0) {
        return done();
      }

      var url = urls.pop();
      request(app)
      .get(url)
      .expect(200)
      .expect('Content-Type', /html/)
      .expect(/\<title\>/)
      .end(nextRequest);
    }

    urls.length.should.not.equal(0);
    nextRequest();
  });

  it('serves static files', function(done) {
    this.timeout(10000);

    var filename = path.join(app.DIST_DIR, 'hi.txt');
    var str = 'hi ' + Date.now();

    fs.writeFileSync(filename, str);
    request(app)
      .get('/hi.txt')
      .expect(200)
      .expect(str)
      .end(function(err) {
        fs.unlinkSync(filename);
        done(err);
      });
  });

  it('redirects old routes to new ones', function(done) {
    request(app)
      .get('/clubs/curriculum/')
      .expect('Location', '/activities/web-lit-basics/')
      .expect(302)
      .end(done);
  });

  it('redirects to a locale', function(done) {
    request(app)
      .get('/foobar')
      .set('Accept-Language', 'en-US')
      .expect(function(res) {
       if(res.headers.location !== "/en-US/foobar") throw new Error("Doesn't redirect to locale");
      })
      .end(done);
  });

  it('preserves query params when forwarding to a locale', function(done) {
    request(app)
      .get('/foobar/?key=value&key2=value2')
      .set('Accept-Language', 'en-US')
      .expect(function(res) {
       if(res.headers.location !== "/en-US/foobar/?key=value&key2=value2") throw new Error("Doesn't preserve query params");
      })
      .end(done);
  });

  it('redirects to a 404', function(done) {
    request(app)
      .get('/asdfasdfasdf')
      .redirects(1)
      .expect(404)
      .end(done);
  });

  it('reports 404s without redirecting when given a locale', function(done) {
    this.timeout(10000);
    request(app)
      .get('/en-US/asdfasdfasdf')
{'fraction_non_alphanumeric': 0.17820247933884298, 'fraction_numerical': 0.01618457300275482, 'mean_word_length': 2.669614655716993, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4249049', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1886, 'n_words': 376}