var Backend = require('../lib/backend');
var expect = require('expect.js');
var types = require('../lib/types');

describe('middleware', function() {

  beforeEach(function() {
    this.backend = new Backend();
  });

  describe('connect', function() {

    it('passes the agent on connect', function() {
      var clientId;
      this.backend.use('connect', function(request, next) {
        clientId = request.agent.clientId;
        next();
      });

      var connection = this.backend.connect();
      expect(connection.id).equal(null);
      connection.on('connected', function() {
        expect(connection.id).equal(clientId);
        done();
      });
    });

    it('passing an error on connect stops the client', function(done) {
      this.backend.use('connect', function(request, next) {
        next({message: 'No good'});
      });

      var connection = this.backend.connect();
      connection.on('stopped', function() {
        done();
      });
    });

  });

  describe('submit', function() {

    it('gets options passed to backend.submit', function(done) {
      this.backend.use('submit', function(request, next) {
        expect(request.options).eql({testOption: true});
        done();
      });
      var op = {create: {type: types.defaultType.uri}};
      var options = {testOption: true};
      this.backend.submit(null, 'dogs', 'fido', op, options, function(err) {
        if (err) throw err;
      });
    });

  });

});

@@ -203,32 +203,257 @@
 ckend();%0A  %7D);%0A%0A
+  describe('use', function() %7B%0A%0A    it('returns itself to allow chaining', function() %7B%0A      var response = this.backend.use('submit', function(request, next) %7B%7D);%0A      expect(response).equal(this.backend);%0A    %7D);%0A%0A  %7D);%0A%0A
   describe('conn

Add test for chaining