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