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