#!/usr/bin/perl

# cpanel - mod_fastinclude.t                      Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package Attracta::FastInclude::Test;

# Module - tests functionality of mod_fastinclude
#
#  Created by Dave Koston <dave@kostonconsulting.com>.
#  Copyright (c) 2012 Attracta. All rights reserved.
#

use strict;

my $test_include_file = '/usr/local/apache/htdocs/.fastinclude';
my $test_html_file    = '/usr/local/apache/htdocs/attracta-test.html';

sub addTestHTMLFile {
    if ( open( my $fh, '>', $test_html_file ) ) {
        print $fh "<html><head></head><body><p>Test</p></body></html>";
        close($fh);
        return 1;
    }
    else {
        return 0;
    }
}

sub removeTestHTMLFile {
    if ( -f $test_html_file ) {
        unlink($test_html_file);
    }
}

sub addTestIncludeFile {
    if ( open( my $fh, '>', $test_include_file ) ) {
        print $fh "<!--fast_include_test-->";
        close($fh);
        return 1;
    }
    else {
        return 0;
    }
}

sub removeTestIncludeFile {
    if ( -f $test_include_file ) {
        unlink($test_include_file);
    }
}

sub getLocalhostResponse {
    my $curl_exists = `which curl`;
    my $response    = 0;
    if ($curl_exists) {
        $response = `curl -sS http://127.0.0.1/attracta-test.html`;
    }
    return $response;
}

sub isStandardResponse {
    my $response = shift;

    if ( $response eq '<html><head></head><body><p>Test</p></body></html>' ) {
        return 1;
    }
    else {
        return 0;
    }
}

sub isFIResponse {
    my $response = shift;

    if ( $response eq '<html><head></head><body><p>Test</p></body></html><!--fast_include_test-->' ) {
        return 1;
    }
    else {
        return 0;
    }

}

package main;

# cpanel - mod_fastinclude.t                      Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use warnings;

use Test::More tests => 6;

ok( Attracta::FastInclude::Test::addTestHTMLFile(),    "Test html file has been created" );
ok( Attracta::FastInclude::Test::addTestIncludeFile(), "Test include file has been created" );

my $resp = Attracta::FastInclude::Test::getLocalhostResponse();

ok( !Attracta::FastInclude::Test::isStandardResponse($resp), "Response received is not a standard response" );
ok( Attracta::FastInclude::Test::isFIResponse($resp),        "Response received is a fastinclude response" );
ok( Attracta::FastInclude::Test::removeTestHTMLFile(),       "Test html file has been deleted" );
ok( Attracta::FastInclude::Test::removeTestIncludeFile(),    "Test include file has been deleted" );
