<commit_before>var http = require('http');
var express = require('express');
var expect = require('chai').expect;
var forceDomain = require('../lib/express-force-domain');

var serverPort = 4321;

describe('express-force-domain middleware', function() {

  var app, server;

  beforeEach(function() {
    app = express();
    server = app.listen(serverPort);
  });

  afterEach(function() {
    server.close();
  });

  it('responds with 301 when the requested domain does not match', function(done) {

    app.use(forceDomain('example.com'))

    request('wrong.example.com', function(res) {

      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(301);

      var body = ''
      res.on('data', function(chunk) {
        body += chunk;
      });
      res.on('end', function() {
        expect(res.headers.location).to.be.equal('http://example.com/');
        done();
      });

    })

  });

  it('responds with 200 when domain matches', function(done) {

    app.use(forceDomain('example.com'));
    app.get('/', function(req, res) {
      res.sendStatus(200);
      res.end();
    });

    request('example.com', function(res) {

      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(200);
      done();

    })

  });

});

function request(hostname, callback) {
  http.get({
    port: serverPort,
    hostname: '127.0.0.1',
    headers: {
      host: hostname
    }
  }, callback);
}<commit_msg>test using path prefixes<commit_after>var http = require('http');
var express = require('express');
var expect = require('chai').expect;
var forceDomain = require('../lib/express-force-domain');

var serverPort = 4321;

describe('express-force-domain middleware', function() {

  var app, server;

  beforeEach(function() {
    app = express();
    server = app.listen(serverPort);
  });

  afterEach(function() {
    server.close();
  });

  it('responds with 301 when the requested domain does not match', function(done) {

    app.use(forceDomain('example.com'))

    request('wrong.example.com')
    .then(function(res) {

      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(301);

      var body = ''
      res.on('data', function(chunk) {
        body += chunk;
      });
      res.on('end', function() {
        expect(res.headers.location).to.be.equal('http://example.com/');
        done();
      });

    });

  });

  it('responds with 200 when domain matches', function(done) {

    app.use(forceDomain('example.com'));
    app.get('/', function(req, res) {
      res.sendStatus(200);
      res.end();
    });

    request('example.com')
    .then(function(res) {

      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(200);
      done();

    });

  });

  it('can be used with path prefixes', function(done) {

    app.get('/baz', function(req, res) {
      res.sendStatus(200);
      res.end();
    });

    app.use('/foo', forceDomain('example.com'));
    app.get('/foo/bar', function(req, res) {
      res.sendStatus(200);
      res.end();
    });

    request('example.com', '/foo/bar')
    .then(function(res) {
      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(200);

      return request('example.com', '/baz');
    }).then(function(res) {
      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(200);

      return request('wrong.example.com', '/foo/bar');
    })
    .then(function(res) {
      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(301);

      return request('wrong.example.com', '/baz');
    })
    .then(function(res) {
      expect(res).to.exist;
      expect(res.statusCode).to.be.equal(200);
      done();
    });

  });

});

function request(hostname, path, callback) {

  path = path || '/';

  var q = new Promise(function(resolve, reject) {
    if(!callback) {
      callback = resolve;
    }
  });

  http.get({
    port: serverPort,
    hostname: '127.0.0.1',
    path: path,
    headers: {
      host: hostname
    }
  }, callback);

  return q;

}
{'fraction_non_alphanumeric': 0.18717244821562265, 'fraction_numerical': 0.01697030197154979, 'mean_word_length': 2.7111111111111112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6365500', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1290, 'n_words': 229}