Apache2-API

 view release on metacpan or  search on metacpan

t/01.api.t  view on Meta::CPAN

    foreach my $test ( @tests )
    {
        &simple_test({ target => 'response', name => $test, code => Apache2::Const::HTTP_OK });
    }
};

sub make_request
{
    my( $type, $path, $opts ) = @_;
    
    my $http_meth = uc( $opts->{http_method} // 'GET' );
    my $req = HTTP::Request->new( $http_meth => "${proto}://${hostport}/tests/${type}/${path}",
        ( exists( $opts->{headers} ) ? $opts->{headers} : () ),
        ( ( exists( $opts->{body} ) && length( $opts->{body} // '' ) ) ? $opts->{body} : () ),
    );
    if( $opts->{query} )
    {
        my $u = URI->new( $req->uri );
        $u->query( $opts->{query} );
        $req->uri( $u );
    }
    
    unless( $req->header( 'Content-Type' ) )
    {
        $req->header( Content_Type => 'text/plain; charset=utf-8' );
    }
    
    # $req->header( Host => "${mp_host}:${port}" );
    diag( "Request for $path is: ", $req->as_string ) if( $DEBUG );
    my $resp = $ua->request( $req );
    diag( "Server response for $path is: ", $resp->as_string ) if( $DEBUG );
    return( $resp );
}

sub simple_test
{
    my $opts = shift( @_ );
    if( !$opts->{name} )
    {
        die( "No test name was provided." );
    }
    elsif( !defined( $opts->{code} ) )
    {
        die( "No HTTP code was provided." );
    }
    elsif( !defined( $opts->{target} ) )
    {
        die( "No test target was provided. It should be 'api', 'request' or 'response'" );
    }
    my $resp = &make_request( $opts->{target} => $opts->{name}, $opts );
    is( $opts->{code}, Apache2::Const::HTTP_OK, $opts->{name} ) || 
        diag( "Error with test \"$opts->{name}\". See log content below:\n", &get_log( $opts ) );
}

sub get_log
{
    my $opts = shift( @_ );
    my $log_file = $target2path->{ $opts->{target} }->child( $opts->{name} . '.log' );
    if( $log_file->exists )
    {
        return( $log_file->load_utf8 );
    }
    else
    {
        diag( "Test $opts->{target} -> $opts->{name} seems to have failed, but there is no log file \"$log_file\"" ); 
    }
}

done_testing();

__END__



( run in 1.358 second using v1.01-cache-2.11-cpan-39bf76dae61 )