Skip to content

Commit

Permalink
Correcting logic to match WebMachine and added Context.state
Browse files Browse the repository at this point in the history
  • Loading branch information
tautologistics committed Dec 9, 2009
1 parent 3149834 commit abd59d6
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 25 deletions.
86 changes: 66 additions & 20 deletions nodemachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function v3c4 (context) {
var accept = accepted.shift().split(';')[0].toLowerCase();
match = ((accept == "*/*") || (provided.indexOf(accept) > -1));
}
//TODO: save selection to context
HandleDecision(context, match, true, v3d4, 406);
}

Expand All @@ -106,6 +107,7 @@ function v3d5 (context) {
var accept = accepted.shift().split(';')[0].toLowerCase();
match = ((accept == "*") || (provided.indexOf(accept) > -1));
}
//TODO: save selection to context
HandleDecision(context, match, true, v3e5, 406);
}

Expand All @@ -122,6 +124,7 @@ function v3e6 (context) {
var accept = accepted.shift().split(';')[0].toLowerCase();
match = ((accept == "*") || (provided.indexOf(accept) > -1));
}
//TODO: save selection to context
HandleDecision(context, match, true, v3f6, 406);
}

Expand All @@ -138,6 +141,7 @@ function v3f7 (context) {
var accept = accepted.shift().split(';')[0].toLowerCase();
match = ((accept == "*") || (provided.indexOf(accept) > -1));
}
//TODO: save selection to context
HandleDecision(context, match, true, v3g7, 406);
}

Expand Down Expand Up @@ -318,15 +322,41 @@ function v3n16 (context) {
}

function v3n11 (context) {
context.app.resourceLocationForPostChanged(context.req, function v3l5_callback (result) {
if (result) {
contest.setHeader("Location", result);
HandleDecision(context, true, true, 303, 303);
} else
/*
* if (post_is_create()) {
* if (create_path() != null) {
* set_disp_path()
* if (accept_helper()) {
* if (resp_redirect()) {
* "Location: " + 303
* }
* }
* } else {
* !error
* }
* } else {
* process_post()
* }
*/

context.app.postIsCreate(context.req, function v3l5_callback (result) {
if (result)
context.app.createPath(context.req, function v3l5b_callback (result) {
if (result)
context.res.setHeader("Location", result);
HandleDecision(context, (result != null), true, 303, 500);
});
else
HandleDecision(context, true, true, v3p11, v3p11);
});
}

function v3p11 (context) {
//if create_path() [state] then 201 else o20
context.res.sendBody("v3p11 reached");
//TODO
}

function v3o16 (context) {
HandleDecision(context, (context.req.method == 'PUT'), true, v3o14, v3o18);
}
Expand Down Expand Up @@ -357,22 +387,33 @@ function v3p3 (context) {
});
}

function v3p11 (context) {
context.res.sendBody("v3p11 reached");
//TODO
}

function HandleDecision (context, result, expected, match, nomatch) {
var which = (result == expected) ? match : nomatch;
if ((typeof which) == "function") {
//if (context.trace)
// sys.debug("[TRACE] " + which.name);
if (context.trace)
// sys.debug("[TRACE] " + which.name);
context.stack.push(which.name);
which(context);
} else if (which == parseInt(which)) {
context.res.status = which;
// sys.debug("Decision Stack: " + context.stack.join(', '));
context.res.setHeader('Decision-Stack', context.stack.join(', '));
function finishFun (result) {
//if (context.trace)
// sys.debug("Decision Stack: " + context.stack.join(', '));
if (result)
context.res.setHeader("ETag", result)
if (context.trace)
context.res.setHeader('Decision-Stack', context.stack.join(', '));
context.app.resourceExpiration(context, function (result) {
if (result)
context.res.setHeader("Expires", result.toUTCString());
context.res.finish();
});
}
if (which == 304)
context.app.resourceEtag(context, finishFun);
else
finishFun();
} else
throw new Exception("Unhandled result type for HandleDecision()");
}
Expand All @@ -389,7 +430,7 @@ function HandleRequest (server, req, res, trace) {
// res.sendBody("Howdy!\n");
// res.sendBody(JSON.stringify(req.headers));

res.finish();
//res.finish();
}

