Skip to content

Instantly share code, notes, and snippets.

@oravecz
Created August 17, 2011 21:38
Show Gist options
  • Select an option

  • Save oravecz/1152708 to your computer and use it in GitHub Desktop.

Select an option

Save oravecz/1152708 to your computer and use it in GitHub Desktop.
Test cases for RingoJS - scriptName and pathInfo
/**
* From the JSGI specification: (http://wiki.commonjs.org/wiki/JSGI/Level0/A/Draft2)
*
* The remainder of the request URL's "path", designating the virtual "location" of the Request's
* target within the Application. This may be an empty string, if the request URL targets the
* Application root and does not have a trailing slash. Restriction: if non-empty `pathInfo` MUST
* start with "/" and MUST NOT be decoded.
*
* Note: We can't simulate context path in the request, so this test may not be complete.
*/
exports.testPathInfo = function() {
function checkPathInfo(app, path, expected) {
function assertThis(method) {
var resp = app({headers: {host: "foo.com"}, env: {}, method: method, scriptName: base, pathInfo: path});
assert.equal(resp, expected);
if (path !== '/') {
resp = app({headers: {host: "foo.com"}, env: {}, method: method, scriptName: base, pathInfo: path + '/'});
assert.equal(resp, expected + '/');
}
}
['GET','POST'].forEach(function(method) {
assertThis(method);
});
}
var controller = function(req) { return req.pathInfo; };
var base = '/context/path';
var app = new Application();
app.configure(route);
app.get("/", controller);
app.post("/", controller);
app.get("/foo", controller);
app.post("/foo", controller);
app.get("/bar/foo", controller);
app.post("/bar/foo", controller);
app.get("/bar/some$path", controller);
checkPathInfo(app, '/', '');
checkPathInfo(app, '/foo', '/foo');
checkPathInfo(app, '/bar/foo', '/bar/foo');
checkPathInfo(app, '/bar/some%24path', '/bar/some%24path');
var mounted = new Application();
mounted.configure(mount);
mounted.mount("/", controller);
checkPathInfo(mounted, '/', '');
checkPathInfo(mounted, '/foo', '/foo');
checkPathInfo(mounted, '/bar/foo', '/bar/foo');
mounted.mount("/baz", controller);
checkPathInfo(mounted, '/baz', '');
checkPathInfo(mounted, '/baz/foo', '/foo');
checkPathInfo(mounted, '/baz/bar/foo', '/bar/foo');
mounted.mount("/baz/qux", controller);
checkPathInfo(mounted, '/baz/qux', '');
checkPathInfo(mounted, '/baz/qux/foo', '/foo');
checkPathInfo(mounted, '/baz/qux/bar/foo', '/bar/foo');
};
/**
* From the JSGI specification: (http://wiki.commonjs.org/wiki/JSGI/Level0/A/Draft2)
*
* The remainder of the request URL's "path", designating the virtual "location" of the Request's
* target within the Application. This may be an empty string, if the request URL targets the
* Application root and does not have a trailing slash. Restriction: if non-empty `pathInfo` MUST
* start with "/" and MUST NOT be decoded.
*
* Note: We can't simulate context path in the request, so this test may not be complete.
*/
exports.testScriptName = function() {
function checkScriptName(app, path, expected) {
function assertThis(method) {
var resp = app({headers: {host: "foo.com"}, env: {}, method: method, scriptName: base, pathInfo: path});
assert.equal(resp, base + expected);
}
['GET','POST'].forEach(function(method) {
assertThis(method);
});
}
var controller = function(req) { return req.scriptName; };
var base = '/context/path';
var app = new Application();
app.configure(route);
app.get("/", controller);
app.post("/", controller);
app.get("/foo", controller);
app.post("/foo", controller);
checkScriptName(app, '/', '');
checkScriptName(app, '/foo', '');
var mounted = new Application();
mounted.configure(mount);
mounted.mount("/", controller);
checkScriptName(mounted, '/', '');
checkScriptName(mounted, '/foo', '');
mounted.mount("/baz", controller);
checkScriptName(mounted, '/baz', '/baz');
checkScriptName(mounted, '/baz/foo', '/baz');
mounted.mount("/baz/qux", controller);
checkScriptName(mounted, '/baz/qux', '/baz/qux');
checkScriptName(mounted, '/baz/qux/foo', '/baz/qux');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment