| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | use strict; |
| 4 | |
| 5 | use File::Spec; |
| 6 | use File::Temp (); |
| 7 | |
| 8 | use Test::More tests => 20; |
| 9 | |
| 10 | use_ok('Apache::Session::Wrapper'); |
| 11 | |
| 12 | |
| 13 | my %params = |
| 14 | ( class => 'Flex', |
| 15 | store => 'File', |
| 16 | lock => 'Null', |
| 17 | generate => 'MD5', |
| 18 | serialize => 'Storable', |
| 19 | ); |
| 20 | |
| 21 | $params{directory} = File::Temp::tempdir( CLEANUP => 1 ); |
| 22 | |
| 23 | # We do this to generate a pre-existing session id that will be used |
| 24 | # in the tests below. |
| 25 | use Apache::Session::Flex; |
| 26 | my %session; |
| 27 | tie %session, 'Apache::Session::Flex', undef, |
| 28 | { Store => 'File', |
| 29 | Lock => 'Null', |
| 30 | Generate => 'MD5', |
| 31 | Serialize => 'Storable', |
| 32 | Directory => $params{directory}, |
| 33 | }; |
| 34 | $session{bar}{baz} = 1; |
| 35 | my $id = $session{_session_id}; |
| 36 | untie %session; |
| 37 | |
| 38 | { |
| 39 | my $w = Apache::Session::Wrapper->new(%params); |
| 40 | |
| 41 | ok( tied %{ $w->session }, 'session is a tied thing' ); |
| 42 | isa_ok( tied %{ $w->session }, 'Apache::Session' ); |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | my $w = Apache::Session::Wrapper->new(%params); |
| 47 | |
| 48 | $w->session( session_id => $id )->{foo} = 'bar'; |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | my $w = Apache::Session::Wrapper->new(%params); |
| 53 | |
| 54 | is( $w->session( session_id => $id )->{foo}, 'bar', |
| 55 | 'stored a value in the session' ); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | my $w = Apache::Session::Wrapper->new(%params); |
| 60 | |
| 61 | eval { $w->session( session_id => 'abcdef' ) }; |
| 62 | |
| 63 | ok( ! $@, 'invalid session id is allowed by default' ); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | my $w = Apache::Session::Wrapper->new( %params, allow_invalid_id => 0 ); |
| 68 | |
| 69 | eval { $w->session( session_id => 'abcdef' ) }; |
| 70 | my $e = $@; |
| 71 | |
| 72 | ok( $e, 'invalid session id caused an error' ); |
| 73 | isa_ok( $e, 'Apache::Session::Wrapper::Exception::NonExistentSessionID' ); |
| 74 | } |
| 75 | |
| 76 | { |
| 77 | my $w = Apache::Session::Wrapper->new(%params); |
| 78 | |
| 79 | $w->session( session_id => $id )->{bar}{baz} = 50; |
| 80 | |
| 81 | is( $w->session( session_id => $id )->{bar}{baz}, 50, |
| 82 | 'always write - in memory value' ); |
| 83 | } |
| 84 | |
| 85 | { |
| 86 | my $w = Apache::Session::Wrapper->new(%params); |
| 87 | |
| 88 | is( $w->session( session_id => $id )->{bar}{baz}, 50, |
| 89 | 'always write - stored value' ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | { |
| 94 | my $w = Apache::Session::Wrapper->new( %params, always_write => 0 ); |
| 95 | |
| 96 | $w->session( session_id => $id )->{bar}{baz} = 100; |
| 97 | |
| 98 | is( $w->session( session_id => $id )->{bar}{baz}, 100, |
| 99 | 'always write is off - in memory value' ); |
| 100 | } |
| 101 | |
| 102 | { |
| 103 | my $w = Apache::Session::Wrapper->new( %params, always_write => 0 ); |
| 104 | |
| 105 | is( $w->session( session_id => $id )->{bar}{baz}, 50, |
| 106 | 'always write is off - stored value' ); |
| 107 | |
| 108 | $w = Apache::Session::Wrapper->new( %params, session_id => $id ); |
| 109 | is( $w->session->{_session_id}, $id, 'id matches session id given to new()' ); |
| 110 | } |
| 111 | |
| 112 | { |
| 113 | my $w = Apache::Session::Wrapper->new( %params ); |
| 114 | |
| 115 | $w->session( session_id => $id )->{quux} = 100; |
| 116 | |
| 117 | $w->delete_session; |
| 118 | |
| 119 | is( $w->session( session_id => $id )->{quux}, undef, |
| 120 | 'session is empty after delete_session' ); |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | no warnings 'redefine'; |
| 125 | # so attempt to connect to MySQL doesn't happen |
| 126 | local *Apache::Session::Wrapper::_make_session = sub {}; |
| 127 | |
| 128 | my $wrapper = |
| 129 | eval { local $^W = 0; |
| 130 | Apache::Session::Wrapper->new( class => 'Flex', |
| 131 | store => 'MySQL', |
| 132 | lock => 'Null', |
| 133 | generate => 'MD5', |
| 134 | serialize => 'Storable', |
| 135 | data_source => 'foo', |
| 136 | user_name => 'bar', |
| 137 | password => 'baz', |
| 138 | ) }; |
| 139 | unlike( $@, qr/parameters/, 'pass correct parameters for MySQL flex' ); |
| 140 | |
| 141 | is( $wrapper->{params}{DataSource}, 'foo', 'DataSource is foo' ); |
| 142 | is( $wrapper->{params}{UserName}, 'bar', 'UserName is bar' ); |
| 143 | is( $wrapper->{params}{Password}, 'baz', 'Password is baz' ); |
| 144 | } |
| 145 | |
| 146 | { |
| 147 | eval { local $^W = 0; |
| 148 | Apache::Session::Wrapper->new( class => 'Postgres', |
| 149 | data_source => 'foo', |
| 150 | user_name => 'foo', |
| 151 | password => 'foo', |
| 152 | commit => 0, |
| 153 | ) }; |
| 154 | unlike( $@, qr/parameters/, 'first param set for Pg' ); |
| 155 | } |
| 156 | |
| 157 | { |
| 158 | my $dbh = bless {}, 'DBI'; |
| 159 | |
| 160 | eval { local $^W = 0; |
| 161 | Apache::Session::Wrapper->new( class => 'Postgres', |
| 162 | handle => $dbh, |
| 163 | commit => 0, |
| 164 | ) }; |
| 165 | unlike( $@, qr/parameters/, 'second param set for Pg' ); |
| 166 | } |
| 167 | |
| 168 | { |
| 169 | my $dbh = bless {}, 'DBI'; |
| 170 | |
| 171 | eval { Apache::Session::Wrapper->new( class => 'Postgres', |
| 172 | commit => 0, |
| 173 | ) }; |
| 174 | like( $@, qr/required parameters.+missing: handle/, 'incomplete params for Pg' ); |
| 175 | } |