Commit | Line | Data |
d77a73f1 |
1 | #!./perl -wT |
2 | |
8f3ccfa2 |
3 | use 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 |
7 | use lib qw(blib/lib blib/arch); |
d77a73f1 |
8 | |
9 | use Test::More tests => 12; |
10 | |
11 | use_ok( 'CGI::Push' ); |
12 | |
13 | ok( my $q = CGI::Push->new(), 'create a new CGI::Push object' ); |
14 | |
15 | # test the simple_counter() method |
ac734d8b |
16 | like( join('', $q->simple_counter(10)) , '/updated.+?10.+?times./', 'counter' ); |
d77a73f1 |
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 |
21 | my $sleep_in_tests = 0; |
22 | |
23 | SKIP: { |
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() |
32 | ok( ! defined $q->push_delay(), 'no initial delay' ); |
33 | is( $q->push_delay(.5), .5, 'set a delay' ); |
34 | |
35 | my $out = tie *STDOUT, 'TieOut'; |
36 | |
37 | # next_page() to be called twice, last_page() once, no delay |
38 | my %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 |
ac734d8b |
47 | like( $$out, '/WARNING: YOUR BROWSER/', 'unsupported browser warning' ); |
d77a73f1 |
48 | |
49 | # these should appear correctly |
50 | is( ($$out =~ s/next page//g), 2, 'next_page callback called appropriately' ); |
51 | is( ($$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' ); |
ac734d8b |
56 | like( $$out, '/Content-[Tt]ype: fake/', 'set custom Content-type' ); |
d77a73f1 |
57 | |
58 | # use our own counter, as $COUNTER in CGI::Push is now off |
59 | my $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 |
ac734d8b |
73 | like( $$out, '/ype: text\/plain/', 'set custom Content-type in next_page()' ); |
d77a73f1 |
74 | is( $$out =~ s/arduk//g, 1, 'found text from next_page()' ); |
75 | |
76 | package TieOut; |
77 | |
78 | sub TIEHANDLE { |
79 | bless( \(my $text), $_[0] ); |
80 | } |
81 | |
82 | sub PRINT { |
83 | my $self = shift; |
84 | $$self .= join( $/, @_ ); |
85 | } |