var expect = require('expect') var Router = require('../lib/router') var NotFoundError = require('../lib/error').NotFound describe('Router', function () { it('→ exists', function () { expect(Router).toExist() }) it('→ add root node', function (done) { var router = new Router() router.add('/', function () { return 'some value' }) router.route('/', function (err, handlerResult) { expect(err).toBe(null) expect(handlerResult).toBe('some value') done() }) }) it('→ add/get nested route', function (done) { var router = new Router() router.add('/some/path/here', function () { return 'some value' }) router.route('/some/path/here', function (err, handlerResult) { expect(err).toBe(null) expect(handlerResult).toBe('some value') done() }) }) it('→ partial route is a not found error', function (done) { var router = new Router() router.add(['/user', /^(\d+)$/], function (number) { return number }) router.route('/user/', function (err) { expect(err).toBeA(NotFoundError) done() }) }) it('→ get error in callback for missing handler', function (done) { var router = new Router() router.route('/some/fake/path', function (err, handlerResult) { expect(err).toBeA(Error) expect(err.message).toMatch(/not found/) expect(handlerResult).toNotExist() done() }) }) it('→ get routes based on regex', function (done) { var router = new Router() router.add(['/by/id', /\d+/], function (someId) { expect(someId).toBe('123') }) router.route('/by/id/123', function (err) { expect(err).toBe(null, err && err.message) done() }) }) it('→ regex routes can return NotFoundError', function (done) { var router = new Router() router.add(['/by/id', /\d+/], function () {}) router.route('/by/id/not-an-id', function (err) { expect(err).toBeA(NotFoundError) done() }) }) it('→ get based on regex with multiple captures global', function (done) { var router = new Router() router.add(['by', 'order', /(\d+)/g], function (one, two, three) { expect(one).toBe('1') expect(two).toBe('2') expect(three).toBe('3') }) router.route('/by/order/1-2-3', function (err) { expect(err).toBe(null, err && err.message) done() }) }) it('→ get based regex with multiple captures not global', function (done) { var router = new Router() router.add(['by', 'order', /(\d+)/], function (one, two, three) { expect(one).toBe('1') expect(two).toNotExist() expect(three).toNotExist() }) router.route('/by/order/1-2-3', function (err) { expect(err).toBe(null, err && err.message) done() }) }) it('→ pass in arguments before capturing path arguments', function (done) { var router = new Router() var api = {key: 123, any: 'object is passed', method: function () {}} router.add(['/by/order', /(\d+)/], function (inputApi, urlParam) { expect(inputApi).toBe(api) expect(urlParam).toBe('123') done() }) router.route('/by/order/123', {args: [api]}) }) function toInt(part) { var results = /^(\d+)$/.exec(part) if (results) { return parseInt(results[0], 10) } else { return null } } it('→ get based on function', function (done) { var router = new Router() router.add(['by', 'order', toInt], function (val) { expect(val).toBe(123) }) router.route('/by/order/123', function (err) { expect(err).toBe(null) done() }) }) it('→ function routes can return NotFoundError', function (done) { var router = new Router() router.add(['/by/order', toInt], function () {}) router.route('/by/order/not-found', function (err) { expect(err).toBeA(NotFoundError) done() }) }) it('→ check not found error', function (done) { var router = new Router() router.route('/not/real/id', function (err) { expect(Router.isNotFound(err)).toBe(true) done() }) }) it('→ check not found error on not real error', function () { expect(Router.isNotFound(new Error('Not the missing error'))).toBe(false) }) })