summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2011-05-06 09:39:52 +0200
committerfranck cuny <franck@lumberjaph.net>2011-07-26 13:19:59 +0200
commit3b98ef2a833a2bf012ee76f9ffd06242cc96b6da (patch)
treec8814fa5eaa66ac0908eef0c3855866c2c1ec509
parentnew middleware (diff)
downloadnet-http-spore-3b98ef2a833a2bf012ee76f9ffd06242cc96b6da.tar.gz
add tests for middleware Auth::Header
Signed-off-by: franck cuny <franck@lumberjaph.net>
-rw-r--r--lib/Net/HTTP/Spore/Middleware/Auth/Header.pm2
-rw-r--r--t/spore-middleware/auth-header.t70
2 files changed, 71 insertions, 1 deletions
diff --git a/lib/Net/HTTP/Spore/Middleware/Auth/Header.pm b/lib/Net/HTTP/Spore/Middleware/Auth/Header.pm
index 95cff9e..87c9619 100644
--- a/lib/Net/HTTP/Spore/Middleware/Auth/Header.pm
+++ b/lib/Net/HTTP/Spore/Middleware/Auth/Header.pm
@@ -3,7 +3,7 @@ package Net::HTTP::Spore::Middleware::Auth::Header;
 # ABSTRACT: middleware for authentication with specific header
 
 use Moose;
-extends 'Net::HTTP::Spore::Middleware::Auth::Auth';
+extends 'Net::HTTP::Spore::Middleware::Auth';
 
 has header_name => (isa => 'Str', is => 'rw', required => 1);
 has header_value => (isa => 'Str', is => 'rw', required => 1);
diff --git a/t/spore-middleware/auth-header.t b/t/spore-middleware/auth-header.t
new file mode 100644
index 0000000..a67541e
--- /dev/null
+++ b/t/spore-middleware/auth-header.t
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Try::Tiny;
+use Net::HTTP::Spore;
+
+my $header_name  = 'X-API-Auth-test';
+my $header_value = '12345';
+
+my $mock_server = {
+    '/show' => sub {
+        my $req  = shift;
+        my $auth = $req->header($header_name);
+        if ( $auth && $auth eq $header_value ) {
+            $req->new_response( 200, [ 'Content-Type' => 'text/plain' ], 'ok' );
+        }
+        else {
+            $req->new_response( 403, [ 'Content-Type' => 'text/plain' ],
+                'not ok' );
+        }
+      }
+};
+
+my @tests = (
+    {
+        middlewares => [ [ 'Mock', tests => $mock_server ] ],
+        expected => { status => 403, body => 'not ok' }
+    },
+    {
+        middlewares => [
+            [
+                'Auth::Header',
+                header_name  => $header_name,
+                header_value => $header_value
+            ],
+            [ 'Mock', tests => $mock_server ],
+        ],
+        expected => { status => 200, body => 'ok' }
+    },
+    {
+        middlewares => [
+            [
+                'Auth::Header',
+                header_name  => $header_name,
+                header_value => 'foo'
+            ],
+            [ 'Mock', tests => $mock_server ],
+        ],
+        expected => { status => 403, body => 'not ok' }
+    },
+);
+
+plan tests => 2 * @tests;
+
+foreach my $test (@tests) {
+    my $client =
+      Net::HTTP::Spore->new_from_spec( 't/specs/api.json',
+        base_url => 'http://localhost/' );
+    foreach ( @{ $test->{middlewares} } ) {
+        $client->enable(@$_);
+    }
+
+    my $res;
+
+    try { $res = $client->get_info(); } catch { $res = $_ };
+
+    is $res->status, $test->{expected}->{status}, 'valid HTTP status';
+    is $res->body,   $test->{expected}->{body},   'valid HTTP body';
+}