New Test for CGI::Push
authorchromatic <chromatic@wgz.org>
Mon, 10 Sep 2001 15:14:01 +0000 (09:14 -0600)
committerJarkko Hietaniemi <jhi@iki.fi>
Mon, 10 Sep 2001 23:26:40 +0000 (23:26 +0000)
Message-ID: <20010910211833.30177.qmail@onion.perl.org>

p4raw-id: //depot/perl@11988

MANIFEST
lib/CGI/t/push.t [new file with mode: 0644]

index ff9c218..d56709b 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -810,6 +810,7 @@ lib/CGI/t/form.t            See if CGI.pm works
 lib/CGI/t/function.t           See if CGI.pm works
 lib/CGI/t/html.t               See if CGI.pm works
 lib/CGI/t/pretty.t             See if CGI.pm works
+lib/CGI/t/push.t               See if CGI::Push works
 lib/CGI/t/request.t            See if CGI.pm works
 lib/CGI/t/switch.t              See if CGI::Switch still loads
 lib/CGI/t/util.t               See if CGI.pm works
diff --git a/lib/CGI/t/push.t b/lib/CGI/t/push.t
new file mode 100644 (file)
index 0000000..2459c1f
--- /dev/null
@@ -0,0 +1,84 @@
+#!./perl -wT
+
+BEGIN {
+       chdir 't' if -d 't';
+       @INC = '../lib';
+}
+
+use Test::More tests => 12; 
+
+use_ok( 'CGI::Push' );
+
+ok( my $q = CGI::Push->new(), 'create a new CGI::Push object' );
+
+# test the simple_counter() method
+like( join('', $q->simple_counter(10)) , qr/updated.+?10.+?times./, 'counter' );
+
+# test do_sleep, except we don't want to bog down the tests
+# there's also a potential timing-related failure lurking here
+# change this variable at your own risk
+my $sleep_in_tests = 0;
+
+SKIP: {
+       skip( 'do_sleep() test may take a while', 1 ) unless $sleep_in_tests;
+
+       my $time = time;
+       CGI::Push::do_sleep(2);
+       is(time - $time, 2, 'slept for a while' );
+}
+
+# test push_delay()
+ok( ! defined $q->push_delay(), 'no initial delay' );
+is( $q->push_delay(.5), .5, 'set a delay' );
+
+my $out = tie *STDOUT, 'TieOut';
+
+# next_page() to be called twice, last_page() once, no delay
+my %vars = (
+       -next_page      => sub { return if $_[1] > 2; 'next page' },
+       -last_page      => sub { 'last page' },
+       -delay          => 0,
+);
+
+$q->do_push(%vars);
+
+# this seems to appear on every page
+like( $$out, qr/WARNING: YOUR BROWSER/, 'unsupported browser warning' );
+
+# these should appear correctly
+is( ($$out =~ s/next page//g), 2, 'next_page callback called appropriately' );
+is( ($$out =~ s/last page//g), 1, 'last_page callback called appropriately' );
+
+# send a fake content type (header capitalization varies in CGI, CGI::Push)
+$$out = '';
+$q->do_push(%vars, -type => 'fake' );
+like( $$out, qr/Content-[Tt]ype: fake/, 'set custom Content-type' );
+
+# use our own counter, as $COUNTER in CGI::Push is now off
+my $i;
+$$out = '';
+
+# no delay, custom headers from callback, only call callback once
+$q->do_push(
+       -delay          => 0,
+       -type           => 'dynamic',
+       -next_page      => sub { 
+               return if $i++;
+               return $_[0]->header('text/plain'), 'arduk';
+        },
+);
+
+# header capitalization again, our word should appear only once
+like( $$out, qr!ype: text/plain!, 'set custom Content-type in next_page()' );
+is( $$out =~ s/arduk//g, 1, 'found text from next_page()' );
+       
+package TieOut;
+
+sub TIEHANDLE {
+       bless( \(my $text), $_[0] );
+}
+
+sub PRINT {
+       my $self = shift;
+       $$self .= join( $/, @_ );
+}