|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use Test::More; |
| 4 | +use MetaCPAN::Web::Test; |
| 5 | +use MIME::Base64; |
| 6 | +use Try::Tiny; |
| 7 | +use URI::Escape; |
| 8 | + |
| 9 | +{ |
| 10 | + package ## no critic (Package) |
| 11 | + MetaCPAN::Web::Controller::TestSession; |
| 12 | + use Moose; |
| 13 | + BEGIN { extends 'MetaCPAN::Web::Controller'; } |
| 14 | + |
| 15 | + sub index : Path { |
| 16 | + my ( $self, $c ) = @_; |
| 17 | + if ( my $flavor = $c->req->param('flavor') ) { |
| 18 | + $c->req->session->set( flavor => $flavor ); |
| 19 | + } |
| 20 | + $c->res->body('yum'); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +test_psgi app, sub { |
| 25 | + my $cb = shift; |
| 26 | + |
| 27 | + subtest 'verify cookie handling' => sub { |
| 28 | + my $url = q[/testsession]; |
| 29 | + |
| 30 | + my $cookie = get_cookie( $cb, $url ); |
| 31 | + |
| 32 | + my $biscuit = get_cookie( $cb, "$url?flavor=snickerdoodle", $cookie ); |
| 33 | + |
| 34 | + isnt $cookie, $biscuit, 'cookie has been baked'; |
| 35 | + |
| 36 | + is get_cookie( $cb, $url, $biscuit ), $biscuit, q[cookie preserved]; |
| 37 | + |
| 38 | + isnt get_cookie( $cb, $url ), $biscuit, q[cookie has been eaten :'(]; |
| 39 | + |
| 40 | + my $spoiled = $biscuit; |
| 41 | + is get_cookie( $cb, $url, $biscuit ), $biscuit, q[cookie is back]; |
| 42 | + |
| 43 | + $spoiled =~ s/:([^:])/:=/; # Chew cookie. |
| 44 | + isnt get_cookie( $cb, $url, $spoiled ), $spoiled, q[cookie went bad]; |
| 45 | + |
| 46 | + $spoiled = $biscuit; |
| 47 | + $spoiled =~ s/(.)$/=/; # Chew signature. |
| 48 | + isnt get_cookie( $cb, $url, $spoiled ), $spoiled, q[siggy went bad]; |
| 49 | + }; |
| 50 | + |
| 51 | +}; |
| 52 | + |
| 53 | +sub get_cookie { |
| 54 | + my ( $cb, $url, $send_cookie ) = @_; |
| 55 | + my $req = HTTP::Request->new( |
| 56 | + GET => $url, |
| 57 | + [ |
| 58 | + Cookie => $send_cookie, |
| 59 | + ] |
| 60 | + ); |
| 61 | + ok( my $res = $cb->($req), $url ); |
| 62 | + is( $res->code, 200, 'code 200' ) |
| 63 | + or diag $res->content; |
| 64 | + |
| 65 | + my $cookie = URI::Escape::uri_unescape( |
| 66 | + ( $res->header('set-cookie') =~ /([^;]+)/ )[0] ); |
| 67 | + |
| 68 | + like $cookie, qr/:\w+[=]{0,2}:/, 'looks like cookie'; |
| 69 | + |
| 70 | + #diag $cookie; |
| 71 | + |
| 72 | + return $cookie; |
| 73 | +} |
| 74 | +done_testing; |
0 commit comments