summary refs log tree commit diff
path: root/t/spore
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 /t/spore
parentadd new_from_string and some tests (diff)
downloadnet-http-spore-42d3c34351474c14cef06803c18a2b4175495338.tar.gz
add enable_if feature + tests
Diffstat (limited to 't/spore')
-rw-r--r--t/spore/02_enable.t56
1 files changed, 56 insertions, 0 deletions
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';