1#!./perl -wT 2 3use lib qw(t/lib); 4 5# Due to a bug in older versions of MakeMaker & Test::Harness, we must 6# ensure the blib's are in @INC, else we might use the core CGI.pm 7use lib qw(blib/lib blib/arch); 8 9use Test::More tests => 12; 10 11use_ok( 'CGI::Push' ); 12 13ok( my $q = CGI::Push->new(), 'create a new CGI::Push object' ); 14 15# test the simple_counter() method 16like( join('', $q->simple_counter(10)) , '/updated.+?10.+?times./', 'counter' ); 17 18# test do_sleep, except we don't want to bog down the tests 19# there's also a potential timing-related failure lurking here 20# change this variable at your own risk 21my $sleep_in_tests = 0; 22 23SKIP: { 24 skip( 'do_sleep() test may take a while', 1 ) unless $sleep_in_tests; 25 26 my $time = time; 27 CGI::Push::do_sleep(2); 28 is(time - $time, 2, 'slept for a while' ); 29} 30 31# test push_delay() 32ok( ! defined $q->push_delay(), 'no initial delay' ); 33is( $q->push_delay(.5), .5, 'set a delay' ); 34 35my $out = tie *STDOUT, 'TieOut'; 36 37# next_page() to be called twice, last_page() once, no delay 38my %vars = ( 39 -next_page => sub { return if $_[1] > 2; 'next page' }, 40 -last_page => sub { 'last page' }, 41 -delay => 0, 42); 43 44$q->do_push(%vars); 45 46# this seems to appear on every page 47like( $$out, '/WARNING: YOUR BROWSER/', 'unsupported browser warning' ); 48 49# these should appear correctly 50is( ($$out =~ s/next page//g), 2, 'next_page callback called appropriately' ); 51is( ($$out =~ s/last page//g), 1, 'last_page callback called appropriately' ); 52 53# send a fake content type (header capitalization varies in CGI, CGI::Push) 54$$out = ''; 55$q->do_push(%vars, -type => 'fake' ); 56like( $$out, '/Content-[Tt]ype: fake/', 'set custom Content-type' ); 57 58# use our own counter, as $COUNTER in CGI::Push is now off 59my $i; 60$$out = ''; 61 62# no delay, custom headers from callback, only call callback once 63$q->do_push( 64 -delay => 0, 65 -type => 'dynamic', 66 -next_page => sub { 67 return if $i++; 68 return $_[0]->header('text/plain'), 'arduk'; 69 }, 70); 71 72# header capitalization again, our word should appear only once 73like( $$out, '/ype: text\/plain/', 'set custom Content-type in next_page()' ); 74is( $$out =~ s/arduk//g, 1, 'found text from next_page()' ); 75 76package TieOut; 77 78sub TIEHANDLE { 79 bless( \(my $text), $_[0] ); 80} 81 82sub PRINT { 83 my $self = shift; 84 $$self .= join( $/, @_ ); 85} 86