Added tests for sanely persistent flash. Will re-commit when I know if they work...
Kieren Diment [Wed, 8 Mar 2006 09:21:28 +0000 (09:21 +0000)]
t/lib/FlashTestApp.pm [new file with mode: 0644]
t/semi_persistent_flash.t [new file with mode: 0644]

diff --git a/t/lib/FlashTestApp.pm b/t/lib/FlashTestApp.pm
new file mode 100644 (file)
index 0000000..ff8395b
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -w
+
+package FlashTestApp;
+use Catalyst qw/Session Session::Store::File Session::State::Cookie/;
+
+use strict;
+use warnings;
+
+sub default : Private {
+    my ($self, $c) = @_;
+    $c->session;
+}
+
+    
+sub first : Global {
+    my ( $self, $c ) = @_;
+    if ( ! $c->flash->{is_set}) {
+        $c->stash->{message} = "flash is not set";
+        $c->stash->{is_set} = 1;
+    }
+}
+
+sub second : Global {
+    my ( $self, $c ) = @_;
+    if ($c->flash->{is_set} == 1){
+        $c->stash->{message} = "flash set first time";
+        $c->flash->{is_set}++;
+    }
+}
+
+sub third : Global {
+    my ( $self, $c ) = @_;
+    if ($c->flash->{is_set} == 2) {
+        $c->stash->{message} = "flash set second time";
+        $c->flash->{is_set} = 2;
+    }
+}
+
+sub fourth : Global {
+    my ( $self, $c ) = @_;
+    if ($c->flash->{is_set} == 2) {
+        $c->stash->{message} = "flash set 3rd time, same val as prev."
+    }
+}
+
+sub fifth : Global {
+    my ( $self, $c ) = @_;
+    $c->forward('/first');
+}
+
+sub end : Local {
+    my ($self, $c) = @_;
+    $c->res->output($c->stash->{message});
+}
+
+
+__PACKAGE__->setup;
+
+__PACKAGE__;
+
diff --git a/t/semi_persistent_flash.t b/t/semi_persistent_flash.t
new file mode 100644 (file)
index 0000000..76b968b
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/perl -w
+use strict;
+
+use Test::More;
+
+BEGIN {
+    eval { require Catalyst::Plugin::Session::State::Cookie }
+        or plan skip_all =>
+            "Catalyst::Plugin::Session::State::Cookie is required for this test";
+
+    eval { require Catalyst::Plugin::Session::Store::File}
+        or plan skil_all =>
+            'Catalyst::Plugin::Session::Store::File is required for this test';
+    eval { require Test::WWW::Mechanize::Catalyst }
+      or plan skip_all =>
+      'Test::WWW::Mechanize::Catalyst is required for this test';
+
+    plan tests => '10';
+
+}
+
+use lib "t/lib";
+use Test::WWW::Mechanize::Catalyst 'FlashTestApp';
+
+my $ua = Test::WWW::Mechanize::Catalyst->new;
+
+# flash absent for initial request
+$ua->get_ok( "http://localhost/first") for $ua;
+$ua->content_contains( "flash is not set", "not set");
+
+# present for 1st req.
+$ua->get_ok( "http://localhost/second") for $ua;
+$ua->content_contains( "flash set first time", "set first");
+
+# should be the same 2nd req.
+$ua->get_ok( "http://localhost/third") for $ua;
+$ua->content_contains( "flash set second time", "set second");
+
+# and the third request, flash->{is_set} has the same value as 2nd.
+$ua->get_ok( "http://localhost/fourth") for $ua;
+$ua->content_contains( "flash set 3rd time, same val as prev.", "set third");
+
+
+# and should be absent again for the 4th req.
+$ua->get_ok( "http://localhost/fifth") for $ua;
+$ua->content_contains( "flash is not", "flash has gone");
+