1use Test::More; 2use Plack::Test; 3use Plack::Builder; 4use HTTP::Request::Common; 5 6my $app = sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello $_[0]->{REMOTE_USER}" ] ] }; 7$app = builder { 8 enable "Auth::Basic", authenticator => \&cb; 9 $app; 10}; 11 12my %map = ( 13 admin => 's3cr3t', 14 john => 'foo:bar', 15); 16 17sub cb { 18 my($username, $password) = @_; 19 return $map{$username} && $password eq $map{$username}; 20} 21 22test_psgi app => $app, client => sub { 23 my $cb = shift; 24 25 my $res = $cb->(GET "http://localhost/"); 26 is $res->code, 401; 27 28 my $req = GET "http://localhost/", "Authorization" => "Basic YWRtaW46czNjcjN0"; 29 $res = $cb->($req); 30 is $res->code, 200; 31 is $res->content, "Hello admin"; 32 33 $req = GET "http://localhost/", "Authorization" => "Basic am9objpmb286YmFy"; 34 $res = $cb->($req); 35 is $res->code, 200; 36 is $res->content, "Hello john"; 37}; 38done_testing; 39 40