Merge pull request +11228 from c9/fix-frontdoor-type-checks

Fix frontdoor type checks
pull/227/head
Fabian Jakobs 2015-12-21 16:38:07 +01:00
commit e1c2c7a2a1
3 zmienionych plików z 49 dodań i 6 usunięć

21
node_modules/frontdoor/lib/route.js wygenerowano vendored
Wyświetl plik

@ -195,19 +195,25 @@ module.exports = function Route(route, options, handler, types) {
var type = param.type;
var value = EMPTY;
var isValid = true;
switch(param.source) {
switch (param.source) {
case "body":
if (param.optional && !(key in body))
break;
value = body[key]; // body is already JSON parsed
if (param.optional && value == null)
if (param.optional && value == null) {
value = EMPTY;
break;
}
isValid = type.check(value);
break;
case "query":
if (param.optional && query[key] == null)
if (param.optional && query[key] == null) {
value = EMPTY;
break;
}
try {
value = type.parse(query[key]);
@ -216,13 +222,19 @@ module.exports = function Route(route, options, handler, types) {
}
isValid = isValid === false ? false : type.check(value);
break;
case "url":
if (param.optional && urlParams[key] == null)
if (param.optional && urlParams[key] == null) {
value = EMPTY;
break;
}
value = urlParams[key]; // is already parsed and checked
isValid = true;
break;
default:
throw new Error("Invalid source: " + params.source);
}
if (!isValid) {
@ -238,7 +250,6 @@ module.exports = function Route(route, options, handler, types) {
req.params[key] = value;
else if ("default" in param)
req.params[key] = param.default;
}
}
}

32
node_modules/frontdoor/lib/route_test.js wygenerowano vendored
Wyświetl plik

@ -164,6 +164,38 @@ module.exports = {
});
},
"test router: decode parameter in body with defaults": function(next) {
var route = new Route("/user", {
params: {
scm: {
type: /^(git|hg)?$/,
optional: true,
default: "git",
source: "body"
}
}
}, sinon.stub());
var req = {
match: "match",
parsedUrl: { query: "" },
body: { }
};
var res = {};
route.decodeParams(req, res, function(err, result) {
assert.equal(err, null);
assert.equal(req.params.scm, "git");
req.body.scm = null; // should be treated the same as undefined
route.decodeParams(req, res, function(err, result) {
assert.equal(err, null);
assert.equal(req.params.scm, "git");
next();
});
});
},
"test router: optional number argument can be falsy": function(next) {
var route = new Route("/user", {
params: {

Wyświetl plik

@ -122,7 +122,7 @@ function plugin(options, imports, register) {
var allowedErrorKeys = [
"message", "projectState", "premium", "retryIn", "progress",
"oldHost", "blocked", "className"
"oldHost", "blocked", "className", "errors"
];
allowedErrorKeys.forEach(function(key) {