Apache2-API
view release on metacpan or search on metacpan
t/lib/Test/Apache2/Common.pm view on Meta::CPAN
$r->content_type( 'text/plain' );
$r->status( $code );
$r->rflush;
$r->print( @_ );
return( $code );
}
sub success { return( shift->reply( Apache2::Const::HTTP_OK => 'ok' ) ); }
sub _request { return( shift->{request} ); }
sub _target { die( "This method needs to be superseeded in the inheriting package." ) }
sub _test
{
my $self = shift( @_ );
my $opts = shift( @_ );
die( "Argument provided is not an hash reference." ) if( ref( $opts ) ne 'HASH' );
my $class = ref( $self );
my $api = $self->api;
my $r = $self->request;
my $debug = $self->debug;
my $meth = $opts->{method};
if( !$meth )
{
$r->log_error( "${class}: no method provided to test." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
}
# expect may be undef
if( !exists( $opts->{expect} ) )
{
$r->log_error( "${class}: no expected value provided to test method '$meth'." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
}
my $expect = $opts->{expect};
my $args = exists( $opts->{args} ) ? $opts->{args} : undef;
$opts->{type} //= '';
my $obj = $self->_target;
if( !$obj )
{
$r->log_error( "${class}: Cannot get a target object." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
}
my $code = $obj->can( $meth );
if( !$code )
{
$r->log_error( "${class}: Method '$meth' is not supported in ", ref( $obj ), "." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
}
my $base_path;
unless( $base_path = $class2log->{ ref( $obj ) } )
{
my @parts = split( /::/, ref( $obj ) );
my $parent_path = $config->{vars}->{t_logs} || die( "No 't_logs' variable in Apache::TestConfig->thaw->httpd_config" );
$parent_path = file( $parent_path );
$base_path = $parent_path->child( join( '/', map( lc( $_ ), split( /::/, ref( $obj ) ) ) ) );
$base_path->mkpath if( !$base_path->exists );
$class2log->{ ref( $obj ) } = $base_path;
}
my $log_file = $base_path->child( "${meth}.log" );
my $io = $log_file->open( '>', { autoflush => 1, binmode => 'utf8' } ) ||
die( "Unable to open test log file \"$log_file\" in write mode: $!" );
my $val = $args ? $code->( $obj, @$args ) : $code->( $obj );
my $rv;
if( ref( $expect ) eq 'CODE' )
{
$rv = $expect->( $val, { object => $self, log => sub{ $io->print( @_, "\n" ) } } );
}
elsif( $opts->{type} eq 'boolean' )
{
$rv = ( int( $val // '' ) == $expect );
if( !$rv )
{
$io->print( "Boolean value expected (", ( $expect // 'undef' ), "), but got '", int( $val // '' ), "'\n" );
}
}
elsif( $opts->{type} eq 'isa' )
{
$rv = ( Scalar::Util::blessed( $val ) && $val->isa( $expect ) );
if( !$rv )
{
$io->print( "Object of class '", ( $expect // 'undef' ), "', but instead got '", ( $val // 'undef' ), "'\n" );
}
}
else
{
if( !defined( $val ) )
{
$rv = !defined( $expect );
if( !$rv )
{
$io->print( "Expected a defined value (", ( $expect // 'undef' ), "), but instead got an undefined one.\n" );
}
}
elsif( !defined( $expect ) )
{
$rv = 0;
if( !$rv )
{
$io->print( "Expected an undefined value, but instead got a defined one (", ( $val // 'undef' ), ").\n" );
}
}
else
{
$rv = ( $val eq $expect );
if( !$rv )
{
$io->print( "Expected the value to be '", ( $expect // 'undef' ), "', but instead got '", ( $val // 'undef' ), "'\n" );
}
}
}
$io->close;
$log_file->remove if( $log_file->is_empty );
$r->log_error( "${class}: ${meth}() -> ", ( $rv ? 'ok' : 'not ok' ) ) if( $debug );
return( $self->ok( $rv ) );
}
1;
# NOTE: POD
__END__
=encoding utf8
=head1 NAME
Test::Apache2::Common - Apache2::API Testing Common Class
=head1 SYNOPSIS
package Test::Apache2::API;
use parent qw( Test::Apache2::Common );
# etc.
=head1 VERSION
v0.1.0
=head1 DESCRIPTION
This is a package to inherit from for the test modules.
=head1 METHODS
=head2 failure
Calls L</reply> with C<Apache2::Const::HTTP_EXPECTATION_FAILED> and C<failed> and returns its value, which is the HTTP code.
=head2 is
Provided with a resulting value and an expected value and this returns C<ok> if both match, or a string explaining the failure to match.
=head2 ok
Provided with a boolean value, and this returns the value returned by L</success> or L</failure> otherwise.
=head2 reply
Provided with a response http code and some text data, and this will return the response to the http client.
=head2 success
Calls L</reply> with C<Apache2::Const::HTTP_OK> and C<ok> and returns its value, which is the http code.
=head1 AUTHOR
Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
=head1 SEE ALSO
L<Apache2::API>, L<Apache2::API::Request>, L<Apache2::API::Response>, L<Apache::Test>, L<Apache::TestUtil>, L<Apache::TestRequest>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2019-2023 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated
files under the same terms as Perl itself.
=cut
( run in 2.347 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )