HTTP basic auth
[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 => 13;
7 use Test::MockObject::Extends;
8 use Test::MockObject;
9 use HTTP::Headers;
10
11
12 my $m; BEGIN { use_ok($m = "Catalyst::Plugin::Authentication::Credential::HTTP") }
13
14 can_ok( $m, "authenticate_http" );
15 can_ok( $m, "authorization_required" );
16 can_ok( $m, "authorization_required_response" );
17
18 my $req = Test::MockObject->new;
19 my $req_headers = HTTP::Headers->new;
20
21 $req->set_always( headers => $req_headers );
22
23 my $res = Test::MockObject->new;
24
25 my $status;
26 $res->mock(status => sub { $status = $_[1] });
27
28 my $res_headers = HTTP::Headers->new;
29 $res->set_always( headers => $res_headers );
30
31 my $c = Test::MockObject::Extends->new( $m );
32
33 my @login_info;
34 $c->mock( login => sub { shift; @login_info = @_; 1 } );
35 $c->set_false( "detach" );
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 ok( $c->authorization_required, "authorization required with successful authentication");
49 ok( !$c->called("detach"), "didnt' detach");
50
51 $req_headers->clear;
52 $c->clear;
53
54 ok( !$c->authorization_required, "authorization required with bad authentication");
55 $c->called_ok("detach", "detached");
56
57 is( $status, 401, "401 status code" );
58 like( $res_headers->www_authenticate, qr/^Basic/, "WWW-Authenticate header set");