tests: add more tests for app

parent f490e782
var http = require('http')
var connect = require('../')
describe('app', function(){
......@@ -45,9 +46,91 @@ describe('app', function(){
app.stack.should.have.length(3);
})
it('should work as middlware', function(done){
var http = require('http');
it('should work in http.createServer', function(done){
var app = connect();
app.use(function (req, res) {
res.end('hello, world!');
});
var server = http.createServer(app).listen(5556, function(){
http.get({
host: 'localhost',
port: 5556,
path: '/'
}, function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
buf.should.eql('hello, world!');
server.close(done);
});
});
});
})
it('should be a callable function', function(done){
var app = connect();
app.use(function (req, res) {
res.end('hello, world!');
});
function handler(req, res) {
res.write('oh, ');
app(req, res);
}
var server = http.createServer(handler).listen(5557, function(){
http.get({
host: 'localhost',
port: 5557,
path: '/'
}, function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
buf.should.eql('oh, hello, world!');
server.close(done);
});
});
});
})
it('should invoke callback if request not handled', function(done){
var app = connect();
app.use('/foo', function (req, res) {
res.end('hello, world!');
});
function handler(req, res) {
res.write('oh, ');
app(req, res, function() {
res.end('no!');
});
}
var server = http.createServer(handler).listen(5558, function(){
http.get({
host: 'localhost',
port: 5558,
path: '/'
}, function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
buf.should.eql('oh, no!');
server.close(done);
});
});
});
})
it('should work as middlware', function(done){
// custom server handler array
var handlers = [connect(), function(req, res, next){
res.writeHead(200, {'Content-Type': 'text/plain'});
......@@ -65,10 +148,10 @@ describe('app', function(){
}
// create a non-connect server
var server = http.createServer(run).listen(5556, function(){
var server = http.createServer(run).listen(5559, function(){
http.get({
host: 'localhost',
port: 5556,
port: 5559,
path: '/'
}, function(res){
var buf = '';
......@@ -76,8 +159,7 @@ describe('app', function(){
res.on('data', function(s){ buf += s });
res.on('end', function(){
buf.should.eql('Ok');
server.close();
done();
server.close(done);
});
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment