var crud = require('../');
var express = require('express');
var assert = require('chai').assert;
var http = require('http');
var request = require('superagent');

function makeapp () {
  var app = express();
  app.set('controller path', __dirname + '/controllers');
  return app;
}

describe('express-crud', function() {
  var app, server, address;
  before(function(done) {
    app = makeapp();
    server = http.Server(app);
    server.listen(0, function () {
      address = server.address();
      done();
    });
  });


  after(function(done) {
    server.close();
    done();
  });

  describe('.controllers', function() {
    it('should contain all the correct controllers', function() {
      app.set('controller path', __dirname + '/controllers/');
      crud(app,{verbose:false});
      assert.isNotNull(app.controllers, 'Crud adds controllers to applications');
      assert.isNotNull(app.controllers['test'], 'Crud adds controllers to applications');
      assert.isNotNull(app.controllers['subtest'], 'Crud adds controllers to applications');
      assert.isNotNull(app.controllers['subtest'].error, 'Crud adds error handler from controllers to applications');
      assert.isTrue(app.controllers['test'].set('jsonp callback'));
    });
  });

  describe('routes', function() {
    it('should route requests to correct routes', function(done) {
      var expected = {
        'GET /test':{"test:":"tests"},
        'POST /test':{"test:":"test created"},
        'PUT /test/1':{"test:":"test 1"},
        'GET /test/1':{"test:":"test 1"},
        'DEL /test/1':{"test:":"test 1 delete"},
        'GET /test/1/subtest/1': {"subtest:":"subtest 1"},
        'GET /test/1/subtest': {"error":'subtest-error'}
      };
      var numRoutes = Object.keys(expected).length;
      Object.keys(expected).forEach(function(key) {
        var parts = key.split(' ');
        var method = parts[0];
        var path = parts[1];
        var result = expected[key];
        var url = 'http://' + address.address + ':' + address.port + path;
        request[method.toLowerCase()](url, function(error, res) {
          res.req.method = res.req.method == 'DELETE' ? 'DEL' : res.req.method;
          assert.deepEqual(JSON.parse(res.text), expected[res.req.method + ' '+res.req.path]);
          --numRoutes || done();
        });
      });
    });
  });
});
@@ -1055,194 +1055,8 @@
 ');%0A
-      assert.isNotNull(app.controllers%5B'subtest'%5D.error, 'Crud adds error handler from controllers to applications');%0A      assert.isTrue(app.controllers%5B'test'%5D.set('jsonp callback'));%0A
     
@@ -2145,21 +2145,356 @@
 %7D);%0A    %7D);%0A
+    it('should set options to the controller app', function() %7B%0A      assert.isTrue(app.controllers%5B'test'%5D.set('jsonp callback'));%0A    %7D);%0A    it('should set the error handler to controller app', function() %7B%0A      assert.isNotNull(app.controllers%5B'subtest'%5D.error, 'Crud adds error handler from controllers to applications');%0A    %7D)%0A
   %7D);%0A%7D);

move error and object tests to own testblocks