
var connect = require('../');

describe('app', function(){
  it('should inherit from event emitter', function(done){
    var app = connect();
    app.on('foo', done);
    app.emit('foo');
  })

  it('should not obscure FQDNs', function(done){
    var app = connect();

    app.use(function(req, res){
      res.end(req.url);
    });

    app.request()
    .get('http://example.com/foo')
    .expect('http://example.com/foo', done);
  })

  it('should allow old-style constructor middleware', function(done){
    var app = connect(
        connect.json()
      , connect.multipart()
      , connect.urlencoded()
      , function(req, res){ res.end(JSON.stringify(req.body)) });

    app.stack.should.have.length(4);

    app.request()
      .post('/')
      .set('Content-Type', 'application/json')
      .write('{"foo":"bar"}')
      .expect('{"foo":"bar"}', done);
  })

  it('should allow old-style .createServer()', function(){
    var app = connect.createServer(
        connect.json()
      , connect.multipart()
      , connect.urlencoded());

    app.stack.should.have.length(3);
  })

  it('should escape the 404 response body', function(done){
    var app = connect();
    app.request()
    .get('/foo/<script>stuff</script>')
    .expect('Cannot GET /foo/&lt;script&gt;stuff&lt;/script&gt;', done);
  })
})

@@ -1079,32 +1079,1004 @@
 ength(3);%0A  %7D)%0A%0A
+  it('should work as middlware', function(done)%7B%0A    var http = require('http');%0A%0A    // custom server handler array%0A    var handlers = %5Bconnect(), function(req, res, next)%7B%0A      res.writeHead(200, %7B'Content-Type': 'text/plain'%7D);%0A      res.end('Ok');%0A    %7D%5D;%0A%0A    // execute handlers one after another%0A    var curHandler = 0;%0A    var execHandler = function(req, res)%7B%0A      if (handlers%5BcurHandler%5D)%0A        handlers%5BcurHandler++%5D(req, res, function()%7B%0A          execHandler(req, res);%0A        %7D);%0A    %7D%0A%0A    // create a non-connect server%0A    var server = http.createServer(execHandler).listen(5556, function()%7B%0A      // test it out%0A      http.get('http://localhost:5556/', function(res)%7B%0A        res.setEncoding('utf8');%0A        var data = '';%0A        res.on('data', function(chunk)%7B%0A          data += chunk;%0A        %7D);%0A        res.on('end', function()%7B%0A          data.should.eql('Ok');%0A          server.close();%0A          done();%0A        %7D);%0A      %7D);%0A    %7D);%0A  %7D)%0A%0A
   it('should esc

test connect as middleware