Apache2-API

 view release on metacpan or  search on metacpan

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

package Test::Apache2::Common;
BEGIN
{
    use strict;
    use warnings;
    use lib './lib';
    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 Apache::TestConfig;
    use APR::URI ();
    use Apache2::API;
    use Module::Generic::File qw( file );
    use Scalar::Util;
};

use strict;
use warnings;

our $config = Apache::TestConfig->thaw->httpd_config;
our $class2log = {};

sub handler : method
{
    my( $class, $r ) = @_;
    my $debug = $r->dir_config( 'API_DEBUG' );
    $r->log_error( "${class}: Received request for uri \"", $r->uri, "\" matching file \"", $r->filename, "\": ", $r->as_string );
    my $uri = APR::URI->parse( $r->pool, $r->uri );
    my $path = [split( '/', $uri->path )]->[-1];
    my $api = Apache2::API->new( $r, debug => $debug, compression_threshold => 102400 );
    if( !$api )
    {
        $r->log_error( "${class}: Error instantiating Apache2::API object: ", Apache2::API->error );
        return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
    }
    my $self = bless( { request => $r, api => $api, debug => int( $r->dir_config( 'API_DEBUG' ) ) } => $class );
    my $code = $self->can( $path );
    if( !defined( $code ) )
    {
        $r->log_error( "No method \"$path\" for testing." );
        return( Apache2::Const::DECLINED );
    }
    $r->err_headers_out->set( 'Test-No' => $path );
    my $rc = $code->( $self );
    $r->log_error( "${class}: Returning HTTP code '$rc' for method '$path'" );
    if( $rc == Apache2::Const::HTTP_OK )
    {
        # https://perl.apache.org/docs/2.0/user/handlers/intro.html#item_RUN_FIRST
        # return( Apache2::Const::DONE );
        return( Apache2::Const::OK );
    }
    else
    {
        return( $rc );
    }
    # $r->connection->client_socket->close();
    exit(0);
}

sub api { return( shift->{api} ); }

sub request { return( shift->{request} ); }



( run in 3.190 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )