1
0
Fork 0

Fuxx: Added support for default in FormatMiddleware

This commit is contained in:
Lucas Dohmen 2013-03-20 14:29:21 +01:00
parent 51f75fbb08
commit d71b459d58
2 changed files with 22 additions and 9 deletions

View File

@ -449,12 +449,16 @@ BaseMiddleware = function (templateCollection, helperCollection) {
// Use it by calling: // Use it by calling:
// //
// FormatMiddleware = require('frank').FormatMiddleware; // FormatMiddleware = require('frank').FormatMiddleware;
// app.use(FormatMiddleware.new(['json'])); // app.before("/*", FormatMiddleware.new(['json']));
// //
// or the shortcut: // or the shortcut:
// //
// app.accepts(['json']); // app.accepts(['json']);
FormatMiddleware = function (allowedFormats) { //
// In both forms you can give a default format as a second parameter,
// if no format could be determined. If you give no defaultFormat this
// case will be handled as an error.
FormatMiddleware = function (allowedFormats, defaultFormat) {
'use strict'; 'use strict';
var middleware, urlFormatToMime, mimeToUrlFormat, determinePathAndFormat; var middleware, urlFormatToMime, mimeToUrlFormat, determinePathAndFormat;
@ -470,9 +474,9 @@ FormatMiddleware = function (allowedFormats) {
"text/plain": "txt" "text/plain": "txt"
}; };
determinePathAndFormat = function(path, headers) { determinePathAndFormat = function (path, headers) {
var parsed = { var parsed = {
contentType: headers.accept, contentType: headers.accept
}; };
path = path.split('.'); path = path.split('.');
@ -483,16 +487,17 @@ FormatMiddleware = function (allowedFormats) {
parsed.path = path.join('.'); parsed.path = path.join('.');
} }
if (parsed.contentType === undefined) { if (parsed.contentType === undefined && parsed.format === undefined) {
parsed.format = defaultFormat;
parsed.contentType = urlFormatToMime[defaultFormat];
} else if (parsed.contentType === undefined) {
parsed.contentType = urlFormatToMime[parsed.format]; parsed.contentType = urlFormatToMime[parsed.format];
} } else if (parsed.format === undefined) {
if (parsed.format === undefined) {
parsed.format = mimeToUrlFormat[parsed.contentType]; parsed.format = mimeToUrlFormat[parsed.contentType];
} }
if (parsed.format !== mimeToUrlFormat[parsed.contentType]) { if (parsed.format !== mimeToUrlFormat[parsed.contentType]) {
parsed.error = "Contradiction between Accept Header and URL." parsed.error = "Contradiction between Accept Header and URL.";
} }
if (allowedFormats.indexOf(parsed.format) < 0) { if (allowedFormats.indexOf(parsed.format) < 0) {

View File

@ -533,6 +533,14 @@ function FormatMiddlewareSpec () {
assertFalse(nextCalled); assertFalse(nextCalled);
}, },
testFallBackToDefaultWhenMissingBothURLAndResponseType: function () {
request = { path: "test/1", headers: {} };
middleware = new Middleware(["json"], "json");
middleware(request, response, options, next);
assertEqual(request.format, "json");
assertEqual(response.contentType, "application/json");
},
// JSON // JSON
testSettingTheFormatAttributeAndResponseTypeForJsonViaURL: function () { testSettingTheFormatAttributeAndResponseTypeForJsonViaURL: function () {
request = { path: "test/1.json", headers: {} }; request = { path: "test/1.json", headers: {} };