Remove ::Plugin:: from module namespace, provide a legacy/compatibility ::Plugin...
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / basic.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More tests => 15;
5 use Test::MockObject::Extends;
6 use Test::MockObject;
7 use Test::Exception;
8 use HTTP::Headers;
9
10 my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::HTTP") }
11 can_ok( $m, "authenticate_http" );
12 can_ok( $m, "authorization_required" );
13 can_ok( $m, "authorization_required_response" );
14 my $req = Test::MockObject->new;
15 my $req_headers = HTTP::Headers->new;
16 $req->set_always( headers => $req_headers );
17 my $res = Test::MockObject->new;
18 my $status;
19 $res->mock(status => sub { $status = $_[1] });
20 my $content_type;
21 $res->mock(content_type => sub { $content_type = $_[1] });
22 my $body;
23 $res->mock(body => sub { $body = $_[1] });
24 my $res_headers = HTTP::Headers->new;
25 $res->set_always( headers => $res_headers );
26 my $c = Test::MockObject::Extends->new( $m );
27 my $cache = Test::MockObject->new;
28 $cache->mock(set => sub { shift->{$_[0]} = $_[1] });
29 $cache->mock(get => sub { return shift->{$_[0]} });
30 $c->mock(cache => sub { $cache });
31 $c->mock(debug => sub { 0 });
32 my @login_info;
33 $c->mock( login => sub { shift; @login_info = @_; 1 } );
34 $c->set_always( config => {} );
35 $c->set_always( req => $req );
36 $c->set_always( res => $res );
37
38 ok( !$c->authenticate_http, "http auth fails without header");
39 $req_headers->authorization_basic( qw/foo bar/ );
40 ok( $c->authenticate_http, "auth successful with header");
41 is_deeply( \@login_info, [qw/foo bar/], "login info delegated");
42 lives_ok {
43     $c->authorization_required
44 } "no detach on authorization required with successful authentication";
45 $req_headers->clear;
46 $c->clear;
47 throws_ok {
48     $c->authorization_required( realm => "foo" );
49 } qr/^ $Catalyst::DETACH $/x, "detached on no authorization required with bad auth";
50 is( $status, 401, "401 status code" );
51 is( $content_type, 'text/plain' );
52 is( $body, 'Authorization required.' );
53 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Digest/, "WWW-Authenticate header set: digest");
54 like( ($res_headers->header('WWW-Authenticate'))[1], qr/^Basic/, "WWW-Authenticate header set: basic");
55 like( ($res_headers->header('WWW-Authenticate'))[1], qr/realm=foo/, "WWW-Authenticate header set: basic with realm");