Skip to content

Commit

Permalink
Updated downloads example with acceptance tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
slaskis authored and tj committed Feb 18, 2012
1 parent ac387ca commit 7ea7a53
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
14 changes: 9 additions & 5 deletions examples/downloads/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

var express = require('../../')
, app = express();
, app = module.exports = express();

app.get('/', function(req, res){
res.send('<ul>'
Expand All @@ -28,15 +28,19 @@ app.get('/files/:file(*)', function(req, res, next){
// will respond with 500 "Internal Server Error".
app.use(function(err, req, res, next){
// log all errors
console.error(err.stack);

if ('test' != process.env.NODE_ENV)
console.error(err.stack);

// special-case 404s
if (404 == err.status) {
res.statusCode = 404;
res.send('Cant find that file, sorry!');
} else {
next(err);
}
});

app.listen(3000);
console.log('Express started on port 3000');
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}
32 changes: 32 additions & 0 deletions test/acceptance/downloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var app = require('../../examples/downloads/app')
, request = require('../support/http');

describe('downloads', function(){
describe('GET /', function(){
it('should have a link to amazing.txt', function(done){
request(app)
.get('/')
.expect(/href="\/files\/amazing.txt"/,done)
})
})

describe('GET /files/amazing.txt', function(){
it('should have a download header', function(done){
request(app)
.get('/files/amazing.txt')
.end(function(res){
res.should.have.property('statusCode',200)
res.headers.should.have.property('content-disposition', 'attachment; filename="amazing.txt"')
done()
})
})
})

describe('GET /files/missing.txt', function(){
it('should respond with 404', function(done){
request(app)
.get('/files/missing.txt')
.expect(404,done)
})
})
})

0 comments on commit 7ea7a53

Please sign in to comment.