Module-Generic

 view release on metacpan or  search on metacpan

t/lib/Test/Apache2/MG.pm  view on Meta::CPAN

## -*- perl -*-
##----------------------------------------------------------------------------
## Module Generic - ~/t/lib/Test/Apache2/MG.pm
## Version v0.1.1
## Copyright(c) 2025 DEGUEST Pte. Ltd.
## Author: Jacques Deguest <jack@deguest.jp>
## Created 2025/05/05
## Modified 2025/05/05
## All rights reserved
## 
## This program is free software; you can redistribute  it  and/or  modify  it
## under the same terms as Perl itself.
##----------------------------------------------------------------------------
package Test::Apache2::MG;
BEGIN
{
    use strict;
    use warnings;
    use lib './lib';
    use parent qw( Test::Apache2::Base );
    use Apache2::Connection ();
    use Apache2::Const -compile => qw( :common :http OK DECLINED );
    use Apache2::RequestIO ();
    use Apache2::RequestRec ();
    # so we can get the request as a string
    use Apache2::RequestUtil ();
    use Apache2::Response ();
    use APR::Table ();
    use APR::URI ();
    use JSON;
    use Module::Generic;
    use Module::Generic::Global;
    use Scalar::Util;
    # 2021-11-1T17:12:10+0900
    use Test::Time time => 1635754330;
    use constant HAS_SSL => ( $ENV{HTTPS} || ( defined( $ENV{SCRIPT_URI} ) && substr( lc( $ENV{SCRIPT_URI} ), 0, 5 ) eq 'https' ) ) ? 1 : 0;
};

use strict;
use warnings;

sub handler : method
{
    my( $class, $r ) = @_;
    my $debug = int( $r->dir_config( 'MG_DEBUG' ) // 0 );
    $r->log_error( "$class: MG_DEBUG value set to '$debug'" );
    Apache2::RequestUtil->request( $r );
    # Module::Generic::Global->cleanup_register( $r );
    # Call the inner module to instantiate a new object
    my $obj = MyObject->new( request => $r, debug => $debug );
    if( !$obj )
    {
        $r->log_error( "$class: Error instantiating Apache2::API object: ", MyObject->error );
        return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
    }
    my $self = bless( { request => $r, _object => $obj, debug => $debug } => $class );
    return( $self->process );
}

sub dummy_error
{
    my $self = shift( @_ );
    my $obj = $self->_object || die( "No object." );
    $obj->error({ message => 'Oh no!', code => 400 });
    return( $self->reply_enhanced({ message => $obj->error->message, code => $obj->error->code }) );
}

sub has_modperl { return( shift->_test({ method => 'has_modperl', type => 'boolean', expect => 1 }) ); }

sub non_threaded_error
{
    my $self = shift( @_ );
    my $obj = $self->_object || die( "No object." );
    $obj->error({ message => 'Non-threaded error test', code => 400 });
    return( $self->reply_enhanced({ message => $obj->error->message, code => $obj->error->code }) );
}

sub threaded_error
{
    my $self = shift( @_ );
    my $obj = $self->_object || die( "No object." );
    $obj->error({ message => 'Threaded error test', code => 400 });
    return( $self->reply_enhanced({ message => $obj->error->message, code => $obj->error->code }) );
}

# Calling the method 'new_json' inherited from Module::Generic
sub json { return( shift->_test({ method => 'new_json', expect => sub
{
    my $json = shift( @_ );
    return( Scalar::Util::blessed( $json ) && 
               $json->isa( 'JSON' ) && 
               $json->canonical && 
               $json->get_relaxed && 
               $json->get_utf8 && 
               $json->get_allow_nonref && 
               $json->get_allow_blessed && 
               $json->get_convert_blessed );
}, args => [pretty => 1, ordered => 1, relaxed => 1, utf8 => 1, allow_nonref => 1, allow_blessed => 1, convert_blessed => 1] }) ); }

sub reply
{
    return( shift->reply_enhanced( Apache2::Const::HTTP_OK => {
        message => "ok",
    }) );
}

# NOTE: Hidden package MyObject
package
    MyObject;
BEGIN
{
    use strict;
    use warnings;
    use warnings::register;
    use parent qw( Module::Generic );
    our $VERSION = 'v0.1.0';



( run in 2.555 seconds using v1.01-cache-2.11-cpan-e86d8f7595a )