Skip to content

Commit

Permalink
[hooks.js] added expectParams in routes
Browse files Browse the repository at this point in the history
This gives more error checking and better documentation.

Change-Id: I58dc5775e632ec4d59a65c443a5faa12457a186e
Reviewed-on: http://review.northscale.com:8080/315
Reviewed-by: Steve Yen <steve.yen@gmail.com>
Tested-by: Steve Yen <steve.yen@gmail.com>
  • Loading branch information
Aliaksey Kandratsenka authored and steveyen committed Jun 7, 2010
1 parent c835129 commit b82c3a4
Showing 1 changed file with 91 additions and 26 deletions.
117 changes: 91 additions & 26 deletions deps/menelaus/priv/js/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ var MockedRequest = mkClass({
return body.call(this, foundResp, routeArgs);
return foundResp;
},
executeRouteResponse: function (foundResp, routeArgs) {
if (_.isFunction(foundResp)) {
if (functionArgumentNames(foundResp)[0] == "$data")
routeArgs.unshift(this.deserialize());
foundResp = foundResp.apply(this, routeArgs);
if (this.responded)
return;
if (foundResp == null)
foundResp = "";
}
return _.clone(foundResp);
},
respondForReal: function () {
if ($.ajaxSettings.beforeSend)
$.ajaxSettings.beforeSend(this.fakeXHR);
Expand All @@ -193,16 +205,9 @@ var MockedRequest = mkClass({

try {
this.checkAuth();
if (_.isFunction(foundResp)) {
if (functionArgumentNames(foundResp)[0] == "$data")
routeArgs.unshift(this.deserialize());
foundResp = foundResp.apply(this, routeArgs);
if (this.responded)
return;
if (foundResp == null)
foundResp = "";
}
this.fakeResponse(_.clone(foundResp));
foundResp = this.executeRouteResponse(foundResp, routeArgs);
if (!this.responded)
this.fakeResponse(foundResp);
} catch (e) {
if (e !== this.authError) {
throw e;
Expand Down Expand Up @@ -377,6 +382,59 @@ var MockedRequest = mkClass({
}
}

// for optional params
function opt(name) {
name = new String(name);
name.__opt = true;
return name;
}

function expectParams() {
var expectedParams = _.toArray(arguments);

var chainedRoute = expectedParams[0];
if (!_.isString(chainedRoute))
expectedParams.shift();
else
chainedRoute = null;

var mustParams = [], optionalParams = [];
_.each(expectedParams, function (p) {
if (p.__opt)
optionalParams.push(p.valueOf());
else
mustParams.push(p);
});

var difference = function (a, b) {
return _.reject(a, function (e) {
return _.include(b, e);
});
}

return function () {
var params = this.deserialize();
var keys = _.keys(params);

var missingParams = difference(mustParams, keys);
if (missingParams.length) {
var msg = "Missing required parameter(s): " + missingParams.join(', ');
alert("hooks.js: " + msg);
throw new Error(msg);
}

var unexpectedParams = difference(difference(keys, mustParams), optionalParams);
if (unexpectedParams.length) {
var msg = "Post has unexpected parameter(s): " + unexpectedParams.join(', ');
alert("hooks.js: " + msg);
throw new Error(msg);
}

if (chainedRoute)
return this.executeRouteResponse(chainedRoute, _.toArray(arguments));
}
}

var rv = [
[post("logClientError"), method('doNothingPOST')],
[get("logs"), {list: [{type: "info", code: 1, module: "ns_config_log", tstamp: 1265358398000, shortText: "message", text: "config changed"},
Expand Down Expand Up @@ -554,28 +612,35 @@ var MockedRequest = mkClass({
[del("pools", "default", "buckets", x), method('handleBucketRemoval')],

[get("nodes", x), {
"license":"","licenseValue":false,"licenseValidUntil":"invalid"
"memoryQuota":"none",
"storage":{"ssd":[],
"hdd":[{"path":"./data","quotaMb":"none","state":"ok"}]},
"hostname":"127.0.0.1",
"version":"1.0.3_98_g5d1f7a2",
"os":"i386-apple-darwin10.3.0",
"ports":{"proxy":11211,"direct":11210}}
}],
"license":"","licenseValue":false,"licenseValidUntil":"invalid",
"memoryQuota":"none",
"storage":{"ssd":[],
"hdd":[{"path":"./data","quotaMb":"none","state":"ok"}]},
"hostname":"127.0.0.1",
"version":"1.0.3_98_g5d1f7a2",
"os":"i386-apple-darwin10.3.0",
"ports":{"proxy":11211,"direct":11210}}],
[post("nodes", x, "controller", "settings"), {}], //missing

[post("node", "controller", "initStatus"), function ($data) {
this.globalData.initValue = $data;
this.globalData.initValue = $data.initValue;
}],

[post("node", "controller", "doJoinCluster"), method('handleJoinCluster')],
[post("node", "controller", "doJoinCluster"), expectParams(method('handleJoinCluster'),
"clusterMemberHostIp", "clusterMemberPort",
"user", "password")],
[post("pools", "default", "controller", "testWorkload"), method('handleWorkloadControlPost')],
[post("controllers", "ejectNode"), method('doNothingPOST')],
[post("controllers", "rebalance"), method('doNothingPOST')],
[post("controllers", "addNode"), method("doNothingPOST")],
[post("controllers", "failOver"), method("doNothingPOST")],
[post("controllers", "reAddNode"), method("doNothingPOST")]
[post("controllers", "ejectNode"), expectParams(method('doNothingPOST'),
"otpNode")],
[post("controllers", "rebalance"), expectParams(method('doNothingPOST'),
opt("knownNodes"), opt("ejectedNodes"))], //opt is tmp
[post("controllers", "addNode"), expectParams(method("doNothingPOST"),
"hostname",
"user", "password")],
[post("controllers", "failOver"), expectParams(method("doNothingPOST"),
opt("hostname"))],
[post("controllers", "reAddNode"), expectParams(method("doNothingPOST"),
"hostname")]
];

rv.x = x;
Expand Down

0 comments on commit b82c3a4

Please sign in to comment.