summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-12 16:11:06 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-12 16:11:06 +0200
commit42d3c34351474c14cef06803c18a2b4175495338 (patch)
tree343b0da4295b0dc239a98581321476732ad15143
parentadd new_from_string and some tests (diff)
downloadnet-http-spore-42d3c34351474c14cef06803c18a2b4175495338.tar.gz
add enable_if feature + tests
-rw-r--r--lib/Net/HTTP/Spore/Middleware.pm8
-rw-r--r--lib/Net/HTTP/Spore/Role/Middleware.pm36
-rw-r--r--t/spore/02_enable.t56
3 files changed, 87 insertions, 13 deletions
diff --git a/lib/Net/HTTP/Spore/Middleware.pm b/lib/Net/HTTP/Spore/Middleware.pm
index 0b8584c..6d284b5 100644
--- a/lib/Net/HTTP/Spore/Middleware.pm
+++ b/lib/Net/HTTP/Spore/Middleware.pm
@@ -18,13 +18,17 @@ sub response_cb {
 }
 
 sub wrap {
-    my ($self, @args) = @_;
+    my ($self, $cond, @args) = @_;
 
     if (!ref $self) {
         $self = $self->new(@args);
     }
+
     return sub {
-        $self->call(@_);
+        my $request = shift;
+        if ($cond->($request)) {
+            $self->call($request, @_);
+        }
     };
 }
 
diff --git a/lib/Net/HTTP/Spore/Role/Middleware.pm b/lib/Net/HTTP/Spore/Role/Middleware.pm
index dd2c1c5..60f4703 100644
--- a/lib/Net/HTTP/Spore/Role/Middleware.pm
+++ b/lib/Net/HTTP/Spore/Role/Middleware.pm
@@ -3,36 +3,50 @@ package Net::HTTP::Spore::Role::Middleware;
 use Moose::Role;
 
 has middlewares => (
-    is      => 'rw',
-    isa     => 'ArrayRef',
-    traits  => ['Array'],
-    lazy => 1,
-    default => sub { [] },
+    is         => 'rw',
+    isa        => 'ArrayRef',
+    traits     => ['Array'],
+    lazy       => 1,
+    default    => sub { [] },
     auto_deref => 1,
-    handles => { _add_middleware => 'push', _filter_middlewares => 'grep'},
+    handles    => { _add_middleware => 'push', _filter_middlewares => 'grep' },
 );
 
 sub _load_middleware {
-    my ( $self, $mw, @args ) = @_;
+    my ( $self, $mw, $cond, @args ) = @_;
 
     Class::MOP::load_class($mw);
 
-    my $code = $mw->wrap( @args );
+    my $code = $mw->wrap( $cond, @args );
     $self->_add_middleware($code);
 }
 
-sub enable {
-    my ($self, $mw, @args) = @_;
+sub _complete_mw_name {
+    my ($self, $mw) = @_;
 
     if ($mw !~ /(?:^\+|Net\:\:HTTP\:\:Spore\:\:Middleware)/) {
         $mw = "Net::HTTP::Spore::Middleware::".$mw;
     }
-    $self->_load_middleware($mw, @args);
+
+    return $mw;
+}
+
+sub enable {
+    my ($self, $mw, @args) = @_;
+
+    confess "middleware name is missing" unless $mw;
+
+    $self->enable_if(sub{1}, $mw, @args);
     $self;
 }
 
 sub enable_if {
     my ($self, $cond, $mw, @args) = @_;
+
+    confess "condition must be a code ref" if (!$cond || ref $cond ne 'CODE');
+
+    $mw = $self->_complete_mw_name($mw);
+    $self->_load_middleware($mw, $cond, @args);
     $self;
 }
 
diff --git a/t/spore/02_enable.t b/t/spore/02_enable.t
new file mode 100644
index 0000000..3f25606
--- /dev/null
+++ b/t/spore/02_enable.t
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+plan tests => 9;
+
+use IO::All;
+use Net::HTTP::Spore;
+use Net::HTTP::Spore::Request;
+
+package Net::HTTP::Spore::Middleware::Dummy;
+use base qw/Net::HTTP::Spore::Middleware/;
+
+sub call { 1 }
+
+package main;
+
+my $couchdb_spec = 't/specs/couchdb.json';
+my %args = ( api_base_url => 'http://localhost:5984', );
+my $content < io($couchdb_spec);
+
+ok my $client = Net::HTTP::Spore->new_from_spec( $couchdb_spec, %args );
+
+is scalar @{$client->middlewares}, 0, 'no middleware';
+
+dies_ok {
+    $client->enable();
+}, 'middleware name is required';
+
+dies_ok {
+    $client->enable('FOOBARBAZAWESOMEMIDDLEWARE');
+}, 'middleware should be loadable';
+
+dies_ok {
+    $client->enable_if('Format::JSON');
+}, 'enable if require a coderef';
+
+$client->enable('Dummy');
+is scalar @{$client->middlewares}, 1, 'middleware dummy added';
+
+$client->reset_middlewares();
+is scalar @{$client->middlewares}, 0, 'no middleware loaded';
+
+my $mw_test = sub { (shift)->method eq 'GET'; };
+
+my $request = Net::HTTP::Spore::Request->new({REQUEST_METHOD => 'GET'});
+
+$client->enable_if($mw_test, 'Dummy');
+
+my $res = $client->middlewares->[0]->($request);
+is $res, 1, 'condition match';
+
+$request->env->{REQUEST_METHOD} = 'POST';
+$res = $client->middlewares->[0]->($request);
+ok !$res, 'condition is not matched';