Exemplo n.º 1
0
    ConstructedRoute.prerequisites = (function ( prerequisites ) {

        var stack = new Stack();

        if ( prerequisites ) {
            if( Util.isnt.Array( prerequisites ) ) {
                prerequisites = [ prerequisites ];
            }

            var PREREQUISITES = router.PREREQUISITES;

            Util.Array.each( prerequisites, function ( pr ) {

                if( Util.is.Function( pr ) ) {
                    stack.queue( pr );
                    return;
                }

                if( Util.is.String( pr ) && Util.is.Function( PREREQUISITES[ pr ] ) ) {
                    stack.queue( PREREQUISITES[ pr ] );
                    return;
                }

                throw new Error( "Invalid prerequisites supplied..." );

            });
        }

        return stack;

    })( options.prerequisites );
Exemplo n.º 2
0
    describe( ".[method]", function () {


        Util.Array.each( Route.METHODS, function ( method ) {
            var methodName = method.toLowerCase();

            describe( "." + methodName, function () {

                it( "should present an error if no url is specified", function () {
                    expect(function () {
                        router[ methodName ]();
                    }).to.throw( TypeError, "first argument should be a String or Regexp" );
                });

                it( "should present an error if no function is specified", function () {
                    expect(function () {
                        router[ methodName ]( '/some/url' );
                    }).to.throw( TypeError, "It seems that you didn't provided any function" );
                });

            });

        });

    });
Exemplo n.º 3
0
describe( "support connect", function () {
    var app, router;

    beforeEach(function () {
        var _ = helper();

        app = _.app;
        router = _.router;
    });

    Util.Array.each( Route.METHODS, function ( method, i ) {
        var methodName = method.toLowerCase();

        describe( "router." + methodName, function () {

            it( "should work with a single function", function ( done ) {
                router[ methodName ]( '/', function ( req, res, next ) {
                    res.statusCode = 200;
                    res.write( req.method );
                    res.end();
                });

                request( app )
                    [ methodName ]( '/' )
                    .expect( 200, method, done );
            });

            it( "shouldn't work with different method", function ( done ) {
                var reqMethod = Route.METHODS[ Route.METHODS.length === i + 1 ? 0 : i + 1 ],
                reqMethodName = reqMethod.toLowerCase();

                router[ methodName ]( '/', function ( req, res, next ) {
                    res.statusCode = 200;
                    res.write( req.method );
                    res.end();
                });

                request( app )
                    [ reqMethodName ]( '/' )
                    .expect( 404, 'NotFoundError: No route was matched', done );
            });

        });

    });

});
Exemplo n.º 4
0
                .then(function ( value ) {
                    if( Util.is.String( value ) ) return value;

                    debug( "trying to dispatch params stacks" );

                    if( ! route.params ) {
                        debug( "there aren't params on this route instance" );
                        return;
                    }

                    // Hey honey
                    // It seems that there are params, we must check, for each param,
                    // if there is a stack available to run, mix them up and execute
                    // them! Seems hard? F*cking no!

                    var stacks = [];

                    // Check for params stacks
                    for( var x in route.params ) {
                        if( router.paramsStacks[ x ] ) {
                            stacks.push( router.paramsStacks[ x ] );
                        }
                    }

                    debug( "%s stacks found", stacks.length );

                    if ( stacks.length === 0 ) {
                        debug( "giving up on params stacks" );
                        return;
                    }

                    // Giving super powers to stacks!! With promise powder!
                    var promise = Promise.cast();

                    Util.Array.each( stacks, function ( stack ) {
                        promise = promise.then(function ( value ) {
                            if( Util.is.String( value ) ) return value;
                            return stack.dispatch( req, res );
                        });
                    });

                    return promise;
                })
Exemplo n.º 5
0
Stack.prototype.dispatch = function () {

    var stack = this,
        dispatchArgs = arguments,
        context = Util.extend( [], arguments ),
        promise = Promise.cast().bind( context );

    Util.Array.each( stack, function ( fn ) {
        promise = promise.then(function ( value ) {

            // If some string or object has been passed, break stack
            if ( stack.options.breakOn && stack.options.breakOn( value ) ) {
                return value;
            }

            var fulfill, reject, next,
                args = Util.extend( [], context );

            args.push( next = function ( value ) {
                if ( Util.is.Error( value ) ) {
                    reject( value );
                    return;
                }

                fulfill( value );
            });

            return new Promise(function () {
                fulfill = arguments[0];
                reject = arguments[1];

                var result = fn.apply( this, args );

                if( result || dispatchArgs.length === fn.params.length ) {
                    next( result );
                }
            });
        });
    });

    return promise;
};
Exemplo n.º 6
0
Stack.prototype.queue = function ( fn ) {

    if( Util.is.Array( fn ) ) {
        Util.Array.each( fn, this.queue, this );
        return this;
    }

    if( Util.isnt.Function( fn ) ) {
        throw new TypeError( "fn should be a function" );
    }

    debug( "queueing function " + ( fn.name || 'anonymous' ) );

    // Save the params that fn accepts
    fn.params = Util.Function.getParamNames( fn );

    this.push( fn );

    return this;
};
Exemplo n.º 7
0
Router.prototype.addRoutes = function ( routesOptions ) {
    return Util.Array.map( routesOptions, this.addRoute, this );
};
Exemplo n.º 8
0
    // If there aren't step functions, throw error
    if( options.stack.length === 0 ) {
        throw new TypeError( "It seems that you didn't provided any function" );
    }

    return this.addRoute( options );
};

// Inject function proxying
Util.Array.each( Route.METHODS, function ( method ) {

    /*
        http method will be always uppercase, so we need to lower case it to be
        presented as a good method name
    */
    var methodName = method.toLowerCase();

    Router.prototype[ methodName ] = function () {
        return methodConstruction.call( this, method, arguments );
    };

});

Router.prototype.param = function ( param ) {

    if( Util.isnt.String( param ) ) {
        throw new TypoError( "You must provide the param name as first argument" );
    }

    var fns = [];