Skip to content

Commit

Permalink
Added res.type (browser). Closes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 25, 2012
1 parent 0412097 commit 437277a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function Response(req, res, options) {
* - `.contentType` the content type without params
*
* A response of "Content-Type: text/plain; charset=utf-8"
* will provide you with a `.contentType` of "text/plain".
* will provide you with a `.type` of "text/plain".
*
* @param {Object} header
* @api private
Expand Down
17 changes: 16 additions & 1 deletion lib/superagent.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ var superagent = function(exports){
return fields;
}

/**
* Return the mime type for the given `str`.
*
* @param {String} str
* @return {String}
* @api private
*/

function type(str){
return str.split(/ *; */).shift();
};

/**
* Initialize a new `Response` with the given `xhr`.
*
Expand Down Expand Up @@ -265,13 +277,16 @@ var superagent = function(exports){
* - `.contentType` the content type without params
*
* A response of "Content-Type: text/plain; charset=utf-8"
* will provide you with a `.contentType` of "text/plain".
* will provide you with a `.type` of "text/plain".
*
* @param {Object} header
* @api private
*/

Response.prototype.setHeaderProperties = function(header){
var ct = this.header['content-type'] || '';
this.type = type(ct);

// TODO: moar!
var params = (this.header['content-type'] || '').split(/ *; */);
this.contentType = params.shift();
Expand Down
4 changes: 2 additions & 2 deletions test/test.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ test('POST multiple .send() calls', function(next){
});
});

test('GET .contentType', function(next){
test('GET .type', function(next){
request
.get('/pets')
.end(function(res){
assert('application/json' == res.contentType);
assert('application/json' == res.type);
next();
});
});
Expand Down

0 comments on commit 437277a

Please sign in to comment.