From e0c071b4976da8f27a6c589a4aa7782f9ce86ccf Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Tue, 14 Apr 2015 13:45:04 +0000 Subject: [PATCH] refactor tests as mocha --- node_modules/frontdoor/lib/route_test.js | 270 +++++++++++---------- node_modules/frontdoor/lib/section_test.js | 170 +++++++++++++ 2 files changed, 312 insertions(+), 128 deletions(-) create mode 100644 node_modules/frontdoor/lib/section_test.js diff --git a/node_modules/frontdoor/lib/route_test.js b/node_modules/frontdoor/lib/route_test.js index 3d98b710..2d533ee9 100644 --- a/node_modules/frontdoor/lib/route_test.js +++ b/node_modules/frontdoor/lib/route_test.js @@ -1,137 +1,151 @@ "use strict"; -"use server"; - -var assert = require("assert"); var sinon = require("sinon"); +var frontdoor = require('../frontdoor'); +var Route = frontdoor.Route; +var assert = require('assert'); -var Route = require("./route"); +require("c9/inline-mocha")(module); +require("amd-loader"); -module.exports = { - "test router: simple route with argument": function() { - var route = new Route("/user/:name", sinon.stub()); - - var req = {}; - assert.equal(route.match(req, "/juhu"), false); - assert.equal(route.match(req, "/juhu/12"), false); - - assert.equal(route.match(req, "/user/fabian"), true); - assert.equal(req.match.name, "fabian"); - }, - - "test router: simple route with number argument": function() { - var route = new Route("/user/:id", { - params: { - id: { - type: "int" - } +it("test router: simple route with argument", function(done) { + var route = new Route("/user/:name", sinon.stub()); + + var req = {}; + assert.equal(route.match(req, "/juhu"), false); + assert.equal(route.match(req, "/juhu/12"), false); + + assert.equal(route.match(req, "/user/fabian"), true); + assert.equal(req.match.name, "fabian"); + + done(); +}); + +it("test router: simple route with number argument", function(done) { + var route = new Route("/user/:id", { + params: { + id: { + type: "int" } - }, sinon.stub()); - - var req = {}; - assert.equal(route.match(req, "/user/fabian"), false); - assert.equal(route.match(req, "/user/123"), true); - assert.equal(req.match.id, 123); - }, - - "test router: for params if the value is a string it is treated as the type": function() { - var route = new Route("/user/:id", { - params: { - id: "int" - } - }, sinon.stub()); - - var req = {}; - assert.equal(route.match(req, "/user/123"), true); - assert.equal(req.match.id, 123); - }, - - "test router: complex route with wildcard arguments": function() { - var route = new Route("/user/:name/:rest*", { - params: { - id: { - type: "int" - }, - rest: { - type: "string" - } - } - }, sinon.stub()); - - var req = {}; - - assert.equal(route.match(req, "/user/fabian"), false); - assert.equal(route.match(req, "/user/fabian/"), true); - assert.equal(req.match.name, "fabian"); - assert.equal(req.match.rest, "/"); - assert.equal(route.match(req, "/user/fabian/abc"), true); - assert.equal(req.match.name, "fabian"); - assert.equal(req.match.rest, "/abc"); - assert.equal(route.match(req, "/user/fabian/abc/123"), true); - assert.equal(req.match.name, "fabian"); - assert.equal(req.match.rest, "/abc/123"); - }, - - "test router: complex route with multiple arguments": function() { - var route = new Route("/user/:name/:id", { - params: { - id: { - type: "int" - } - } - }, sinon.stub()); - - var req = {}; - - assert.equal(route.match(req, "/user/fabian"), false); - assert.equal(route.match(req, "/user/123"), false); - assert.equal(route.match(req, "/user/fabian/123"), true); - assert.equal(req.match.id, 123); - assert.equal(req.match.name, "fabian"); - }, - - "test regexp types": function() { - var route = new Route("/users/:uid", { - params: { - uid: /u\d+/ - } - }, sinon.stub()); - - var req = {}; - - assert.ok(route.match(req, "/users/u123")); - assert.ok(!route.match(req, "/users/_u123")); - }, - - "test custom type without register": function() { - var DateType = { - parse: function(string) { - if (!/\d{13}/.test(string)) - throw new Error("not a timestamp"); - - return new Date(parseInt(string, 10)); + } + }, sinon.stub()); + + var req = {}; + assert.equal(route.match(req, "/user/fabian"), false); + assert.equal(route.match(req, "/user/123"), true); + assert.equal(req.match.id, 123); + + done(); +}); + +it("test router: for params if the value is a string it is treated as the type", function(done) { + var route = new Route("/user/:id", { + params: { + id: "int" + } + }, sinon.stub()); + + var req = {}; + assert.equal(route.match(req, "/user/123"), true); + assert.equal(req.match.id, 123); + + done(); +}); + +it("test router: complex route with wildcard arguments", function(done) { + var route = new Route("/user/:name/:rest*", { + params: { + id: { + type: "int" }, - check: function(value) { - return value instanceof Date; + rest: { + type: "string" } - }; - - var route = new Route("/ts/:ts", { - params: { - ts: { - type: DateType - } - } - }, sinon.stub()); - - var req = {}; - - assert.ok(route.match(req, "/ts/1353676299181")); - assert.ok(req.match.ts instanceof Date); - - assert.ok(!route.match(req, "/ts/353676299181")); - assert.ok(!route.match(req, "/ts/abc")); - } -}; + } + }, sinon.stub()); -!module.parent && require("asyncjs").test.testcase(module.exports).exec(); + var req = {}; + + assert.equal(route.match(req, "/user/fabian"), false); + assert.equal(route.match(req, "/user/fabian/"), true); + assert.equal(req.match.name, "fabian"); + assert.equal(req.match.rest, "/"); + assert.equal(route.match(req, "/user/fabian/abc"), true); + assert.equal(req.match.name, "fabian"); + assert.equal(req.match.rest, "/abc"); + assert.equal(route.match(req, "/user/fabian/abc/123"), true); + assert.equal(req.match.name, "fabian"); + assert.equal(req.match.rest, "/abc/123"); + + done(); + +}); + +it("test router: complex route with multiple arguments", function(done) { + var route = new Route("/user/:name/:id", { + params: { + id: { + type: "int" + } + } + }, sinon.stub()); + + var req = {}; + + assert.equal(route.match(req, "/user/fabian"), false); + assert.equal(route.match(req, "/user/123"), false); + assert.equal(route.match(req, "/user/fabian/123"), true); + assert.equal(req.match.id, 123); + assert.equal(req.match.name, "fabian"); + + done(); + +}); + +it("test regexp types", function(done) { + var route = new Route("/users/:uid", { + params: { + uid: /u\d+/ + } + }, sinon.stub()); + + var req = {}; + + assert.ok(route.match(req, "/users/u123")); + assert.ok(!route.match(req, "/users/_u123")); + + done(); + +}); + +it("test custom type without register", function(done) { + var DateType = { + parse: function(string) { + if (!/\d{13}/.test(string)) + throw new Error("not a timestamp"); + + return new Date(parseInt(string, 10)); + }, + check: function(value) { + return value instanceof Date; + } + }; + + var route = new Route("/ts/:ts", { + params: { + ts: { + type: DateType + } + } + }, sinon.stub()); + + var req = {}; + + assert.ok(route.match(req, "/ts/1353676299181")); + assert.ok(req.match.ts instanceof Date); + + assert.ok(!route.match(req, "/ts/353676299181")); + assert.ok(!route.match(req, "/ts/abc")); + + done(); +}); \ No newline at end of file diff --git a/node_modules/frontdoor/lib/section_test.js b/node_modules/frontdoor/lib/section_test.js new file mode 100644 index 00000000..b54b6658 --- /dev/null +++ b/node_modules/frontdoor/lib/section_test.js @@ -0,0 +1,170 @@ +var assert = require('assert-diff'); +var Section = require('../frontdoor').Section; +var Url = require('url'); + +require("c9/inline-mocha")(module); +// require("amd-loader"); + + +var mock = { + req: function( method, uri) { + var parsedUrl = Url.parse(uri||'', true); + + return { + method: method || 'get', + parsedUrl: parsedUrl, + pathname: parsedUrl.pathname, + } + + } +}; + +it('Defines params on section level', function(done) { + var testParams = { + required: { + type: 'int', + optional: false, + }, + int: { + type: 'int', + source: 'url' + }, + string: 'string', + alphanum: { + type: /[a-z0-9]+/, + source: 'url' + }, + }; + + var cases = [ + { + label: 'Match a simple string param', + path: '/test/:string', + url: '/test/foo', + params: { + string: 'foo', + } + }, + { + label: 'Match a simple number param', + path: '/test/:int', + url: '/test/123', + params: { + int: 123, + } + }, + { + label: 'Match multiple params', + path: '/test/:int/:string', + url: '/test/123/hello', + params: { + string: 'hello', + int: 123, + } + }, + { + label: 'Match multiple params 3x', + path: '/test/:string/:int/:alphanum', + url: '/test/hello/123/baz123', + params: { + string: 'hello', + int: 123, + alphanum: 'baz123' + } + }, + { + label: 'Check ordered params', + path: '/test/:string/:int/:alphanum', + url: '/test/123/hello/baz123', + err: true, + }, + { + label: 'Must match type int param', + path: '/test/:int', + url: '/test/test', + err: true, + }, + { + label: 'Must match optinal type int', + path: '/test/:int', + url: '/test', + err: true, + }, + { + label: 'Must match required type int', + path: '/test/:required', + url: '/test', + err: true, + }, + { + label: 'Match an optional param', + path: '/test/:optional', + url: '/test', + err: true, + }, + { + label: 'Match an implied url param', + path: '/test/:implied', + url: '/test/ok', + params: { + implied: 'ok', + }, + }, + { + label: 'Query params can be passed along', + path: '/test/:string/:int/:alphanum', + url: '/test/hello/123/baz123?q=123', + options: { + params: { + q: { + type: 'int', + optional: false, + source: 'query', + } + } + }, + params: { + string: 'hello', + int: 123, + alphanum: 'baz123', + q: 123 + } + }, + { + label: 'Required query params must be passed', + path: '/test/:string/:int/:alphanum', + url: '/test/hello/123/baz123', + err: true, + options: { + params: { + q: { + type: 'int', + optional: false, + source: 'query', + } + } + }, + }, + ]; + + cases.forEach(function(testCase) { + var req = mock.req('get', testCase.url), + api = new Section('test'); + + api.params = testParams; + + api.get( testCase.path, testCase.options || {}, function(req, res, next){ + assert.deepEqual( req.params, testCase.params, testCase.label ); + }); + + api.handle( req.pathname, req, {}, function(err) { + if ( testCase.err ) { + assert.ok( 'route not matched: ' + testCase.label ); + return; + } + assert.fail( 'route not matched: ' + testCase.label ); + }); + }); + + done(); +});