UNPKG

stick

Version:

JSGI based webapp framework

205 lines (185 loc) 5.7 kB
var ByteString = require("binary").ByteString; var MemoryStream = require("io").MemoryStream; var stick = require("../lib/stick"); var Application = stick.Application; var middleware = require("../lib/middleware"); var mount = middleware.mount, route = middleware.route; var assert = require("assert"); exports.testMiddleware = function() { function twice(next, app) { return function(req) { return next(req) + next(req); }; } function uppercase(next, app) { return function(req) { return next(req.toUpperCase()).toUpperCase(); }; } function foobar(next, app) { return function(req) { return req === "FOO" ? "bar" : "unexpected req: " + req; }; } function append_(next, app) { return function(req) { return next(req) + "_"; }; } function _prepend(next, app) { return function(req) { return "_" + next(req); }; } var app = new Application(foobar()); app.configure(twice, uppercase); assert.equal(app("foo"), "BARBAR"); app = new Application(); app.configure(twice, uppercase, foobar); assert.equal(app("foo"), "BARBAR"); var dev = app.env("development"); dev.configure(twice); var prod = app.env("production"); prod.configure(_prepend, append_); assert.equal(app("foo"), "BARBAR"); assert.equal(dev("foo"), "BARBARBARBAR"); assert.equal(prod("foo"), "_BARBAR_"); }; exports.testMount = function() { function testMount(app) { app.mount("/foo", function() { return "/foo"; }); app.mount({host:"foo.com"}, function() { return "foo.com"; }); app.mount({host:"bar.org", path:"/baz"}, function() { return "bar.org/baz"; }); assert.equal(app({headers:{host:['bar.com']}, env:{}, pathInfo:"/foo"}), "/foo"); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/foo"}), "/foo"); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/"}), "foo.com"); assert.equal(app({headers:{host:['bar.org']}, env:{}, pathInfo:"/baz"}), "bar.org/baz"); assert.throws(function() { app({headers:{host:['bing.org']}, env:{}, pathInfo:"/"}); }, Error); } var app = new Application(); app.configure(mount); testMount(app); // configuration via module name app = new Application(); app.configure("mount"); testMount(app); }; exports.testMountSort = function() { var app = new Application(); app.configure(mount); app.mount("/", function() { return "root"; }); app.mount("/foo", function() { return "foo"; }); app.mount("/foo/bar", function() { return "foo/bar"; }); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/"}), "root"); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/foo"}), "foo"); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/foo/bar"}), "foo/bar"); assert.equal(app({headers:{host:['foo.com']}, env:{}, pathInfo:"/bars"}), "root"); }; /** * Test that makes sure the most 'literal' version of the URL is resolved instead of parameterized * version. ie /foo wins over /:param when url is /foo. */ exports.testRouteResolution = function() { function testPath(path, expected) { assert.equal(app({method:'GET', headers:{host:['foo.com']}, env:{}, pathInfo:path}), expected); } var app = new Application(); app.configure(route); app.get("/:param", function(req, p) { return '[' + p + ']'; }); app.get("/foo", function() { return "foo"; }); app.get("/bar/:param", function(req, p) { return 'bar/[' + p + ']'; }); app.get("/bar/foo", function() { return "bar/foo"; }); app.get("/baz/:param/qux", function(req, p) { return 'baz/[' + p + ']/qux'; }); app.get("/baz/123/qux", function() { return "baz/123/qux"; }); testPath("/foo", "foo"); testPath("/abc", "[abc]"); testPath("/bar/foo", "bar/foo"); testPath("/bar/abc", "bar/[abc]"); testPath("/baz/abc/qux", "baz/[abc]/qux"); testPath("/baz/123/qux", "baz/123/qux"); }; exports.testPost = function() { function testPost(path, expected) { assert.equal(app({ method:'POST', headers:{host:['foo.com']}, env:{}, pathInfo:path, input:new MemoryStream(new ByteString('+P340irvc?dsfdsf+"210oi4-')) }), expected); } var app = new Application(); app.configure(route); app.post("/foo", function(request) { return "foo"; }); testPost("/foo", "foo"); }; /** * Test that makes sure the most 'literal' version of the URL is resolved instead of parameterized * version. ie /foo wins over /:param when url is /foo. */ exports.testMountAndRouteResolution = function() { function testPath(path, expected) { assert.equal(mountApp({method:'GET', headers:{host:['foo.com']}, env:{}, pathInfo:path}), expected); } var app = new Application(); app.configure(route); app.get("/:param", function(req, p) { return '[' + p + ']'; }); app.get("/foo", function() { return "foo"; }); app.get("/bar/foo", function() { return "bar/foo"; }); app.get("/bar/:param", function(req, p) { return 'bar/[' + p + ']'; }); app.get("/baz/:param/qux", function(req, p) { return 'baz/[' + p + ']/qux'; }); app.get("/baz/123/qux", function() { return "baz/123/qux"; }); var mountApp = new Application(); mountApp.configure(mount); mountApp.mount("/test", app); testPath("/test/foo", "foo"); testPath("/test/abc", "[abc]"); testPath("/test/bar/foo", "bar/foo"); testPath("/test/bar/abc", "bar/[abc]"); testPath("/test/baz/abc/qux", "baz/[abc]/qux"); testPath("/test/baz/123/qux", "baz/123/qux"); }; if (require.main === module) { require("test").run(exports); }