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