Skip to content

Commit

Permalink
custom mime types support, unit cases (closes danwrong#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
disfated committed Jan 8, 2012
1 parent 60983d5 commit 6e55df7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
30 changes: 23 additions & 7 deletions lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,33 @@ function postJson(url, data, options) {
var parsers = {
auto: function(data, callback) {
var contentType = this.headers['content-type'];

var contentParser;
if (contentType) {
for (var matcher in parsers.auto.matchers) {

if (contentType.indexOf(matcher) == 0) {
return parsers.auto.matchers[matcher].call(this, data, callback);
contentType = contentType.replace(/;.+/, ''); // remove all except mime type (eg. text/html; charset=UTF-8)
if (contentType in parsers.auto.matchers) {
contentParser = parsers.auto.matchers[contentType];
} else {
// custom (vendor) mime types
var parts = contentType.match(/^([\w-]+)\/vnd((?:\.(?:[\w-]+))+)\+([\w-]+)$/i);
if (parts) {
var type = parts[1];
var vendors = parts[2].substr(1).split('.');
var subtype = parts[3];
var vendorType;
while (vendors.pop() && !(vendorType in parsers.auto.matchers)) {
vendorType = vendors.length
? type + '/vnd.' + vendors.join('.') + '+' + subtype
: vendorType = type + '/' + subtype;
}
contentParser = parsers.auto.matchers[vendorType];
}
}
}

callback(null, data);
if (typeof contentParser == 'function') {
contentParser.call(this, data, callback);
} else {
callback(null, data);
}
},
json: function(data, callback) {
if (data && data.length) {
Expand Down
24 changes: 23 additions & 1 deletion test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,17 @@ function dataResponse(request, response) {
});
request.addListener('end', function() {
response.writeHead(200, {
'content-type': 'application/json',
'content-type': 'application/json'
});
response.end(JSON.stringify(JSON.parse(echo)));
});
break;
case '/custom-mime':
response.writeHead(200, {
'content-type': 'application/vnd.github.beta.raw+json; charset=UTF-8'
});
response.end(JSON.stringify([6,6,6]));
break;
default:
response.writeHead(404);
response.end();
Expand Down Expand Up @@ -369,6 +375,22 @@ module.exports['Deserialization'] = {
test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
test.done();
});
},

'Should understand custom mime-type': function(test) {
rest.parsers.auto.matchers['application/vnd.github+json'] = function(data, callback) {
rest.parsers.json.call(this, data, function(err, data) {
err || (data.__parsedBy__ = 'github');
callback(err, data);
});
};
rest.get(host + '/custom-mime').on('complete', function(data) {
test.expect(3);
test.ok(Array.isArray(data), 'should be array, returned: ' + sys.inspect(data));
test.equal(data.join(''), '666', 'should be [6,6,6], returned: ' + sys.inspect(data));
test.equal(data.__parsedBy__, 'github', 'should use vendor-specific parser, returned: ' + sys.inspect(data.__parsedBy__));
test.done();
});
}

};
Expand Down

0 comments on commit 6e55df7

Please sign in to comment.