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);
}

@@ -545,34 +545,44 @@
 ong.example.com'
-, 
+)%0A    .then(
 function(res) %7B%0A
@@ -881,32 +881,33 @@
      %7D);%0A%0A    %7D)
+;
 %0A%0A  %7D);%0A%0A  it('r
@@ -1120,18 +1120,28 @@
 ple.com'
-, 
+)%0A    .then(
 function
@@ -1245,16 +1245,17 @@
 %0A%0A    %7D)
+;
 %0A%0A  %7D);%0A
@@ -1259,51 +1259,1131 @@
 );%0A%0A
-%7D);%0A%0Afunction request(hostname, callback) %7B
+  it('can be used with path prefixes', function(done) %7B%0A%0A    app.get('/baz', function(req, res) %7B%0A      res.sendStatus(200);%0A      res.end();%0A    %7D);%0A%0A    app.use('/foo', forceDomain('example.com'));%0A    app.get('/foo/bar', function(req, res) %7B%0A      res.sendStatus(200);%0A      res.end();%0A    %7D);%0A%0A    request('example.com', '/foo/bar')%0A    .then(function(res) %7B%0A      expect(res).to.exist;%0A      expect(res.statusCode).to.be.equal(200);%0A%0A      return request('example.com', '/baz');%0A    %7D).then(function(res) %7B%0A      expect(res).to.exist;%0A      expect(res.statusCode).to.be.equal(200);%0A%0A      return request('wrong.example.com', '/foo/bar');%0A    %7D)%0A    .then(function(res) %7B%0A      expect(res).to.exist;%0A      expect(res.statusCode).to.be.equal(301);%0A%0A      return request('wrong.example.com', '/baz');%0A    %7D)%0A    .then(function(res) %7B%0A      expect(res).to.exist;%0A      expect(res.statusCode).to.be.equal(200);%0A      done();%0A    %7D);%0A%0A  %7D);%0A%0A%7D);%0A%0Afunction request(hostname, path, callback) %7B%0A%0A  path = path %7C%7C '/';%0A%0A  var q = new Promise(function(resolve, reject) %7B%0A    if(!callback) %7B%0A      callback = resolve;%0A    %7D%0A  %7D);%0A
 %0A  h
@@ -2441,16 +2441,32 @@
 0.0.1',%0A
+    path: path,%0A
     head
@@ -2515,10 +2515,24 @@
 lback);%0A
+%0A  return q;%0A%0A
 %7D%0A

test using path prefixes