<commit_before>var path = require('path');
var http = require('http');
var connect = require('connect');
var expect = require('expect.js');
var fakeredis = require('fakeredis');
var sinon = require('sinon');
var Server = require('../../lib/server/superstatic_server');
var ConfigFile = require('../../lib/server/config/file');
var ConfigRedis = require('../../lib/server/config/redis');
var StoreLocal = require('../../lib/server/store/local');
var StoreS3 = require('../../lib/server/store/s3');
var middleware = require('../../lib/server/middleware');

var PORT = 4000;
var HOST = '127.0.0.1';
var CWD = path.join(process.cwd(), 'test/fixtures/sample_app');

describe('Superstatic server', function() {
  beforeEach(function () {
    this.server = localServer();
  });
  
  it('sets the settings object on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the store object on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the routes list on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the port on the instancje', function () {
    expect(this.server._port).to.be(PORT);
  });
  
  describe('#createServer()', function() {
    it('create and insatnce of SuperstaticServer', function () {
      expect(this.server instanceof Server).to.be(true);
    });
    
    describe('local options', function() {
      beforeEach(function () {
        this.server = localServer();
      });
      
      
      it('configures the settings object as a file', function () {
        expect(this.server.settings instanceof ConfigFile).to.be(true);
      });
      
      it('configures the file store as a file system store', function () {
        expect(this.server.store instanceof StoreLocal).to.be(true);
      });
    })
    
    describe('remote options', function() {
      beforeEach(function () {
        this.server = remoteServer();
      });
      
      it('configures the settings object as a redis client', function () {
        expect(this.server.settings instanceof ConfigRedis).to.be(true);
      });
      
      it('configures the file store as an s3 bucket', function () {
        expect(this.server.store instanceof StoreS3).to.be(true);
      });
    })
  });

  it('starts the server', function (done) {
    var self = this;
    
    startServer(this.server, function (finished) {
      expect(self.server._server).to.not.be(undefined);
      finished(done);
    });
  });
  
  it('tracks all open connectionss', function (done) {
    var self = this;
    
    startServer(this.server, function (finished) {
      self.server._openServer.on('connection', function () {
        expect(self.server._openServer._connects).to.not.eql({});
      });
      
      httpGet('/', function (data) {
        finished(done);
      });
    });
  });
  
  it('closes all connections when server is stopped', function (done) {
    var self = this;
    var destroySpy = sinon.spy();
    
    startServer(this.server, function (finsished) {
      self.server._openServer._connects.someConnection = {
        destroy: destroySpy
      };
      self.server.stop(function () {
        expect(destroySpy.called).to.be(true);
        done();
      });
    });
  });
  
  it('removes all listensers when server is stopped', function (done) {
    var self = this;
    var removeSpy = sinon.spy();
    
    
    startServer(this.server, function (finsished) {
      self.server._openServer.removeAllListeners = removeSpy;
      self.server.stop(function () {
        expect(removeSpy.called).to.be(true);
        done();
      });
    });
  });
  
  it('adds a route to the server', function () {
    var routeDef = {
      path: '/route',
      method: 'GET',
      handler: function (req, res) {
        res.end();
      }
    };
    this.server.route(routeDef);
    expect(this.server.routes).to.eql([routeDef]);
  });
  
  describe.only('middleware', function() {
    beforeEach(function () {
      this._connectServer = this.server._createServer();
      this.stack = this._connectServer.stack;
    });
    
    it('uses the logger middleware', function () {
      expect(this.stack[0].handle.toString()).to.equal(connect.logger('short').toString());
    });
    
    // Can't get to pass
    it.skip('uses the query middleware', function () {
      expect(this.stack[1].handle.prototype).to.eql(connect.query.prototype);
    });
    
    it('uses the superstatic router middleware', function () {
      var routerUsed = this.stack[2].handle.toString();
      var routerTest = middleware.router(this.server.settings, this.server.store, this.server.routes).toString();
      
      expect(routerUsed).to.eql(routerTest);
    });
  });
});


function startServer (server, callback) {
  server.start(function () {
    callback(function (done) {
      server.stop(done);
    });
  });
}

