Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Allow permission MIME converter resolution for requests
Browse files Browse the repository at this point in the history
Setting `config.permissive = true` for the mime interceptor will allow
requests with an unknown Content-Type to proceed. In this situation the
mime interceptor will leave the request entity untouched.

Fixes: #88
  • Loading branch information
scothis committed Jan 12, 2015
1 parent 405cbc3 commit 556dbee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ See the docs for the MIME registry for more information on available converters
<td><em>default registry</em></td>
<td>custom MIME registry</td>
</tr>
<tr>
<td>permissive</td>
<td>optional</td>
td><em>false</em></td>
<td>allow an unknown mime type for a request</td>
</tr>
</table>

**Example**
Expand Down
20 changes: 14 additions & 6 deletions interceptor/mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

define(function (require) {

var interceptor, mime, registry, plainText, when;
var interceptor, mime, registry, noopConverter, when;

interceptor = require('../interceptor');
mime = require('../mime');
registry = require('../mime/registry');
when = require('when');

plainText = registry.lookup('text/plain');
noopConverter = {
read: function (obj) { return obj; },
write: function (obj) { return obj; }
};

/**
* MIME type support for request and response entities. Entities are
Expand All @@ -36,6 +39,7 @@
* converter, defaults to the client originating the request
* @param {Registry} [config.registry] MIME registry, defaults to the root
* registry
* @param {boolean} [config.permissive] Allow an unkown request MIME type
*
* @returns {Client}
*/
Expand All @@ -55,7 +59,13 @@
return request;
}

return config.registry.lookup(type).then(function (converter) {
return config.registry.lookup(type).otherwise(function () {
// failed to resolve converter
if (config.permissive) {
return noopConverter;
}
throw 'mime-unknown';
}).then(function (converter) {
var client = config.client || request.originator;

return when.attempt(converter.write, request.entity, { client: client, request: request, mime: type, registry: config.registry })
Expand All @@ -66,8 +76,6 @@
request.entity = entity;
return request;
});
}, function () {
throw 'mime-unknown';
});
},
response: function (response, config) {
Expand All @@ -77,7 +85,7 @@

var type = mime.parse(response.headers['Content-Type']);

return config.registry.lookup(type).otherwise(function () { return plainText; }).then(function (converter) {
return config.registry.lookup(type).otherwise(function () { return noopConverter; }).then(function (converter) {
var client = config.client || response.request && response.request.originator;

return when.attempt(converter.read, response.entity, { client: client, response: response, mime: type, registry: config.registry })
Expand Down
17 changes: 17 additions & 0 deletions test/interceptor/mime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@
})
);
},
'should error the request if unable to find a converter for the desired mime, unless in permissive mode': function () {
var client, entity, request;

client = mime(
function (request) {
return { request: request, headers: {} };
},
{ permissive: true }
);

entity = {};
request = { headers: { 'Content-Type': 'application/vnd.com.example' }, entity: entity };
return client(request).then(function (response) {
assert.same(entity, response.request.entity);
assert.equals('application/vnd.com.example', response.request.headers['Content-Type']);
}).otherwise(fail);
},
'should use text/plain converter for a response if unable to find a converter for the desired mime': function () {
var client;

Expand Down

0 comments on commit 556dbee

Please sign in to comment.