1N/A#!./perl -wT
1N/A
1N/Ause Test::More tests => 12;
1N/A
1N/Ause_ok( 'CGI::Push' );
1N/A
1N/Aok( my $q = CGI::Push->new(), 'create a new CGI::Push object' );
1N/A
1N/A# test the simple_counter() method
1N/Alike( join('', $q->simple_counter(10)) , '/updated.+?10.+?times./', 'counter' );
1N/A
1N/A# test do_sleep, except we don't want to bog down the tests
1N/A# there's also a potential timing-related failure lurking here
1N/A# change this variable at your own risk
1N/Amy $sleep_in_tests = 0;
1N/A
1N/ASKIP: {
1N/A skip( 'do_sleep() test may take a while', 1 ) unless $sleep_in_tests;
1N/A
1N/A my $time = time;
1N/A CGI::Push::do_sleep(2);
1N/A is(time - $time, 2, 'slept for a while' );
1N/A}
1N/A
1N/A# test push_delay()
1N/Aok( ! defined $q->push_delay(), 'no initial delay' );
1N/Ais( $q->push_delay(.5), .5, 'set a delay' );
1N/A
1N/Amy $out = tie *STDOUT, 'TieOut';
1N/A
1N/A# next_page() to be called twice, last_page() once, no delay
1N/Amy %vars = (
1N/A -next_page => sub { return if $_[1] > 2; 'next page' },
1N/A -last_page => sub { 'last page' },
1N/A -delay => 0,
1N/A);
1N/A
1N/A$q->do_push(%vars);
1N/A
1N/A# this seems to appear on every page
1N/Alike( $$out, '/WARNING: YOUR BROWSER/', 'unsupported browser warning' );
1N/A
1N/A# these should appear correctly
1N/Ais( ($$out =~ s/next page//g), 2, 'next_page callback called appropriately' );
1N/Ais( ($$out =~ s/last page//g), 1, 'last_page callback called appropriately' );
1N/A
1N/A# send a fake content type (header capitalization varies in CGI, CGI::Push)
1N/A$$out = '';
1N/A$q->do_push(%vars, -type => 'fake' );
1N/Alike( $$out, '/Content-[Tt]ype: fake/', 'set custom Content-type' );
1N/A
1N/A# use our own counter, as $COUNTER in CGI::Push is now off
1N/Amy $i;
1N/A$$out = '';
1N/A
1N/A# no delay, custom headers from callback, only call callback once
1N/A$q->do_push(
1N/A -delay => 0,
1N/A -type => 'dynamic',
1N/A -next_page => sub {
1N/A return if $i++;
1N/A return $_[0]->header('text/plain'), 'arduk';
1N/A },
1N/A);
1N/A
1N/A# header capitalization again, our word should appear only once
1N/Alike( $$out, '/ype: text\/plain/', 'set custom Content-type in next_page()' );
1N/Ais( $$out =~ s/arduk//g, 1, 'found text from next_page()' );
1N/A
1N/Apackage TieOut;
1N/A
1N/Asub TIEHANDLE {
1N/A bless( \(my $text), $_[0] );
1N/A}
1N/A
1N/Asub PRINT {
1N/A my $self = shift;
1N/A $$self .= join( $/, @_ );
1N/A}