function httpGet(path, callback) {<commit_msg>Full test coverage for superstatic server<commit_after>var path = require('path');
var http = require('http');
var connect = require('connect');
var expect = require('expect.js');
var fakeredis = require('fakeredis');
var sinon = require('sinon');
var Server = require('../../lib/server/superstatic_server');
var ConfigFile = require('../../lib/server/config/file');
var ConfigRedis = require('../../lib/server/config/redis');
var StoreLocal = require('../../lib/server/store/local');
var StoreS3 = require('../../lib/server/store/s3');
var middleware = require('../../lib/server/middleware');

var PORT = 4000;
var HOST = '127.0.0.1';
var CWD = path.join(process.cwd(), 'test/fixtures/sample_app');

describe('Superstatic server', function() {
  beforeEach(function () {
    this.server = localServer();
  });
  
  it('sets the settings object on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the store object on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the routes list on the instance', function () {
    expect(this.server.settings).to.not.be(undefined);
  });
  
  it('sets the port on the instancje', function () {
    expect(this.server._port).to.be(PORT);
  });
  
  describe('#createServer()', function() {
    it('create and insatnce of SuperstaticServer', function () {
      expect(this.server instanceof Server).to.be(true);
    });
    
    describe('local options', function() {
      beforeEach(function () {
        this.server = localServer();
      });
      
      
      it('configures the settings object as a file', function () {
        expect(this.server.settings instanceof ConfigFile).to.be(true);
      });
      
      it('configures the file store as a file system store', function () {
        expect(this.server.store instanceof StoreLocal).to.be(true);
      });
    })
    
    describe('remote options', function() {
      beforeEach(function () {
        this.server = remoteServer();
      });
      
      it('configures the settings object as a redis client', function () {
        expect(this.server.settings instanceof ConfigRedis).to.be(true);
      });
      
      it('configures the file store as an s3 bucket', function () {
        expect(this.server.store instanceof StoreS3).to.be(true);
      });
    })
  });

  it('starts the server', function (done) {
    var self = this;
    
    startServer(this.server, function (finished) {
      expect(self.server._server).to.not.be(undefined);
      finished(done);
    });
  });
  
  it('tracks all open connectionss', function (done) {
    var self = this;
    
    startServer(this.server, function (finished) {
      self.server._openServer.on('connection', function () {
        expect(self.server._openServer._connects).to.not.eql({});
      });
      
      httpGet('/', function (data) {
        finished(done);
      });
    });
  });
  
  it('closes all connections when server is stopped', function (done) {
    var self = this;
    var destroySpy = sinon.spy();
    
    startServer(this.server, function (finsished) {
      self.server._openServer._connects.someConnection = {
        destroy: destroySpy
      };
      self.server.stop(function () {
        expect(destroySpy.called).to.be(true);
        done();
      });
    });
  });
  
  it('removes all listensers when server is stopped', function (done) {
    var self = this;
    var removeSpy = sinon.spy();
    
    
    startServer(this.server, function (finsished) {
      self.server._openServer.removeAllListeners = removeSpy;
      self.server.stop(function () {
        expect(removeSpy.called).to.be(true);
        done();
      });
    });
  });
  
  it('adds a route to the server', function () {
    var routeDef = {
      path: '/route',
      method: 'GET',
      handler: function (req, res) {
        res.end();
      }
    };
    this.server.route(routeDef);
    expect(this.server.routes).to.eql([routeDef]);
  });
  
  describe('middleware', function() {
    beforeEach(function () {
      this._connectServer = this.server._createServer();
      this.stack = this._connectServer.stack;
      
      this.stackHandleStr = function (idx) {
        return this.stack[idx].handle.toString();
      }
    });
    
    it('uses the logger middleware', function () {
      expect(this.stackHandleStr(0)).to.equal(connect.logger('short').toString());
    });
    
    it('uses the query middleware', function () {
      expect(this.stackHandleStr(1)).to.eql(connect.query().toString());
    });
    
    it('uses the superstatic router middleware', function () {
      var routerUsed = this.stack[2].handle.toString();
      var routerTest = middleware.router(this.server.settings, this.server.store, this.server.routes).toString();
      
      expect(routerUsed).to.eql(routerTest);
    });
    
    it('uses the restful middleware', function () {
      expect(this.stackHandleStr(3)).to.equal(middleware.restful().toString());
    });
    
    it('uses the settings cache middleware', function () {
      expect(this.stackHandleStr(4)).to.equal(middleware.settingsCache.toString());
    });
    
    it('uses the static middleware', function () {
      expect(this.stackHandleStr(5)).to.equal(middleware.static.toString());
    });
    
    it('uses the custom route middleware', function () {
      expect(this.stackHandleStr(6)).to.equal(middleware.customRoute.toString());
    });
    
    it('uses the directory index middleware', function () {
      expect(this.stackHandleStr(7)).to.equal(middleware.directoryIndex.toString());
    });
    
    it('uses the clean urls middleware', function () {
      expect(this.stackHandleStr(8)).to.equal(middleware.cleanUrls.toString());
    });
    
    it('uses the trailing slash remover middleware', function () {
      expect(this.stackHandleStr(9)).to.equal(middleware.removeTrailingSlash.toString());
    });
    
    it('uses the connect gzip middleware', function () {
      expect(this.stackHandleStr(10)).to.equal(connect.compress().toString());
    });
    
    it('uses the not found middleware', function () {
      expect(this.stackHandleStr(11)).to.equal(middleware.notFound.toString());
    });
    
    it('uses the responder middleware', function () {
      expect(this.stackHandleStr(12)).to.equal(middleware.responder.toString());
    });
  });
});



function startServer (server, callback) {
  server.start(function () {
    callback(function (done) {
      server.stop(done);
    });
  });
}

function httpGet(path, callback) {
{'fraction_non_alphanumeric': 0.16148800276195407, 'fraction_numerical': 0.004056620058691524, 'mean_word_length': 2.8739551989301235, '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': '6119770', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3435, 'n_words': 686}