t/unit/Plack/Middleware/EnvObject/call.t
#!/usr/bin/env perl
use Modern::Perl '2013';
use Test2::V0;
use Test2::Tools::Spec;
use Plack::Builder;
use Plack::Test;
use HTTP::Request;
use Scalar::Util qw<reftype refaddr>;
use Plack::Middleware::EnvObject;
describe 'envobject' => sub {
my $testobj = bless( {}, 't::unit::Plack::Middleware::EnvObject::call::test_object' );
my $psgi_app = sub {
my $env = shift;
my $envobj = $env->{'test_envobject'};
if ( ( reftype($envobj) eq reftype($testobj) ) and ( refaddr($envobj) == refaddr($testobj) ) ) {
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'objects equal' ] ];
}
else {
return [ 500, [ 'Content-Type' => 'text/plain' ], [ 'objects not equal' ] ];
}
};
tests 'no name' => sub {
my $wrapped = builder {
enable 'EnvObject';
$psgi_app;
};
my $plack = Plack::Test->create($wrapped);
my $res = $plack->request( HTTP::Request->new( 'GET' => '/' ) );
ok( ( ( $res->code == 500 ) and ( $res->content =~ m/^No name passed to EnvObject/ ) ), 'fails with no name' );
};
tests 'no object' => sub {
my $wrapped = builder {
enable 'EnvObject', name => 'test_envobject';
$psgi_app;
};
my $plack = Plack::Test->create($wrapped);
my $res = $plack->request( HTTP::Request->new( 'GET' => '/' ) );
ok( ( ( $res->code == 500 ) and ( $res->content =~ m/^No object passed to EnvObject/ ) ), 'fails with no object' );
};
tests 'ok' => sub {
my $wrapped = builder {
enable 'EnvObject', name => 'test_envobject', object => $testobj;
$psgi_app;
};
my $plack = Plack::Test->create($wrapped);
my $res = $plack->request( HTTP::Request->new( 'GET' => '/' ) );
ok( ( ( $res->code == 200 ) and ( $res->content eq 'objects equal' ) ), 'object correctly stored' );
};
};
done_testing();