1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
use Mojo::IOLoop;
use Test::More;
use Test::Mojo;
use Mojo::ByteStream 'b';
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port; # Test server
plan tests => 45;
# Lite app
use Mojolicious::Lite;
# Silence
app->log->level('error');
plugin 'basic_auth';
get '/user-pass' => sub {
my $self = shift;
#return $self->render_text('denied')
return $self->render(text => 'authorized')
if $self->basic_auth(realm => username => 'password');
$self->render_text('denied');
};
get '/user-pass-with-colon-password' => sub {
my $self = shift;
return $self->render(text => 'authorized')
if $self->basic_auth(realm => username => 'pass:word');
$self->render_text('denied');
};
get '/pass' => sub {
my $self = shift;
return $self->render_text('denied')
unless $self->basic_auth(realm => 'password');
$self->render_text('authorized');
};
# Entered user/pass supplied to callback
get '/get-auth-callback' => sub {
my $self = shift;
return $self->render_text('authorized')
if $self->basic_auth(
realm => sub { return "@_" eq 'username password' });
$self->render_text('denied');
};
# Callback with colon in password
get '/get-auth-callback-with-colon-password' => sub {
my $self = shift;
return $self->render_text('authorized')
if $self->basic_auth(
realm => sub { return "@_" eq 'username pass:word' });
return $self->render_text('denied');
};
under sub {
my $self = shift;
return $self->basic_auth(
realm => sub { return "@_" eq 'username password' });
};
get '/under-bridge' => sub {
shift->render(text => 'authorized');
};
# Tests
my $t = Test::Mojo->new;
my $encoded;
# Failures #
foreach (
qw(
/user-pass
/pass
/get-auth-callback
)
)
{
# No user/pass
$t->get_ok($_)->status_is(401)
->header_is('WWW-Authenticate' => 'Basic realm=realm')
->content_is('denied');
# Incorrect user/pass
$encoded = b('bad:auth')->b64_encode->to_string;
chop $encoded;
$t->get_ok($_, {Authorization => "Basic $encoded"})->status_is(401)
->header_is('WWW-Authenticate' => 'Basic realm=realm')
->content_is('denied');
}
# Under bridge fail
diag '/under-bridge';
$encoded = b("bad:auth")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/under-bridge', {Authorization => "Basic $encoded"})
->status_is(401)->content_is('');
# Successes #
# Username, password
diag '/user-pass';
$encoded = b("username:password")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/user-pass', {Authorization => "Basic $encoded"})->status_is(200)
->content_is('authorized');
# Username, password with colon in password
diag '/user-pass-with-colon-password';
$encoded = b("username:pass:word")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/user-pass-with-colon-password', {Authorization => "Basic $encoded"})->status_is(200)
->content_is('authorized');
# Password only
diag '/pass';
$encoded = b(":password")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/pass', {Authorization => "Basic $encoded"})->status_is(200)
->content_is('authorized');
# With callback
diag '/get-auth-callback';
$encoded = b("username:password")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/get-auth-callback', {Authorization => "Basic $encoded"})
->status_is(200)->content_is('authorized');
# With callback and colon in password
diag '/get-auth-callback-with-colon-password';
$encoded = b("username:pass:word")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/get-auth-callback-with-colon-password', {Authorization => "Basic $encoded"})
->status_is(200)->content_is('authorized');
# Under bridge
diag '/under-bridge';
$encoded = b("username:password")->b64_encode->to_string;
chop $encoded;
$t->get_ok('/under-bridge', {Authorization => "Basic $encoded"})
->status_is(200)->content_is('authorized');
|