From 3c44b38e0b997aefecf67a3234052a3611fb06c0 Mon Sep 17 00:00:00 2001 From: vti Date: Sun, 9 Nov 2008 15:00:45 +0100 Subject: [PATCH] Version 0.02 - Context class - DetectLang example filter - More tests --- MojoX-Dispatcher-FilterChain/Changes | 7 ++++- MojoX-Dispatcher-FilterChain/Makefile.PL | 1 + .../lib/MojoX/Dispatcher/FilterChain.pm | 20 ++++++-------- MojoX-Dispatcher-FilterChain/t/03chain.t | 31 +++++++++++++++++----- MojoX-Dispatcher-FilterChain/t/detect-lang.t | 3 ++- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/MojoX-Dispatcher-FilterChain/Changes b/MojoX-Dispatcher-FilterChain/Changes index 26dfe24..617cb5f 100644 --- a/MojoX-Dispatcher-FilterChain/Changes +++ b/MojoX-Dispatcher-FilterChain/Changes @@ -1,5 +1,10 @@ Revision history for MojoX-Dispatcher-FilterChain -0.01 2008-11-12 00:00::00 +0.02 2008-11-09 00:00::00 + - Context class + - DetectLang example filter + - More tests + +0.01 2008-11-08 00:00::00 First version. diff --git a/MojoX-Dispatcher-FilterChain/Makefile.PL b/MojoX-Dispatcher-FilterChain/Makefile.PL index 342791c..69e1d7f 100644 --- a/MojoX-Dispatcher-FilterChain/Makefile.PL +++ b/MojoX-Dispatcher-FilterChain/Makefile.PL @@ -12,6 +12,7 @@ WriteMakefile( : ()), PL_FILES => {}, PREREQ_PM => { + 'Mojo' => 0, 'Test::More' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, diff --git a/MojoX-Dispatcher-FilterChain/lib/MojoX/Dispatcher/FilterChain.pm b/MojoX-Dispatcher-FilterChain/lib/MojoX/Dispatcher/FilterChain.pm index 7468711..753e993 100644 --- a/MojoX-Dispatcher-FilterChain/lib/MojoX/Dispatcher/FilterChain.pm +++ b/MojoX-Dispatcher-FilterChain/lib/MojoX/Dispatcher/FilterChain.pm @@ -7,14 +7,14 @@ use base 'Mojo::Base'; use MojoX::Dispatcher::FilterChain::Constants; -our $VERSION = '0.01'; +our $VERSION = '0.02'; use constant DEBUG => $ENV{MOJOX_DISPATCHER_FILTERCHAIN_DEBUG} || 0; -__PACKAGE__->attr('filters', default => sub {[]}); +__PACKAGE__->attr('filters', default => sub { [] }); sub add { - my $self = shift; + my $self = shift; my $filter = shift; my $name = ref $filter; @@ -29,18 +29,14 @@ sub process { foreach my $filter (@{$self->filters}) { my $name = ref $filter; - if ($filter->can('run')) { - warn "Running '$name' filter" if DEBUG; + warn "Running '$name' filter" if DEBUG; - my $proceed = $filter->run(@_); + my $proceed = $filter->run(@_); - warn "Stop filter chain after '$name' filter" - if DEBUG and $proceed == LAST; + warn "Stop filter chain after '$name' filter" + if DEBUG and $proceed == LAST; - last if $proceed == LAST; - } else { - warn "Filter '$name' has no 'run' method, skipping." if DEBUG; - } + last if $proceed == LAST; } } diff --git a/MojoX-Dispatcher-FilterChain/t/03chain.t b/MojoX-Dispatcher-FilterChain/t/03chain.t index fab023d..022046e 100644 --- a/MojoX-Dispatcher-FilterChain/t/03chain.t +++ b/MojoX-Dispatcher-FilterChain/t/03chain.t @@ -3,34 +3,53 @@ use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 4; use_ok('MojoX::Dispatcher::FilterChain'); +use_ok('MojoX::Dispatcher::FilterChain::Context'); -my $c = 0; +my $c = MojoX::Dispatcher::FilterChain::Context->new(); my $chain = MojoX::Dispatcher::FilterChain->new(); -$chain->add(Filter1->new()); -$chain->add(Filter2->new()); +$chain->add(FilterFirst->new()); +$chain->add(FilterIntercept->new()); +$chain->add(FilterLast->new()); + $chain->process($c); +is($c->stash->{foo}, 'bar'); +ok(not defined $c->stash->{last}); + 1; -package Filter1; +package FilterFirst; use base 'MojoX::FilterChain::Base'; use MojoX::FilterChain::Constants; sub run { + my ($self, $c) = @_; + $c->stash->{foo} = 'bar'; return NEXT; } 1; -package Filter2; +package FilterIntercept; +use base 'MojoX::FilterChain::Base'; +use MojoX::FilterChain::Constants; + +sub run { + return LAST; +} +1; + +package FilterLast; use base 'MojoX::FilterChain::Base'; use MojoX::FilterChain::Constants; sub run { + my ($self, $c) = @_; + $c->stash->{last} = 1; return LAST; } 1; diff --git a/MojoX-Dispatcher-FilterChain/t/detect-lang.t b/MojoX-Dispatcher-FilterChain/t/detect-lang.t index 7128d05..c7926c3 100644 --- a/MojoX-Dispatcher-FilterChain/t/detect-lang.t +++ b/MojoX-Dispatcher-FilterChain/t/detect-lang.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 11; use_ok('MojoX::FilterChain::DetectLang'); @@ -21,6 +21,7 @@ $chain->add(MojoX::FilterChain::DetectLang->new(languages => [qw/ en de /])); $c->tx(Mojo::Transaction->new_get('http://example.com')); $chain->process($c); ok(not defined $c->stash->{language}); +is($c->tx->req->url, 'http://example.com'); # no language $c->tx(Mojo::Transaction->new_get('http://example.com/stuff')); -- 2.11.4.GIT