From 556dbee9fc4eec26eca8f0a23581cf315c386d44 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Sun, 11 Jan 2015 16:59:11 -0800 Subject: [PATCH] Allow permission MIME converter resolution for requests 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 --- docs/interceptors.md | 6 ++++++ interceptor/mime.js | 20 ++++++++++++++------ test/interceptor/mime-test.js | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/interceptors.md b/docs/interceptors.md index b429c36..f66101f 100644 --- a/docs/interceptors.md +++ b/docs/interceptors.md @@ -357,6 +357,12 @@ See the docs for the MIME registry for more information on available converters default registry custom MIME registry + + permissive + optional + td>false + allow an unknown mime type for a request + **Example** diff --git a/interceptor/mime.js b/interceptor/mime.js index a6e6411..320384b 100644 --- a/interceptor/mime.js +++ b/interceptor/mime.js @@ -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 @@ -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} */ @@ -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 }) @@ -66,8 +76,6 @@ request.entity = entity; return request; }); - }, function () { - throw 'mime-unknown'; }); }, response: function (response, config) { @@ -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 }) diff --git a/test/interceptor/mime-test.js b/test/interceptor/mime-test.js index 46eb6cf..1cee6ba 100644 --- a/test/interceptor/mime-test.js +++ b/test/interceptor/mime-test.js @@ -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;