Skip to content

Commit

Permalink
res: add .is()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Jun 13, 2014
1 parent 696d55d commit 31ba115
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
3 changes: 1 addition & 2 deletions docs/api/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ this.body = yield db.find('something');
If `app.subdomainOffset` is not set, this.subdomains is `["ferrets", "tobi"]`.
If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`.

### req.is(type)
### req.is(types...)

Check if the incoming request contains the "Content-Type"
header field, and it contains any of the give mime `type`s.
Expand Down Expand Up @@ -346,4 +346,3 @@ this.acceptsLanguages();
### req.get(field)

Return request header.

29 changes: 28 additions & 1 deletion docs/api/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

### res.status=

Set response status via numeric code or case-insensitive string:
Set response status via numeric code:

- 100 "continue"
- 101 "switching protocols"
Expand Down Expand Up @@ -182,6 +182,33 @@ this.type = 'png';
when explicitly defined in full as `res.type = 'text/html'`
no charset is assigned.

### res.is(types...)

Very similar to `this.request.is()`.
Check whether the response type is one of the supplied types.
This is particularly useful for creating middleware that
change certain responses.

For example, this is a middleware that minifies
all HTML response except for streamss.

```js
var minify = require('html-minifier');

app.use(function *minifyHTML(next){
yield* next;

if (!this.response.is('html')) return;

var body = this.response.body;
if (!body) return;
// too difficult to do this with a stream
if ('function' == typeof body.pipe) return;
if (Buffer.isBuffer(body)) body = body.toString('utf8');
this.response.body = minify(body);
});
```

### res.redirect(url, [alt])

Perform a [302] redirect to `url`.
Expand Down
17 changes: 17 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var getType = require('mime-types').contentType;
var isJSON = require('koa-is-json');
var escape = require('escape-html');
var onfinish = require('finished');
var typeis = require('type-is').is;
var status = require('statuses');
var destroy = require('dethroy');
var assert = require('assert');
Expand Down Expand Up @@ -338,6 +339,22 @@ module.exports = {
return type.split(';')[0];
},

/**
* Check whether the response is one of the listed types.
* Pretty much the same as `this.request.is()`.
*
* @param {String|Array} types...
* @return {String|false}
* @api public
*/

is: function(types){
var type = this.type;
if (!types) return type || false;
if (!Array.isArray(types)) types = [].slice.call(arguments);
return typeis(type, types);
},

/**
* Return response header.
*
Expand Down
80 changes: 80 additions & 0 deletions test/response/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

var context = require('../context');
var should = require('should');
var assert = require('assert');

describe('response.is(type)', function(){
it('should ignore params', function(){
var res = context().response;
res.type = 'text/html; charset=utf-8';

res.is('text/*').should.equal('text/html');
})

describe('when no type is set', function(){
it('should return false', function(){
var res = context().response;

assert(false === res.is());
assert(false === res.is('html'));
})
})

describe('when given no types', function(){
it('should return the type', function(){
var res = context().response;
res.type = 'text/html; charset=utf-8';

res.is().should.equal('text/html');
})
})

describe('given one type', function(){
it('should return the type or false', function(){
var res = context().response;
res.type = 'image/png';

res.is('png').should.equal('png');
res.is('.png').should.equal('.png');
res.is('image/png').should.equal('image/png');
res.is('image/*').should.equal('image/png');
res.is('*/png').should.equal('image/png');

res.is('jpeg').should.be.false;
res.is('.jpeg').should.be.false;
res.is('image/jpeg').should.be.false;
res.is('text/*').should.be.false;
res.is('*/jpeg').should.be.false;
})
})

describe('given multiple types', function(){
it('should return the first match or false', function(){
var res = context().response;
res.type = 'image/png';

res.is('png').should.equal('png');
res.is('.png').should.equal('.png');
res.is('text/*', 'image/*').should.equal('image/png');
res.is('image/*', 'text/*').should.equal('image/png');
res.is('image/*', 'image/png').should.equal('image/png');
res.is('image/png', 'image/*').should.equal('image/png');

res.is('jpeg').should.be.false;
res.is('.jpeg').should.be.false;
res.is('text/*', 'application/*').should.be.false;
res.is('text/html', 'text/plain', 'application/json; charset=utf-8').should.be.false;
})
})

describe('when Content-Type: application/x-www-form-urlencoded', function(){
it('should match "urlencoded"', function(){
var res = context().response;
res.type = 'application/x-www-form-urlencoded';

res.is('urlencoded').should.equal('urlencoded');
res.is('json', 'urlencoded').should.equal('urlencoded');
res.is('urlencoded', 'json').should.equal('urlencoded');
})
})
})

0 comments on commit 31ba115

Please sign in to comment.