8 local $Data::Dumper::Sortkeys = 1;
13 eval { require Test::WWW::Mechanize::Catalyst }
15 "Test::WWW::Mechanize::Catalyst is required for this test";
21 use Test::WWW::Mechanize::Catalyst "SessionTestApp";
23 my $ua = Test::WWW::Mechanize::Catalyst->new;
25 # initial request - should not set cookie
26 $ua->get_ok( "http://localhost/page", "initial get" );
27 $ua->content_contains( "please login", "ua not logged in" );
28 is_deeply get_cookie(), undef, "no cookies yet";
30 # request that checks the session - should not set cookie
31 $ua->get_ok( "http://localhost/inspect_session",
32 "check for value in session" );
33 $ua->content_contains( "value of logged_in is 'undef'",
34 "check ua 'logged_in' val" );
35 is_deeply get_cookie(), undef, "no cookies yet";
37 # Login - should create a session
38 $ua->get_ok( "http://localhost/login", "log ua in" );
39 $ua->content_contains( "logged in", "ua logged in" );
41 # check that the session cookie created
42 my $session_id = get_cookie()->{val};
43 ok $session_id, "found a session cookie ($session_id)";
45 # check session loaded from store
46 $ua->get_ok( "http://localhost/page", "get main page" );
47 $ua->content_contains( "you are logged in", "ua logged in" );
48 is get_cookie()->{val}, $session_id, "session id has not changed";
50 # check that the expires time is updated
53 = SessionTestApp->config->{session}{min_lifetime};
55 = SessionTestApp->config->{session}{max_lifetime};
57 # do some requests until the expires changes
58 my $original_expiry = get_cookie()->{expires};
62 $ua->get("http://localhost/inspect_session");
63 my $new_expiry = get_cookie()->{expires};
64 next if $new_expiry == $original_expiry;
65 $original_expiry = $new_expiry;
69 # expiry just updated - check it stays the same
71 "http://localhost/inspect_session",
72 "get page to see expiry not changed"
74 is get_cookie()->{expires}, $original_expiry,
75 "expiry is still '$original_expiry'";
76 is get_cookie()->{val}, $session_id, "session id has not changed";
78 # sleep so that we go past the min lifetime
79 ok sleep $_, "sleep $_ so expires get extended"
80 for $max_lifetime - $min_lifetime + 1;
82 # expiry just updated - check it stays the same
84 "http://localhost/inspect_session",
85 "get page to see expiry has changed"
87 my $new_expiry = get_cookie()->{expires};
88 cmp_ok $new_expiry, '>', $original_expiry,
89 "expiry updated to '$new_expiry'";
90 is get_cookie()->{val}, $session_id, "session id has not changed";
92 # sleep beyond the lifetime and see that the session gets expired
93 ok sleep $_, "sleep $_ so session is too old" for $max_lifetime + 2;
95 "http://localhost/inspect_session",
96 "get page to see session expired"
98 is get_cookie(), undef, "Cookie has been reset";
102 # check that a session that is not in the db is deleted
104 my @session_ids_to_test = (
105 'a' x 40, # valid session id
106 'This is not valid @#$%^&', # bad value
109 foreach my $new_session_id (@session_ids_to_test) {
111 pass "--- Testing session_id '$new_session_id' ---";
113 $ua->get_ok( "http://localhost/login", "log ua in" );
114 $ua->content_contains( "logged in", "ua logged in" );
116 my $session_id = get_cookie()->{val};
117 ok $session_id, "have session_id '$session_id'";
119 # change the value in the cookie to a valid value
120 ok set_cookie_val($new_session_id),
121 "change cookie value to '$new_session_id'";
123 # check that the cookie gets deleted
125 "http://localhost/inspect_session",
126 "get page to see if session is deleted"
128 is get_cookie(), undef, "Cookie has been reset";
132 #############################################################################
135 my $cookie_jar = $ua->cookie_jar;
137 my $cookie_data = undef;
141 my ($version, $key, $val, $path,
142 $domain, $port, $path_spec, $secure,
143 $expires, $discard, $hash
146 # warn "cookie key: $key";
148 if ( $key eq 'sessiontestapp_session' ) {
162 my $cookie_jar = $ua->cookie_jar;
166 my ( $version, $key, $val, $path, $domain ) = @_;
168 # warn "cookie key: $key";
170 if ( $key eq 'sessiontestapp_session' ) {
172 $cookie_jar->set_cookie( $version, $key, $new_val, $path,