function Server (port, host, trace) {
Expand Down Expand Up @@ -432,6 +473,7 @@ function Context (app, req, res, trace) {
this.app = app;
this.req = req;
this.res = res;
this.state = {};
}

function Response (res) {
Expand Down Expand Up @@ -506,6 +548,14 @@ Response.prototype.setHeader = function Response__setHeader (name, value) {
this._headers[name] = value;
}

Response.prototype.getHeader = function Response__getHeader (name) {
return(this._headers[name]);
}

Response.prototype.delHeader = function Response__delHeader (name) {
return(delete this._headers[name]);
}

function App () {
}

Expand Down Expand Up @@ -620,11 +670,7 @@ App.prototype.allowMissingPost = function App__allowMissingPost (req, callback)
callback(false);
}

App.prototype.resourceLocationForPostChanged = function App__resourceLocationForPostChanged (req, callback) {
callback(false);
}

App.prototype.deleteResource = function App__deleteResource (req, callback) {
App.prototype.deleteResource = function App__deleteResource (req, callback) { //Deletion of resource occurs here
callback(false);
}

Expand All @@ -648,7 +694,7 @@ App.prototype.createPath = function App__createPath (req, callback) {
callback(null);
}

App.prototype.processPost = function App__processPost (req, callback) {
App.prototype.processPost = function App__processPost (req, callback) { //Handling of post data occurs here (and write body)
callback(false);
}

Expand Down
138 changes: 133 additions & 5 deletions testscenarios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var sys = require('sys');

exports.testScenarios = [
{
name: "v3b13 false",
Expand Down Expand Up @@ -308,8 +310,17 @@ exports.testScenarios = [
checkStatus: 412,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3h7"],
},
{
name: "v3h7 true",
appConfig: { canHandleResource: true, resourceExists: false },
method: "GET",
path: "/",
headers: { "If-match": "*" },
checkStatus: 412,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3h7"],
},
// {//TODO
// name: "v3h7",
// name: "v3h7 false",
// },
{
name: "v3g7 true",
Expand Down Expand Up @@ -491,6 +502,93 @@ exports.testScenarios = [
checkStatus: 202,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3m20", "v3m20b"],
},
{
name: "v3m16 false",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
},
{
name: "v3n16 false",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
},
{
name: "v3n16 true",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false, getAllowedMethods: ['POST'], postIsCreate: true, createPath: '/items' },
method: "POST",
path: "/",
headers: { },
checkStatus: 303,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3n11"],
},
{
name: "v3n11 true",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false, getAllowedMethods: ['POST'], postIsCreate: true, createPath: '/items' },
method: "POST",
path: "/",
headers: { },
checkStatus: 303,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3n11"],
},
// {//TODO
// name: "v3n11 false",
// },
{
name: "v3o16 false",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
},
{
name: "v3o16 true",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false, getAllowedMethods: ['PUT'], isConflict: true },
method: "PUT",
path: "/",
headers: { },
checkStatus: 409,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o14"],
},
{
name: "v3o14 true",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false, getAllowedMethods: ['PUT'], isConflict: true },
method: "PUT",
path: "/",
headers: { },
checkStatus: 409,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o14"],
},
// {//TODO
// name: "v3o14 false",
//},
{
name: "v3o18 false",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
},
{
name: "v3o18 true",
appConfig: { canHandleResource: true, deleteResource: true, deleteComplete: false, multipleChoices: true },
method: "GET",
path: "/",
headers: { },
checkStatus: 300,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
},
{
name: "v3m20 false",
appConfig: { canHandleResource: true, getAllowedMethods: ['DELETE'], deleteResource: true, deleteComplete: false },
Expand All @@ -501,17 +599,47 @@ exports.testScenarios = [
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3m20", "v3m20b"],
},
{
name: "v3m20 true",
name: "v3o20 false",
appConfig: { canHandleResource: true, getAllowedMethods: ['DELETE'], deleteResource: true, deleteComplete: true, responseEntityExists: false },
method: "DELETE",
path: "/",
headers: { },
checkStatus: 204,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3m20", "v3m20b", "v3o20"],
},
// {//TODO
// name: "v3m16 false",
// }
{
name: "v3o20 true",
appConfig: { canHandleResource: true, getAllowedMethods: ['DELETE'], deleteResource: true, deleteComplete: true, responseEntityExists: true },
method: "DELETE",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3m20", "v3m20b", "v3o20", "v3o18"],
},
{
name: "Send ETag",
appConfig: { canHandleResource: true, resourceEtag: "xxx" },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
checkHeaders: function (headers) {
return(headers["etag"] == "xxx");
}
},
{
name: "Send Expires",
appConfig: { canHandleResource: true, resourceExpiration: new Date("Fri, 28 Nov 1975 10:23:02 GMT") },
method: "GET",
path: "/",
headers: { },
checkStatus: 200,
checkStack: ["v3b13", "v3b12", "v3b11", "v3b10", "v3b9", "v3b8", "v3b7", "v3b6", "v3b5", "v3b4", "v3b3", "v3c3", "v3d4", "v3e5", "v3f6", "v3g7", "v3g8", "v3h10", "v3i12", "v3l13", "v3m16", "v3n16", "v3o16", "v3o18"],
checkHeaders: function (headers) {
return(headers["expires"] == "Fri, 28 Nov 1975 10:23:02 GMT");
}
},
];
//"Fri, 28 Nov 1975 10:23:02 GMT"
//"1975/11/28 5:23:02 AM"
Expand Down

0 comments on commit abd59d6

Please sign in to comment.