add README
[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 skip_all => 'Test::WWW::Mechanize::Catalyst >= 0.40 required' if $Test::WWW::Mechanize::Catalyst::VERSION < 0.40;
14     plan 'no_plan';
15 }
16
17 {
18
19     package CookieTestApp;
20     use Catalyst qw/
21       Session
22       Session::Store::Dummy
23       Session::State::Cookie
24       /;
25
26     sub page : Local {
27         my ( $self, $c ) = @_;
28         $c->res->body( "Hi! hit number " . ++$c->session->{counter} );
29     }
30
31     sub stream : Local {
32         my ( $self, $c ) = @_;
33         my $count = ++$c->session->{counter};
34         $c->res->write("hit number ");
35         $c->res->write($count);
36     }
37
38     sub deleteme : Local {
39         my ( $self, $c ) = @_;
40         my $id = $c->get_session_id;
41         $c->delete_session;
42         my $id2 = $c->get_session_id;
43         $c->res->body( $id ne ( $id2 || '' ) );
44     }
45
46     __PACKAGE__->setup;
47 }
48
49 use Test::WWW::Mechanize::Catalyst qw/CookieTestApp/;
50
51 my $m = Test::WWW::Mechanize::Catalyst->new;
52
53 $m->get_ok( "http://localhost/stream", "get page" );
54 $m->content_contains( "hit number 1", "session data created" );
55
56 my $expired;
57 $m->cookie_jar->scan( sub { $expired = $_[8]; } );
58
59 $m->get_ok( "http://localhost/page", "get page" );
60 $m->content_contains( "hit number 2", "session data restored" );
61
62 $m->get_ok( "http://localhost/stream", "get stream" );
63 $m->content_contains( "hit number 3", "session data restored" );
64
65 sleep 1;
66
67 $m->get_ok( "http://localhost/page", "get stream" );
68 $m->content_contains( "hit number 4", "session data restored" );
69
70 my $updated_expired;
71 $m->cookie_jar->scan( sub { $updated_expired = $_[8]; } );
72
73 cmp_ok( $expired, "<", $updated_expired, "cookie expiration was extended" );
74
75 $m->get_ok( "http://localhost/deleteme", "get page" );
76 $m->content_is( 1, 'session id changed' );