releng
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / t / live_app.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9     eval { require Test::WWW::Mechanize::Catalyst };
10     plan skip_all =>
11       "This test requires Test::WWW::Mechanize::Catalyst in order to run"
12       if $@;
13     plan 'no_plan';
14 }
15
16 {
17
18     package CookieTestApp;
19     use Catalyst qw/
20       Session
21       Session::Store::Dummy
22       Session::State::Cookie
23       /;
24
25     sub page : Local {
26         my ( $self, $c ) = @_;
27         $c->res->body( "Hi! hit number " . ++$c->session->{counter} );
28     }
29
30     sub stream : Local {
31         my ( $self, $c ) = @_;
32         my $count = ++$c->session->{counter};
33         $c->res->write("hit number ");
34         $c->res->write($count);
35     }
36
37     sub deleteme : Local {
38         my ( $self, $c ) = @_;
39         my $id = $c->get_session_id;
40         $c->delete_session;
41         my $id2 = $c->get_session_id;
42         $c->res->body( $id ne ( $id2 || '' ) );
43     }
44
45     __PACKAGE__->setup;
46 }
47
48 use Test::WWW::Mechanize::Catalyst qw/CookieTestApp/;
49
50 my $m = Test::WWW::Mechanize::Catalyst->new;
51
52 $m->get_ok( "http://foo.com/stream", "get page" );
53 $m->content_contains( "hit number 1", "session data created" );
54
55 my $expired;
56 $m->cookie_jar->scan( sub { $expired = $_[8] } );
57
58 $m->get_ok( "http://foo.com/page", "get page" );
59 $m->content_contains( "hit number 2", "session data restored" );
60
61 $m->get_ok( "http://foo.com/stream", "get stream" );
62 $m->content_contains( "hit number 3", "session data restored" );
63
64 sleep 2;
65
66 $m->get_ok( "http://foo.com/page", "get stream" );
67 $m->content_contains( "hit number 4", "session data restored" );
68
69 my $updated_expired;
70 $m->cookie_jar->scan( sub { $updated_expired = $_[8] } );
71
72 cmp_ok( $expired, "<", $updated_expired, "cookie expiration was extended" );
73
74 $m->get_ok( "http://foo.com/deleteme", "get page" );
75 $m->content_is( 1, 'session id changed' );