respect psgi spec. #2

Closed
wants to merge 1 commit into from
View
11 lib/Plack/Middleware/Auth/Form.pm
@@ -6,11 +6,12 @@ use parent qw/Plack::Middleware/;
use Plack::Util::Accessor qw( secure authenticator no_login_page after_logout );
use Plack::Request;
use Scalar::Util;
+use Carp ();
sub prepare_app {
my $self = shift;
- my $auth = $self->authenticator or die 'authenticator is not set';
+ my $auth = $self->authenticator or Carp::croak 'authenticator is not set';
if (Scalar::Util::blessed($auth) && $auth->can('authenticate')) {
$self->authenticator(sub { $auth->authenticate(@_[0,1]) }); # because Authen::Simple barfs on 3 params
} elsif (ref $auth ne 'CODE') {
@@ -46,7 +47,7 @@ sub _login {
my $secure_url = "https://$server" . $env->{PATH_INFO};
return [
301,
- { Location => $secure_url },
+ [ Location => $secure_url ],
[ "<html><body><a href=\"$secure_url\">Need a secure connection</a></body></html>" ]
];
}
@@ -73,7 +74,7 @@ sub _login {
URI->new( $redir_to )->path eq $env->{PATH_INFO};
return [
302,
- { Location => $redir_to },
+ [ Location => $redir_to ],
[ "<html><body><a href=\"$redir_to\">Back</a></body></html>" ]
];
}
@@ -91,7 +92,7 @@ sub _login {
else{
return [
200,
- { 'Content-Type' => 'text/html', },
+ [ 'Content-Type' => 'text/html', ],
[ "<html><body>$form\nAfter login: $env->{'psgix.session'}{redir_to}</body></html>" ]
];
}
@@ -125,7 +126,7 @@ sub _logout {
}
return [
303,
- { Location => $self->after_logout || '/' },
+ [ Location => $self->after_logout || '/' ],
[ "<html><body><a href=\"/\">Home</a></body></html>" ]
];
}
View
81 t/unit.t
@@ -5,78 +5,101 @@ use Test::More;
use Data::Dumper;
use Plack::Middleware::Auth::Form;
+use Plack::Middleware::Lint;
+use HTTP::Message::PSGI;
+use HTTP::Request;
+use Plack::Util;
-
-my $get_req = {
- PATH_INFO => '/login',
- REQUEST_METHOD => 'GET',
- HTTP_REFERER => '/from_page',
-};
+my $get_req = HTTP::Request->new(
+ 'GET' => 'http://localhost/login', [
+ Referer => '/from_page',
+ ],
+)->to_psgi();
my $input = 'username=joe&password=pass1';
open( my $input_fh, '<', \$input ) or die $!;
-my $post_req = {
- PATH_INFO => '/login',
- REQUEST_METHOD => 'POST',
- CONTENT_TYPE => 'application/x-www-form-urlencoded',
- CONTENT_LENGTH => length( $input ),
+my $post_req = +{
+ %{ HTTP::Request->new(
+ POST => 'http://localhost/login',
+ [
+ 'Content-Type' => 'application/x-www-form-urlencoded',
+ 'Content-Length' => length($input),
+ ],
+ )->to_psgi(),
+ },
'psgi.input' => $input_fh,
'psgix.session' => { redir_to => '/landing_page' },
+ 'psgi.version' => [1,0],
};
my $middleware = Plack::Middleware::Auth::Form->new( authenticator => sub { 1 } );
-my $res = $middleware->call( $get_req );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+my $res = $middleware->( $get_req );
like( join( '', @{ $res->[2] } ), qr/form id="login_form"/, '/login with login form' );
is( $get_req->{'psgix.session'}{redir_to}, '/from_page' );
-$res = $middleware->call( $post_req );
-is( $res->[1]{Location}, '/landing_page', 'Redirection after login' ) or warn Dumper($res);
+$res = $middleware->( $post_req );
+is( Plack::Util::header_get($res->[1], 'Location'), '/landing_page', 'Redirection after login' ) or warn Dumper($res);
is( $post_req->{'psgix.session'}{user_id}, 'joe', 'Username saved in the session' );
is( $post_req->{'psgix.session'}{redir_to}, undef, 'redir_to removed after usage' );
ok( !exists $post_req->{'psgix.session'}{remember} );
$post_req->{'psgix.session'}{redir_to} = '/new_landing_page';
$middleware = Plack::Middleware::Auth::Form->new( authenticator => sub { { user_id => 1 } } );
-$res = $middleware->call( $post_req );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+$res = $middleware->( $post_req );
is( $post_req->{'psgix.session'}{user_id}, '1', 'User id saved in the session' );
$middleware = Plack::Middleware::Auth::Form->new( authenticator => sub { 0 } );
-$res = $middleware->call( $post_req );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+$res = $middleware->( $post_req );
like( join( '', @{ $res->[2] } ), qr/error.*form id="login_form"/, 'login form for login error' );
$post_req->{'psgix.session'}{user_id} = '1';
$post_req->{PATH_INFO} = '/logout';
-$middleware = Plack::Middleware::Auth::Form->new( after_logout => '/after_logout' );
-$res = $middleware->call( $post_req );
+$middleware = Plack::Middleware::Auth::Form->new( after_logout => '/after_logout', authenticator => sub { 0 } );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+$res = $middleware->( $post_req );
ok( !exists( $post_req->{'psgix.session'}{user_id} ), 'User logged out' );
-is( $res->[1]{Location}, '/after_logout', 'Redirection after logout' );
+is( Plack::Util::header_get($res->[1], 'Location'), '/after_logout', 'Redirection after logout' );
$middleware = Plack::Middleware::Auth::Form->new(
- app => sub { [ 200, {}, [ 'aaa' . $_[0]->{SimpleLoginForm} ] ] },
+ app => sub { [ 200, [], [ 'aaa' . $_[0]->{SimpleLoginForm} ] ] },
no_login_page => 1,
+ authenticator => sub { 0 },
);
-$res = $middleware->call( $get_req );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+$res = $middleware->( $get_req );
like( join( '', @{ $res->[2] } ), qr/form id="login_form"/, 'login form passed' );
like( join( '', @{ $res->[2] } ), qr/^aaa/, 'app login page used' );
$input = 'username=joe&password=pass1&remember=1';
open( $input_fh, '<', \$input ) or die $!;
-$post_req = {
- PATH_INFO => '/login',
- REQUEST_METHOD => 'POST',
- CONTENT_TYPE => 'application/x-www-form-urlencoded',
- CONTENT_LENGTH => length( $input ),
+$post_req = +{
+ %{
+ HTTP::Request->new(
+ POST => 'http://localhost/login' => [
+ 'Content-Type' => 'application/x-www-form-urlencoded',
+ 'Content-Length' => length( $input ),
+ ]
+ )->to_psgi()
+ },
'psgi.input' => $input_fh,
'psgix.session' => { redir_to => '/landing_page' },
};
-$middleware = Plack::Middleware::Auth::Form->new( authenticator => sub { 1 }, app => sub {} );
-$res = $middleware->call( $post_req );
+$middleware = Plack::Middleware::Auth::Form->new( authenticator => sub { 1 }, app => sub {
+ +[
+ 200, [], []
+ ]
+} );
+$middleware = Plack::Middleware::Lint->wrap($middleware);
+$res = $middleware->( $post_req );
ok( $post_req->{'psgix.session'}{remember}, 'Remeber saved on session' );
is( $post_req->{'psgix.session'}{user_id}, 'joe', 'Username saved in the session' );
$get_req->{PATH_INFO} = '/some_page';
$get_req->{'psgix.session'}{remember} = 1;
-$res = $middleware->call( $get_req );
+$res = $middleware->( $get_req );
ok( $get_req->{'psgix.session.options'}{expires} > 10000, 'Long session' );