Apache2-API

 view release on metacpan or  search on metacpan

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

    use Apache2::API;
    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 compression_threshold { return( shift->_test({ method => 'compression_threshold', expect => 102400 }) ); }

my $json = <<EOT;
{
    "debug": "true",
    "client_id": "d7024a37-f8d8-4d37-bbc4-5bd19429df8c",
    "total": 10.20,
}
EOT

sub decode_json { return( shift->_test({ method => 'compression_threshold', expect => sub
{
    my $ref = shift( @_ );
    return(0) if( ref( $ref ) ne 'HASH' );
    return( $ref->{debug} && $ref->{client_id} eq 'd7024a37-f8d8-4d37-bbc4-5bd19429df8c' && $ref->{total} == 10.20 );
}, args => [$json] }) ); }

sub encode_decode_url
{
    my $self = shift( @_ );
    my $class = ref( $self );
    my $api = $self->api;
    my $r = $self->request;
    my $debug = $self->debug;
    # Borrowed from URL::Encode
    my $UNRESERVED = "0123456789"
    . "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    . "abcdefghijklmnopqrstuvwxyz"
    . "_.~-";
    
    my @tests = (
        [ "",         "",     "empty string" ],
        [ "\x{00E5}", "%E5",  "U+00E5 in native encoding" ],
        [ $UNRESERVED, $UNRESERVED, "unreserved characters" ],
        [ " ", "+", "U+0020 SPACE" ]
    );
    
    for my $ord ( 0x00..0x1F, 0x21..0xFF )
    {
        my $chr = pack( 'C', $ord );
        next unless( index( $UNRESERVED, $chr ) < 0 );
        my $enc = sprintf( '%%%.2X', $ord );
        push @tests, [ $chr, $enc, sprintf( "ordinal %d", $ord ) ];
    }

    my $cnt = 0;
    foreach my $test ( @tests )
    {
        my( $expected, $encoded, $name ) = @$test;
        my $rv = $api->decode_url( $encoded ) eq $expected ? 1 : 0;
        $r->log_error( "$[class}: decode_url(): $name -> ", ( $rv ? 'ok' : 'not ok' ) ) if( $debug );
        $cnt++ if( $rv );
    }

    foreach my $test ( @tests )
    {
        my( $octets, $expected, $name ) = @$test;
        my $rv = $api->encode_url( $octets ) eq $expected ? 1 : 0;
        $r->log_error( "$[class}: encode_url(): $name -> ", ( $rv ? 'ok' : 'not ok' ) ) if( $debug );
        $cnt++ if( $rv );
    }
    return( $self->ok( $cnt == scalar( @tests ) ) );
}

my $jwt = q{eyJleHAiOjE2MzYwNzEwMzksImFsZyI6IkhTMjU2In0.eyJqdGkiOiJkMDg2Zjk0OS1mYWJmLTRiMzgtOTE1ZC1hMDJkNzM0Y2ZmNzAiLCJmaXJzdF9uYW1lIjoiSm9obiIsImlhdCI6MTYzNTk4NDYzOSwiYXpwIjoiNGQ0YWFiYWQtYmJiMy00ODgwLThlM2ItNTA0OWMwZTczNjBlIiwiaXNzIjoiaHR0cHM6Ly9hcG...
# Need to set the Authorization header in the test unit
# $r->authorization( "Bearer ${jwt}" );
sub auth { return( shift->_test({ method => 'get_auth_bearer', expect => $jwt }) ); }

sub header_datetime { return( shift->_test({ method => 'header_datetime', expect => 'Mon, 01 Nov 2021 08:12:10 GMT' }) ); }

sub is_perl_option_enabled { return( shift->_test({ method => 'is_perl_option_enabled', expect => 1, type => 'boolean', args => ['GlobalRequest'] }) ); }

sub json { return( shift->_test({ method => '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->api->reply( Apache2::Const::HTTP_OK => {
        message => "ok",
    }) );
}

sub server { return( shift->_test({ method => 'server', expect => 'Apache2::ServerRec', type => 'isa' }) ); }

sub server_version { return( shift->_test({ method => 'server_version', expect => 'version', type => 'isa' }) ); }

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

1;
# NOTE: POD
# Use this to generate the tests list:
# egrep -E '^sub ' ./t/lib/Test/Apache2/API.pm | perl -lnE 'my $m = [split(/\s+/, $_)]->[1]; say "=head2 $m\n"'
__END__

=encoding utf8

=head1 NAME

Test::Apache2::API - Apache2::API Testing Class

=head1 SYNOPSIS

In the Apache test conf:

    PerlModule Apache2::API
    PerlOptions +GlobalRequest
    PerlSetupEnv On
    <Directory "@documentroot@">



( run in 0.456 second using v1.01-cache-2.11-cpan-5a3173703d6 )