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