Fixing iron:router issue with latest Chrome version

This commit is contained in:
Erik Ziegler committed 2016-06-07 19:32:30 +02:00
1 parent 3734b5f31a
commit 6f23c7163a
20 files changed
+1074 -4

No files matched your search

@@ -0,0 +1,54 @@
Tinytest.add('MiddlewareStack - handler basics', function (test) {
var handler;
var fn = function myName () {};
var opts = {};
// constructor options
// middleware handler
handler = new Handler(fn);
test.equal(handler.handle, fn);
test.equal(handler.name, 'myName', 'name is "myName"');
test.equal(handler.where, 'client');
test.isTrue(handler.test('/'));
test.isTrue(handler.test('/match/everything'));
opts.name = 'newName';
handler = new Handler('/items/:id', fn, opts);
test.equal(handler.handle, fn);
test.equal(handler.name, 'newName', 'name is "newName"');
test.equal(handler.options, opts);
test.isTrue(handler.test('/items/1'));
test.isTrue(handler.test('/items/2'));
handler = new Handler('/items/:id?', fn, opts);
test.isTrue(handler.test('/items/1'));
test.isTrue(handler.test('/items'));
handler = new Handler(/.*/, fn, opts);
test.isTrue(handler.test('/'));
test.isTrue(handler.test('/foo'));
var called = false;
var thisArg = {
methodName: function () {called = true;}
};
handler = new Handler('/items/:id', 'methodName');
handler.handle.call(thisArg);
test.isTrue(called);
handler = new Handler('/items/:id', fn);
var params = handler.params('/items/5');
test.equal(params.id, "5");
});
Tinytest.add('Handler - test', function (test) {
var handler = new Handler('/testme', function () {}, {
where: 'server',
method: 'GET'
});
test.isTrue(handler.test('/testme', {where: 'server', method: 'GET'}));
test.isFalse(handler.test('/testme', {where: 'client', method: 'GET'}));
test.isFalse(handler.test('/testme', {where: 'server', method: 'POST'}));
});
@@ -0,0 +1,182 @@
Tinytest.add('MiddlewareStack - handler names and paths', function (test) {
var handler;
// path is a name
handler = new Handler('home', {});
test.equal(handler.name, 'home', 'name is "home"');
test.equal(handler.path, '/home', 'path is "/home"');
// path is an option
handler = new Handler('home', {path: '/foo'});
test.equal(handler.name, 'home', 'name is "home"');
test.equal(handler.path, '/foo', 'path is "/foo"');
handler = new Handler('/home', {path: '/bar'});
test.equal(handler.path, '/bar', 'path is "/bar"');
handler = new Handler('/home', {path: '/bar', name: 'foo'});
test.equal(handler.path, '/bar', 'path is "/bar"');
test.equal(handler.name, 'foo', 'name is "foo"');
});
Tinytest.add('MiddlewareStack - create and find by name', function (test) {
// basically just test that a handler gets created and keyed by name if thee's
// a name. Also test duplicate named handlers throws an error.
var stack = new Iron.MiddlewareStack;
stack._create('/items', function () {}, {name: 'items'});
test.isTrue(stack.findByName('items'));
test.throws(function () {
// same name
stack._create('/items', function () {}, {name: 'items'});
});
});
Tinytest.add('MiddlewareStack - push', function (test) {
var stack = new Iron.MiddlewareStack;
var fns = [function () {}, function () {}];
stack.push(fns[0]);
test.equal(stack._stack[0].handle, fns[0]);
stack.push(fns[1]);
test.equal(stack._stack[1].handle, fns[1]);
});
Tinytest.add('MiddlewareStack - insertAt', function (test) {
var stack = new Iron.MiddlewareStack;
var fns = [function () {}, function () {}, function () {}];
stack.push(fns[0]);
stack.push(fns[2]);
stack.insertAt(1, fns[1]);
test.equal(stack._stack[1].handle, fns[1]);
});
Tinytest.add('MiddlewareStack - insertBefore', function (test) {
var stack = new Iron.MiddlewareStack;
var fns = [function one() {}, function two() {}, function three() {}];
stack.push(fns[0]);
stack.push(fns[2]);
stack.insertBefore('three', fns[1]);
test.equal(stack._stack[1].handle, fns[1]);
});
Tinytest.add('MiddlewareStack - insertAfter ', function (test) {
var stack = new Iron.MiddlewareStack;
var fns = [function one() {}, function two() {}, function three() {}];
stack.push(fns[0]);
stack.push(fns[2]);
stack.insertAfter('one', fns[1]);
test.equal(stack._stack[1].handle, fns[1]);
});
Tinytest.add('MiddlewareStack - dispatch iteration with this.next', function (test) {
var stack = new Iron.MiddlewareStack;
var calls = [];
if (Meteor.isClient) {
stack.push(function m1 () {
calls.push('m1');
this.next();
});
stack.push(function m2 () {
calls.push('m2');
// no call to next
});
stack.push(function m3 () {
calls.push('m3');
});
stack.dispatch('/', {});
test.equal(calls.length, 2, "call length is two");
test.equal(calls[0], 'm1', "m1 called");
test.equal(calls[1], 'm2', "m2 called");
}
if (Meteor.isServer) {
stack.push(function m1 () {
calls.push('m1');
this.next();
}, {where: 'server'});
stack.push(function m2 () {
calls.push('m2');
// no call to next
}, {where: 'server'});
stack.push(function m3 () {
calls.push('m3');
}, {where: 'server'});
stack.dispatch('/', {});
test.equal(calls.length, 2, "call length is two");
test.equal(calls[0], 'm1', "m1 called");
test.equal(calls[1], 'm2', "m2 called");
}
});
Tinytest.add('MiddlewareStack - dispatch callback', function (test) {
var stack = new Iron.MiddlewareStack;
var calls = [];
if (Meteor.isClient) {
stack.push(function m1 () {
calls.push('m1');
this.next();
});
stack.dispatch('/', {}, function () {
calls.push('done');
});
test.equal(calls.length, 2, "call length is two");
test.equal(calls[0], 'm1', "m1 called");
test.equal(calls[1], 'done', "done called");
}
if (Meteor.isServer) {
stack.push(function m1 () {
calls.push('m1');
this.next();
}, {where: 'server'});
stack.dispatch('/', {}, function () {
calls.push('done');
});
test.equal(calls.length, 2, "call length is two");
test.equal(calls[0], 'm1', "m1 called");
test.equal(calls[1], 'done', "done called");
}
});
if (Meteor.isServer) {
var Fiber = Npm.require('fibers');
Tinytest.addAsync('MiddlewareStack - async next maintains fibers', function (test, done) {
var envVar = new Meteor.EnvironmentVariable;
envVar.withValue(true, function () {
var stack = new Iron.MiddlewareStack;
test.isTrue(envVar.getOrNullIfOutsideFiber());
stack.push(function(req, res, next) {
// break out of the current fiber
setTimeout(function() {
next();
}, 0);
}, {where: 'server'});
stack.push(function(req, res, next) {
test.isTrue(envVar.getOrNullIfOutsideFiber());
this.next();
}, {where: 'server'});
stack.dispatch('/', {}, function () {
test.isTrue(envVar.getOrNullIfOutsideFiber());
done();
});
});
});
}
@@ -0,0 +1,194 @@
Tinytest.add('MiddlewareStack - dispatch', function (test) {
var stack = new Iron.MiddlewareStack;
var calls = {};
var call = function (name) {
calls[name] = calls[name] || 0;
calls[name]++;
};
// client middleware
stack.push(function m1 (req, res, next) {
call('m1');
next();
});
stack.push(function m2 (req, res, next) {
call('m2');
this.next();
});
// client handlers
var params;
stack.push('/items/:id', function item (req, res, next) {
call('item');
params = this.params;
next();
});
// same path is okay as long as name is different
stack.push('/items/:id', function itemTwo (req, res, next) {
call('item2');
});
// server handler
stack.push('/server', function server (req, res, next) {
call('server');
}, { where: 'server' });
var thisArg = {};
if (Meteor.isClient) {
stack.dispatch('/', {});
test.equal(calls.m1, 1, 'm1 not called');
test.equal(calls.m2, 1, 'm2 not called');
test.isFalse(calls.item);
test.isFalse(calls.item2);
test.isFalse(calls.server);
stack.dispatch('/items/1', thisArg);
test.equal(calls.m1, 2, 'm1 not called');
test.equal(calls.m2, 2, 'm2 not called');
test.equal(calls.item, 1, 'item not called');
test.equal(calls.item2, 1, 'item2 not called');
test.isFalse(calls.server);
var params = thisArg.params;
test.equal(params.id, "1");
stack.onServerDispatch(function () {
call('serverDispatch');
});
stack.dispatch('/server', {});
test.equal(calls.serverDispatch, 1);
}
if (Meteor.isServer) {
stack.dispatch('/server', {});
test.isFalse(calls.m1);
test.isFalse(calls.m2);
test.equal(calls.server, 1);
}
});
Tinytest.add('MiddlewareStack - dispatch error handling', function (test) {
if (Meteor.isClient) {
var stack = new Iron.MiddlewareStack;
var calls = [];
stack.push(function (req, res, next) {
calls.push(1);
throw new Error('test');
});
stack.push(function (req, res, next) {
calls.push(2);
next();
});
stack.dispatch('/', {
next: function (err) {
calls.push(3);
test.equal(err.message, 'test');
}
});
test.equal(calls.length, 2);
// first fn throws an error
test.equal(calls[0], 1);
// rest of middleware skipped
test.equal(calls[1], 3);
}
});
Tinytest.add('MiddlewareStack - mounting paths', function (test) {
var stack = new Iron.MiddlewareStack;
var calls = [];
if (Meteor.isClient) {
// add a mounted handler at mountpath /foo
stack.push('/foo', function (req, res, next) {
calls.push({args: EJSON.clone(arguments), thisArg: EJSON.clone(this)});
next();
}, {mount: true});
// add a regular handler at /foo/bar/baz
stack.push('/foo/bar/baz', function (req, res, next) {
calls.push({args: EJSON.clone(arguments), thisArg: EJSON.clone(this)});
});
stack.dispatch('/foo/bar/baz', {});
// make sure it got called
test.equal(calls.length, 2);
// this.url
test.equal(calls[0].thisArg.url, '/bar/baz', 'url wrong on controller');
test.equal(calls[0].thisArg.originalUrl, '/foo/bar/baz', 'originalUrl wrong on controller');
// fn (req, res, next)
// req.url
test.equal(calls[0].args[0].url, '/bar/baz', 'url wrong on req');
test.equal(calls[0].args[0].originalUrl, '/foo/bar/baz', 'originalUrl wrong on req');
// now make sure the url is set back to the originalUrl for non mounted
// handlers.
test.equal(calls[1].thisArg.url, '/foo/bar/baz');
test.equal(calls[1].thisArg.originalUrl, '/foo/bar/baz');
// fn (req, res, next)
// req.url
test.equal(calls[1].args[0].url, '/foo/bar/baz');
test.equal(calls[1].args[0].originalUrl, '/foo/bar/baz');
}
if (Meteor.isServer) {
stack.push('/foo', function (req, res, next) {
calls.push({args: EJSON.clone(arguments), thisArg: EJSON.clone(this)});
next();
}, {mount: true, where: 'server'});
// add a regular handler at /foo/bar/baz
stack.push('/foo/bar/baz', function (req, res, next) {
calls.push({args: EJSON.clone(arguments), thisArg: EJSON.clone(this)});
}, {where: 'server'});
stack.dispatch('/foo/bar/baz', {});
// make sure it got called
test.equal(calls.length, 2);
// this.url
test.equal(calls[0].thisArg.url, '/bar/baz');
test.equal(calls[0].thisArg.originalUrl, '/foo/bar/baz');
// fn (req, res, next)
// req.url
test.equal(calls[0].args[0].url, '/bar/baz');
test.equal(calls[0].args[0].originalUrl, '/foo/bar/baz');
// now make sure the url is set back to the originalUrl for non mounted
// handlers.
test.equal(calls[1].thisArg.url, '/foo/bar/baz');
test.equal(calls[1].thisArg.originalUrl, '/foo/bar/baz');
// fn (req, res, next)
// req.url
test.equal(calls[1].args[0].url, '/foo/bar/baz');
test.equal(calls[1].args[0].originalUrl, '/foo/bar/baz');
}
});
Tinytest.add('MiddlewareStack - append with options', function (test) {
var fn1 = function () {};
var fn2 = function () {};
var stack = new Iron.MiddlewareStack;
stack.append(fn1, fn2, {where: 'server'});
test.equal(stack._stack[0].where, 'server');
test.equal(stack._stack[1].where, 'server');
});
//TODO concat
//TODO append