Skip to content

Commit

Permalink
Merge 2eaa76a into a7258b5
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Oct 23, 2015
2 parents a7258b5 + 2eaa76a commit 575ed31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ module.exports = etag;

/**
* Add ETag header field.
*
* @param {object} [options] see https://github.com/jshttp/etag#options
* @param {boolean} [options.weak]
* @return {Function}
* @api public
*/

function etag() {
function etag(options) {
return function *etag(next){
yield* next;

Expand All @@ -40,11 +41,11 @@ function etag() {
if (!body.path) return;
var s = yield fs.stat(body.path).catch(noop);
if (!s) return;
etag = calculate(s);
etag = calculate(s, options);
} else if (('string' == typeof body) || Buffer.isBuffer(body)) {
etag = calculate(body);
etag = calculate(body, options);
} else {
etag = calculate(JSON.stringify(body));
etag = calculate(JSON.stringify(body), options);
}

// add etag
Expand Down
30 changes: 23 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('etag()', function(){

request(app.listen())
.get('/')
.expect('ETag', '"sQqNsWTgdUEFt6mb5y4/5Q=="')
.expect('ETag', '"b-sQqNsWTgdUEFt6mb5y4/5Q"')
.end(done);
})
})
Expand All @@ -72,7 +72,7 @@ describe('etag()', function(){

request(app.listen())
.get('/')
.expect('ETag', '"sQqNsWTgdUEFt6mb5y4/5Q=="')
.expect('ETag', '"b-sQqNsWTgdUEFt6mb5y4/5Q"')
.end(done);
})
})
Expand All @@ -90,7 +90,7 @@ describe('etag()', function(){

request(app.listen())
.get('/')
.expect('ETag', '"m7WPJhkuS6APAeLnsTa72A=="')
.expect('ETag', '"d-m7WPJhkuS6APAeLnsTa72A"')
.end(done);
})
})
Expand All @@ -108,11 +108,27 @@ describe('etag()', function(){

request(app.listen())
.get('/')
.end(function(err, res){
if (err) return done(err);
res.should.have.header('ETag');
done();
.expect('ETag', 'W/"39b-150886b2270"')
.end(done);
})
})

describe('when with options', function(){
it('should add weak ETag', function(done){
var app = koa();
var options = {weak: true};

app.use(etag(options));

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

request(app.listen())
.get('/')
.expect('ETag', 'W/"b-sQqNsWTgdUEFt6mb5y4/5Q"')
.end(done);
})
})
})

0 comments on commit 575ed31

Please sign in to comment.