print sprintf??? printf!
[catagits/Catalyst-Plugin-Session-State-Stash.git] / t / 05-live.t
1 #!/usr/bin/perl                                                                                                       
2
3 use strict;
4 use warnings;
5
6 use URI::Escape;
7 use Test::More;
8
9 BEGIN {
10     eval { require Test::WWW::Mechanize::Catalyst };
11     plan skip_all =>
12       "This test requires Test::WWW::Mechanize::Catalyst in order to run"
13       if $@;
14     plan skip_all => 'Test::WWW::Mechanize::Catalyst >= 0.40 required' if $Test::WWW::Mechanize::Catalyst::VERSION < 0.40;
15 }
16
17 {
18
19     package TestApp;
20     use Catalyst qw/                                                                                                  
21       Session                                                                                                         
22       Session::Store::Dummy                                                                                           
23       Session::State::Stash
24       -Debug                                                                                      
25       /;
26
27     sub start_session : Local {
28         my ( $self, $c ) = @_;
29         $c->session->{counter} = 1;
30         $c->res->body($c->stash->{_session}->{id});
31     }
32     
33     sub page : Local {
34         my ( $self, $c, $id ) = @_;
35         $c->stash ( '_session' => {id => $id} );
36         $c->res->body( "Hi! hit number " . ++$c->session->{counter} );
37     }
38         
39     sub stream : Local {
40         my ( $self, $c, $id ) = @_;
41         $c->stash ( '_session' => {id => $id} );
42         my $count = ++$c->session->{counter};
43             $c->res->body("hit number $count");
44     }
45     
46     sub deleteme : Local {
47         my ( $self, $c, $id ) = @_;
48         $c->stash ( '_session' => {id => $id} );
49         my $id2 = $c->get_session_id;
50             $c->delete_session;
51         my $id3 = $c->get_session_id;
52         
53         # In the success case, print 'Pass'
54         if (defined $id2 &&
55             defined $id3 &&
56             $id2 ne $id3
57         ) {
58             $c->res->body('PASS');
59         } else {
60             #In the failure case, provide debug info
61             $id3 ||= '';
62             $c->res->body("FAIL: Matching ids, $id3");
63         }
64     }
65     
66     __PACKAGE__->setup;
67 }
68
69 use Test::WWW::Mechanize::Catalyst qw/TestApp/;
70
71 my $m = Test::WWW::Mechanize::Catalyst->new;
72
73 #Number of tests to run. A begin block every 10 will ensure the count is correct
74 my $tests;
75 plan tests => $tests;
76
77 $m->get_ok( "http://localhost/start_session", "get page" );
78 my $session = uri_escape($m->content);
79
80 $m->get_ok( "http://localhost/page/$session", "get page" );
81 $m->content_contains( "hit number 2", "session data restored" );
82
83 $m->get_ok( "http://localhost/stream/$session", "get stream" );
84 $m->content_contains( "hit number 3", "session data restored" );
85
86 BEGIN { $tests += 5; }
87
88 $m->get_ok( "http://localhost/stream/$session", "get page" );
89 $m->content_contains( "hit number 4", "session data restored" );
90 $m->get_ok( "http://localhost/deleteme/$session", "get page" );
91
92 TODO: {
93     local $TODO = "Changing sessions is broken and I've had no success fixing it. Patches welcome";
94     $m->content_is( 'PASS' , 'session id changed' );
95 }
96 BEGIN { $tests += 4; }