Skip to content

Commit

Permalink
add defer option
Browse files Browse the repository at this point in the history
closes #8 and closes #9
  • Loading branch information
jonathanong committed Dec 21, 2013
1 parent ab4387f commit 03c3188
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 87 deletions.
10 changes: 7 additions & 3 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.4.0 / 2013-12-20
==================

* add: defer option - semi-breaking change

1.3.0 / 2013-12-11
==================
Expand All @@ -6,17 +10,17 @@
* rename maxAge -> maxage
* fix: don't bother responding if response already "handled"

1.2.0 / 2013-09-14
1.2.0 / 2013-09-14
==================

* add Last-Modified. Closes #5

1.1.1 / 2013-09-13
1.1.1 / 2013-09-13
==================

* fix upstream responses

1.1.0 / 2013-09-13
1.1.0 / 2013-09-13
==================

* rewrite without send
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $ npm install koa-static
- `maxage` Browser cache max-age in milliseconds. defaults to 0
- `hidden` Allow transfer of hidden files. defaults to false
- `index` Default file name, defaults to 'index.html'
- `defer` If true, serves after `yield next`, allowing any downstream middleware to respond first.

## Example

Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function serve(root, opts) {
opts.root = root;
opts.index = opts.index || 'index.html';

if (!opts.defer) {
return function *(next){
if (this.idempotent && (yield send(this, this.path, opts))) return;
yield next;
}
}

return function *(next){
yield next;

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"license": "MIT",
"dependencies": {
"debug": "*",
"koa-send": "~1.0.0"
"koa-send": "~1.1.1"
},
"scripts": {
"test": "make test"
}
}
258 changes: 175 additions & 83 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,225 @@ var serve = require('..');
var koa = require('koa');

describe('serve(root)', function(){
describe('when upstream middleware responds', function(){
it('should do nothing', function(done){
var app = koa();
describe('when defer: false', function(){
describe('when path is not a file', function(){
it('should 404', function(done){
var app = koa();

app.use(serve('test/fixtures'));
app.use(serve('test/fixtures'));

app.use(function *(next){
yield next;
this.body = 'hey';
});
request(app.listen())
.get('/something')
.expect(404, done);
})
})

describe('when upstream middleware responds', function(){
it('should respond', function(done){
var app = koa();

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect('hey', done);
app.use(serve('test/fixtures'));

app.use(function *(next){
yield next;
this.body = 'hey';
});

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect('world', done);
})
})
})

describe('the path is valid', function(){
it('should serve the file', function(done){
var app = koa();
describe('the path is valid', function(){
it('should serve the file', function(done){
var app = koa();

app.use(serve('test/fixtures'));
app.use(serve('test/fixtures'));

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect('world', done);
request(app.listen())
.get('/hello.txt')
.expect(200)
.expect('world', done);
})
})

describe('.index', function(){
describe('when present', function(){
it('should alter the index file supported', function(done){
var app = koa();

app.use(serve('test/fixtures', { index: 'index.txt' }));

request(app.listen())
.get('/')
.expect(200)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('text index', done);
})
})

describe('when omitted', function(){
it('should use index.html', function(done){
var app = koa();

app.use(serve('test/fixtures'));

request(app.listen())
.get('/world/')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('html index', done);
})
})
})
})

describe('.index', function(){
describe('when present', function(){
it('should alter the index file supported', function(done){
describe('when defer: true', function(){
describe('when upstream middleware responds', function(){
it('should do nothing', function(done){
var app = koa();

app.use(serve('test/fixtures', { index: 'index.txt' }));
app.use(serve('test/fixtures', {
defer: true
}));

app.use(function *(next){
yield next;
this.body = 'hey';
});

request(app.listen())
.get('/')
.get('/hello.txt')
.expect(200)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('text index', done);
.expect('hey', done);
})
})

describe('when omitted', function(){
it('should use index.html', function(done){
describe('the path is valid', function(){
it('should serve the file', function(done){
var app = koa();

app.use(serve('test/fixtures'));
app.use(serve('test/fixtures', {
defer: true
}));

request(app.listen())
.get('/world/')
.get('/hello.txt')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('html index', done);
.expect('world', done);
})
})
})

// describe('when path is a directory', function(){
// describe('and an index file is present', function(){
// it('should redirect missing / to -> / when index is found', function(done){
// var app = koa();
describe('.index', function(){
describe('when present', function(){
it('should alter the index file supported', function(done){
var app = koa();

app.use(serve('test/fixtures', {
defer: true,
index: 'index.txt'
}));

request(app.listen())
.get('/')
.expect(200)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('text index', done);
})
})

describe('when omitted', function(){
it('should use index.html', function(done){
var app = koa();

// app.use(serve('test/fixtures'));
app.use(serve('test/fixtures', {
defer: true
}));

// request(app.listen())
// .get('/world')
// .expect(303)
// .expect('Location', '/world/', done);
// })
// })
request(app.listen())
.get('/world/')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('html index', done);
})
})
})

// describe('and no index file is present', function(){
// it('should not redirect', function(done){
// var app = koa();
// describe('when path is a directory', function(){
// describe('and an index file is present', function(){
// it('should redirect missing / to -> / when index is found', function(done){
// var app = koa();

// app.use(serve('test/fixtures'));
// app.use(serve('test/fixtures'));

// request(app.listen())
// .get('/')
// .expect(404, done);
// })
// })
// })
// request(app.listen())
// .get('/world')
// .expect(303)
// .expect('Location', '/world/', done);
// })
// })

describe('when path is not a file', function(){
it('should 404', function(done){
var app = koa();
// describe('and no index file is present', function(){
// it('should not redirect', function(done){
// var app = koa();

app.use(serve('test/fixtures'));
// app.use(serve('test/fixtures'));

request(app.listen())
.get('/something')
.expect(404, done);
})
})
// request(app.listen())
// .get('/')
// .expect(404, done);
// })
// })
// })

describe('it should not handle the request', function(){
it('when status=204', function(done){
var app = koa();
describe('when path is not a file', function(){
it('should 404', function(done){
var app = koa();

app.use(serve('test/fixtures'));
app.use(serve('test/fixtures', {
defer: true
}));

app.use(function *(next){
this.status = 204;
request(app.listen())
.get('/something')
.expect(404, done);
})

request(app.listen())
.get('/something%%%/')
.expect(204, done);
})

it('when body=""', function(done){
var app = koa();
describe('it should not handle the request', function(){
it('when status=204', function(done){
var app = koa();

app.use(serve('test/fixtures', {
defer: true
}));

app.use(serve('test/fixtures'));
app.use(function *(next){
this.status = 204;
})

app.use(function *(next){
this.body = '';
request(app.listen())
.get('/something%%%/')
.expect(204, done);
})

request(app.listen())
.get('/something%%%/')
.expect(200, done);
it('when body=""', function(done){
var app = koa();

app.use(serve('test/fixtures', {
defer: true
}));

app.use(function *(next){
this.body = '';
})

request(app.listen())
.get('/something%%%/')
.expect(200, done);
})
})
})
})

0 comments on commit 03c3188

Please sign in to comment.