Commits

Fabrice Gabolde committed 664786d

Session tests.

Comments (0)

Files changed (1)

t/plugins/session-yaml.t

+#!perl
+
+use strict;
+use warnings;
+use 5.010;
+use Carp;
+
+use Test::More;
+use Test::SetupTeardown;
+
+use Module::Load;
+use File::Temp qw/tempdir/;
+use Path::Class;
+use IO::Scalar;
+use Plack::Request;
+
+use Floop;
+
+eval { load YAML };
+if (my $error = $@) {
+    plan skip_all => 'YAML is not available';
+}
+
+use_ok 'Floop::Plugin::Session';
+
+my $temp_dir_name = tempdir();
+my $logsink;
+
+sub setup {
+    undef $logsink;
+}
+
+sub teardown {
+    dir($temp_dir_name)->rmtree;
+}
+
+my $environment = Test::SetupTeardown->new(setup => \&setup,
+                                           teardown => \&teardown);
+
+$environment->run_test('session management', sub {
+
+    my $app = Floop->new(
+        work_directory => $temp_dir_name,
+        logger => Floop::Logger::Console->new(access_log_handle => IO::Scalar->new(\$logsink),
+                                              error_log_handle => IO::Scalar->new(\$logsink)));
+    $app->install_plugin('Floop::Plugin::Session', 'YAML');
+
+    can_ok($app, 'create_session');
+    my $session = $app->create_session;
+    isa_ok($session, 'Floop::Session', 'session '.$session->session_id);
+    is($session->app, $app);
+
+    can_ok($app, 'fetch_session');
+    is($app->fetch_session($session->session_id), undef);
+
+    $session->set(foo => 'bar');
+
+    can_ok($app, 'write_session');
+    $app->write_session($session);
+    is_deeply($app->fetch_session($session->session_id)->as_hashref, { foo => 'bar' });
+
+    can_ok($app, 'destroy_session');
+    $app->destroy_session($session);
+    is($app->fetch_session($session->session_id), undef);    
+
+                       });
+
+$environment->run_test('session and context', sub {
+
+    my $app = Floop->new(
+        work_directory => $temp_dir_name,
+        logger => Floop::Logger::Console->new(access_log_handle => IO::Scalar->new(\$logsink),
+                                              error_log_handle => IO::Scalar->new(\$logsink)));
+    $app->install_plugin('Floop::Plugin::Session', 'YAML');
+
+    my $session = $app->create_session;
+    $session->set(foo => 'bar');
+    $app->write_session($session);
+
+    can_ok($app, 'session_of_context');
+    my $context = $app->context_class->new(request => Plack::Request->new({
+        HTTP_COOKIE => sprintf(q{%s=%s}, $app->session_cookie_name, $session->session_id),
+        }),
+                                           app => $app);
+
+    my $session_from_app = $app->session_of_context($context);
+    isa_ok($session_from_app, 'Floop::Session');
+    is($session_from_app->get('foo'), 'bar');
+
+    my $session_from_context = $context->session;
+    isa_ok($session_from_app, 'Floop::Session');
+    is($session_from_app->get('foo'), 'bar');
+
+                       });
+
+done_testing;