Skip to content

Instantly share code, notes, and snippets.

@schwern
Created January 14, 2011 06:44
Show Gist options
  • Save schwern/779280 to your computer and use it in GitHub Desktop.
Save schwern/779280 to your computer and use it in GitHub Desktop.
Why is this failing?
{
package SD::MW::Auth::Anonymous;
use Mouse;
use MouseX::Foreign "Plack::Middleware";
use Plack::Request;
use Plack::Response;
use CGI::Simple::Cookie;
has session_param =>
is => 'rw',
isa => 'Str',
lazy => 1,
default => sub {
return "session";
};
has cookie_expires =>
is => 'rw',
isa => 'Str',
lazy => 1,
default => sub {
return '+5h';
};
has cookie_path =>
is => 'rw',
isa => 'Str',
;
has cookie_secure =>
is => 'rw',
isa => 'Str',
lazy => 1,
default => sub {
return 0;
};
my $Session_Counter = 2;
sub call {
my($self, $env) = @_;
my $session_param = $self->session_param;
my $session_key = Plack::Request->new($env)->cookies->{$session_param};
my $had_key = $session_key;
$session_key ||= $Session_Counter++;
$env->{$session_param} = $session_key;
my $res = $self->app->($env);
# This is to let CGI::Simple::Cookie translate the
# expires "+5h" to an HTTP date which Plack won't do.
my $cookie = CGI::Simple::Cookie->new(
-name => $session_param,
-value => $session_key,
-expires => $self->cookie_expires,
-path => $self->cookie_path,
-secure => $self->cookie_secure,
);
my $response = Plack::Response->new(@$res);
# HA HA HA! Cookies on dowels.
$response->cookies->{$session_param} = {
value => $cookie->value,
expires => $cookie->expires,
path => $cookie->path,
secure => $cookie->secure,
};
$res->[1] = $response->finalize->[1];
return $res;
}
}
use lib 't/lib';
use v5.10;
use strict;
use warnings;
use Test::More;
use HTTP::Request::Common;
use LWP::UserAgent;
use HTTP::Cookies;
use Plack::Test;
use Plack::Builder;
my $CLASS = 'SD::MW::Auth::Anonymous';
$INC{"SD/MW/Auth/Anonymous.pm"} = 1;
note 'if not cookie exists, one gets created'; {
my $cookie_name = 'cookie_goes_into_mouth';
my $app = sub {
my $env = shift;
return [ 200, [ ], [ $env->{$cookie_name} ] ]
};
$app = builder {
enable "+$CLASS",
session_param => $cookie_name,
cookie_path => '/and/stuff',
cookie_expires => '+4h';
$app;
};
$Plack::Test::Impl = 'Server';
my $ua = LWP::UserAgent->new;
$ua->cookie_jar( HTTP::Cookies->new );
test_psgi ua => $ua, app => $app, client => sub {
my $cb = shift;
note "First request";
my $res = $cb->(GET '/');
my $expires = time + 4 * 60 * 60; # 4 hours in the future
is $res->code, 200, "response code";
my $cookies = get_cookies($ua->cookie_jar);
my $cookie = $cookies->{$cookie_name};
ok $cookie, "have a cookie";
is $cookie->{expires}, $expires, ' expires';
is $cookie->{path}, "/and/stuff", ' path';
my $session_key = $cookie->{value};
ok $session_key, " session_key";
is $res->content, $session_key, " session set in the enviornment";
note "Second request";
$res = $cb->(GET '/');
is $res->code, 200, "response code";
$cookies = get_cookies($ua->cookie_jar);
$cookie = $cookies->{$cookie_name};
ok $cookie, "have a cookie";
is $cookie->{value}, $session_key, "got the same session again";
};
}
# Wrapper around the bletcherous but correct HTTP::Cookies
# to extract cookies from a response header.
sub get_cookies {
my $jar = shift;
state $keys = [qw(
version
name
value
path
domain
port
path_spec
secure
expires
discard
)];
# require HTTP::Cookies;
# my $jar = HTTP::Cookies->new;
# $jar->extract_cookies( $res );
my %cookies;
$jar->scan(sub {
my %cookie;
@cookie{@$keys} = @_[0..$#{$keys}];
$cookies{ $cookie{name} } = \%cookie;
});
return \%cookies;
}
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment