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