4 use Test::More tests => 27;
9 use_ok('Catalyst::Authentication::Credential::HTTP::Proxy');
12 my $mock_c = Test::MockObject->new;
13 $mock_c->mock('debug' => sub { 0 });
14 my ($authenticated_user, $authenticated);
15 $mock_c->mock('set_authenticated' => sub { $authenticated_user = $_[1]; $authenticated++; });
17 my ($auth_info, $user);
18 my $mock_realm = Test::MockObject->new;
19 $mock_realm->mock('find_user' => sub { $auth_info = $_[1]; return $user });
22 Catalyst::Authentication::Credential::HTTP::Proxy->new({}, $mock_c, $mock_realm);
23 } qr/Catalyst::Authentication::Credential::HTTP::Proxy/, 'No config throws';
26 Catalyst::Authentication::Credential::HTTP::Proxy->new(
27 {url => 'http://some.proxy:8080'},
30 } 'Normal (with url) ok';
33 Catalyst::Authentication::Credential::HTTP::Proxy->new(
34 {url => 'http://some.proxy:8080',
38 } qr/Catalyst::Authentication::Credential::HTTP::Proxy/, 'Asking for unknown type throws';
40 my $log = Test::MockObject->new;
41 $log->mock('info' => sub {});
42 $log->mock('debug' => sub {});
43 my $req = Test::MockObject->new;
44 my $req_headers = HTTP::Headers->new;
45 my $res = Test::MockObject->new;
46 $req->set_always( headers => $req_headers );
48 $res->mock(status => sub { $status = $_[1] });
50 $res->mock(content_type => sub { $content_type = $_[1] });
53 $res->mock(body => sub { $body = $_[1] });
54 my $res_headers = HTTP::Headers->new;
55 $res->set_always( headers => $res_headers );
56 $mock_c->set_always( debug => 0 );
57 $mock_c->set_always( config => {} );
58 $mock_c->set_always( req => $req );
59 $mock_c->set_always( res => $res );
60 $mock_c->set_always( request => $req );
61 $mock_c->set_always( response => $res );
62 $mock_c->set_always( log => $log );
64 $mock_realm->set_always(name => 'myrealm');
66 my $cred = Catalyst::Authentication::Credential::HTTP::Proxy->new(
67 {url => 'http://some.proxy:8080',
72 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with no auth headers');
74 $cred->authenticate($mock_c, $mock_realm, {});
75 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach';
77 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
78 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm");
82 $req_headers->authorization_basic( qw/Mufasa password/ );
83 my ($auth_ua, $auth_res, $auth_url);
85 no warnings qw/redefine once/;
86 *Catalyst::Authentication::Credential::HTTP::Proxy::User::get = sub { $auth_ua = shift; $auth_url = shift; $auth_res };
88 $auth_res = HTTP::Response->new;
90 $auth_res->message('FAIL');
92 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response !success');
93 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
94 is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
96 $cred->authenticate($mock_c, $mock_realm, {});
97 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';
99 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
100 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm");
103 $auth_res->code(200);
104 ($auth_url, $auth_ua) = (undef, undef);
106 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response success but no user from realm');
107 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
108 is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
109 is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
110 ok(!$authenticated, 'Not set_authenticated');
112 $cred->authenticate($mock_c, $mock_realm, {});
113 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';
115 ($auth_url, $auth_ua) = (undef, undef);
117 $user = Test::MockObject->new;
119 ok($cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns true with auth response success and user from realm');
120 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
121 is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
122 ok($authenticated, 'Called set_authenticated');
123 is("$authenticated_user", "$user", 'Called set_authenticated with user object');
125 $cred->authenticate($mock_c, $mock_realm, {});
126 } '$cred->authenticate does not detach';
127 ok(!$res_headers->header('WWW-Authenticate'), 'No authenticate header on successful auth');