add some tests to t/cat_test.t
[catagits/Catalyst-Plugin-Session.git] / t / cat_test.t
CommitLineData
a2e23c04 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use Test::More;
6use HTTP::Request::Common;
7
8# setup library path
9use FindBin qw($Bin);
10use lib "$Bin/lib";
11
1b41775a 12# this test was copied from CatalystX::SimpleLogin
13
f851ba4e 14BEGIN {
15 plan skip_all => "Need Catalyst::Plugin::Session::State::Cookie"
16 unless do { local $@; eval { require Catalyst::Plugin::Session::State::Cookie; } };
1b41775a 17 plan skip_all => "Need Catalyst::Plugin::Authentication"
18 unless do { local $@; eval { require Catalyst::Plugin::Authentication; } };
f851ba4e 19}
20
a2e23c04 21use Catalyst::Test 'SessionTestApp';
22my ($res, $c);
23
24($res, $c) = ctx_request(POST 'http://localhost/login', [username => 'bob', password => 's00p3r', remember => 1]);
25is($res->code, 200, 'succeeded');
26my $cookie = $res->header('Set-Cookie');
27ok($cookie, 'Have a cookie');
28
71bde3b4 29# cookie is changed by the get
30sleep(1);
1b41775a 31($res, $c) = ctx_request(GET 'http://localhost/page', Cookie => $cookie);
71bde3b4 32like($c->res->body, qr/logged in/, 'logged in');
1b41775a 33my $new_cookie = $res->header('Set-Cookie');
71bde3b4 34isnt( $cookie, $new_cookie, 'cookie expires has been updated' );
35
36# request with no cookie
37($res, $c) = ctx_request(GET 'http://localhost/page' );
38like($c->res->body, qr/please login/, 'not logged in');
39$new_cookie = $res->header('Set-Cookie');
40ok( ! defined $new_cookie, 'no cookie created' );
41
42# check that cookie is reset by reset_session_expires
43($res, $c) = ctx_request(GET 'http://localhost/reset_session_expires', Cookie => $cookie);
44my $reset_cookie = $res->header('Set-Cookie');
45isnt( $cookie, $reset_cookie, 'Cookie has been changed by reset_session' );
1b41775a 46
47# this checks that cookie exists after a logout and redirect
48# Catalyst::Plugin::Authentication removes the user session (remove_persisted_user)
49($res, $c) = ctx_request(GET 'http://localhost/logout_redirect', Cookie => $cookie);
601be17a 50is($res->code, 302, 'redirected');
1b41775a 51is($res->header('Location'), 'http://localhost/from_logout_redirect', 'Redirected after logout_redirect');
52ok($res->header('Set-Cookie'), 'Cookie is there after redirect');
a2e23c04 53
54done_testing;