Catalyst-View-Template-PHP

 view release on metacpan or  search on metacpan

t/10-headers.t  view on Meta::CPAN

use FindBin;
use lib "$FindBin::Bin/lib";
use strict;
use warnings;
use Test::More;
use Catalyst::Test 'TestApp';
use Data::Dumper;
use HTTP::Request::Common;   # reqd for POST requests

eval "use PHP 0.13";
if ($@) {
   plan skip_all => "PHP 0.13 needed for testing";
}

BEGIN {
    no warnings 'redefine';
    *Catalyst::Test::local_request = sub {
	my ($class, $req) = @_;
	my $app = ref($class) eq "CODE" ? $class : $class->_finalized_psgi_app;
	my $ret;
	require Plack::Test;
	Plack::Test::test_psgi(
	    app => sub { $app->( %{ $_[0] } ) },
	    client => sub { $ret = shift->{request} } );
	return $ret;
    };
}

{
    my $response = request POST 'http://localhost/headers.php', [
	abc => 123,
	def => 456
    ];

    ok( $response, 'response simple post ok' );
    my $content = eval { $response->content };

    ok( $TestApp::View::PHPTest::last_header, 'header callback was called' );
    ok( $TestApp::View::PHPTest::first_header, 'header callback was called' );
    ok( scalar @TestApp::View::PHPTest::headers, 'header callback was called' );

    ok( $TestApp::View::PHPTest::first_header eq 'X-header-abc: 123',
	'last header correct' );
    ok( $TestApp::View::PHPTest::last_header eq 'X-header-def: 456',
	'last header correct' );
    ok( $TestApp::View::PHPTest::headers[0] eq $TestApp::View::PHPTest::last_header &&
	$TestApp::View::PHPTest::headers[1] eq $TestApp::View::PHPTest::first_header,
	'header list set correctly' );

    # use headers and the header callback as a channel for PHP/Perl
    # communication.
    $response = request 'http://localhost/header_compute.php';
    $content = eval { $response->content };
    ok( $response, 'response ok for header_compute.php' );
    ok( $content,  'got content for header_compute.php' );
    ok( $content =~ /begin result/, 'found result begin marker' );
    ok( $content =~ /end result/, 'found result end marker' );
    ok( $content !~ /Input/, 'post had no requests, so there are no results' );

    $response = request POST 'http://localhost/header_compute.php', [
	expr1 => 'exp(5.5 * log(14.14))',
	expr6 => '$INC{"PHP.pm"}'
    ];
    $content = eval { $response->content };
    ok( $response, 'response ok for header_compute.php' );
    ok( $content,  'got content for header_compute.php' );
    ok( $content =~ /begin result/, 'found result begin marker' );
    ok( $content =~ /end result/, 'found result end marker' );



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