Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

custom mime types support, unit cases (closes #45)

  • Loading branch information...
commit 6e55df7442dc43f7f49a39bb3168798bad82c866 1 parent 60983d5
@disfated disfated authored
Showing with 46 additions and 8 deletions.
  1. +23 −7 lib/restler.js
  2. +23 −1 test/restler.js
View
30 lib/restler.js
@@ -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) {
View
24 test/restler.js
@@ -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();
@@ -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();
+ });
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.