[dancer-users] Problem with Dancer::Test, forward and session

Marco Pessotto melmothx at gmail.com
Mon Dec 3 09:45:27 GMT 2012


Hello all!

I think, but not sure, I've hit a bug in Dancer::Test.

This is the minimal example:

#### cut here #####
package DancerMelmothBug;
use Dancer ':syntax';

post '/login' => sub {
  if (params->{username} and params->{password}) {
    session 'auth' => 1;
  }
};

get '/login' => sub {
  return "login screen";
};


post '/works' => sub {
  return redirect '/login' unless session->{auth};
  warn "works!";
  return "it works";
};

post '/first' => sub {
  return redirect '/login' unless session->{auth};
  warn "forwarding\n";
  forward '/second';
};

post '/second' => sub {
  return redirect '/login' unless session->{auth};
  warn "done\n";
  return "OK";
};

1;

###### cut here ##########

This is the test script

##### cut here #####

use Test::More tests => 2;

use DancerMelmothBug;
use Dancer::Test;
use Data::Dumper;
use HTTP::Headers;

my $login_creds = {
		   username => "tester",
		   password => "provaprova",
		  };

my $loggingin = dancer_response (POST => '/login', {
   						    params => $login_creds
						   });	
my $sessionkey = $loggingin->header('Set-Cookie');

if ($sessionkey =~ m/(dancer\.session=.*?;)/) {
  $sessionkey = $1;
  print "Got the session key $sessionkey\n";
}

my $loggedinheader = HTTP::Headers->new(
					cookie => $sessionkey
				       );

my $response;

$response = dancer_response(POST => "/works", {
					       params => {},
					       headers => $loggedinheader,
					      });

ok($response->content);


$response = dancer_response(POST => "/first", {
					       params => {},
					       headers => $loggedinheader,
					      });

ok($response->content);
  


##### cut here ####

Running the test yields:

perl test.t
1..2
Got the session key dancer.session=4079393373042855076725642369476525;
works! at DancerMelmothBug.pm line 17.
ok 1
forwarding
not ok 2
#   Failed test at test.t line 42.
# Looks like you failed 1 test of 2.

So, it looks like it fails to get the session after the forwarding.

It also seems that the bug is present only in the testing environment,
because the running app is reacting properly.

Best wishes and thanks in advance.

-- 
Marco


More information about the dancer-users mailing list