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