From: Graham Knop Date: Mon, 17 Aug 2020 06:57:21 +0000 (+0200) Subject: rewrite live app test to avoid Test::WWW::Mechanize::Catalyst X-Git-Tag: v0.18~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Session-State-Cookie.git;a=commitdiff_plain;h=9292790965e6522e20decdc384de7442b12809d3 rewrite live app test to avoid Test::WWW::Mechanize::Catalyst --- diff --git a/Makefile.PL b/Makefile.PL index 32135db..db00f65 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,7 +13,9 @@ my %META = ( } }, test => { requires => { - 'Test::More' => '0.88', + 'Catalyst::Test' => 0, + 'HTTP::Cookies' => 0, + 'Test::More' => '0.88', }, }, runtime => { diff --git a/t/live_app.t b/t/live_app.t index d2b4ef9..706c1e6 100644 --- a/t/live_app.t +++ b/t/live_app.t @@ -6,58 +6,92 @@ use lib "$Bin/lib"; use Test::More; -BEGIN { - eval { require Test::WWW::Mechanize::Catalyst }; - plan skip_all => - "This test requires Test::WWW::Mechanize::Catalyst in order to run" - if $@; - plan skip_all => 'Test::WWW::Mechanize::Catalyst >= 0.40 required' if $Test::WWW::Mechanize::Catalyst::VERSION < 0.40; - plan 'no_plan'; -} +use HTTP::Cookies; +use Catalyst::Utils (); + +use Catalyst::Test qw(CookieTestApp); + +my $jar = HTTP::Cookies->new; +my %cookie; +my $get = sub { + my $url = shift; + my $req = Catalyst::Utils::request($url); + $jar->add_cookie_header($req); + my $res = request($req); + $jar->extract_cookies($res); + + $jar->scan( sub { + if ($_[1] eq 'cookietestapp_session') { + @cookie{qw( + version + key + val + path + domain + port + path_spec + secure + expires + discard + hash + )} = @_; + } + } ); + + return $res; +}; + +my $res; + +$res = $get->('/stream'); +ok $res->is_success, 'get page'; +like $res->content, qr/hit number 1/, 'session data created'; + +my $expired = $cookie{expires}; + +$res = $get->('/page'); +ok $res->is_success, 'get page'; +like $res->content, qr/hit number 2/, 'session data restored'; + +$res = $get->('/page'); +ok $res->is_success, 'get page'; +like $res->content, qr/hit number 3/, 'session data restored'; -use Test::WWW::Mechanize::Catalyst qw/CookieTestApp/; - -my $m = Test::WWW::Mechanize::Catalyst->new; - -$m->get_ok( "http://localhost/stream", "get page" ); -$m->content_contains( "hit number 1", "session data created" ); +sleep 1; -my $expired; -$m->cookie_jar->scan( sub { $expired = $_[8]; } ); +$res = $get->('/page'); +ok $res->is_success, 'get page'; +like $res->content, qr/hit number 4/, 'session data restored'; -$m->get_ok( "http://localhost/page", "get page" ); -$m->content_contains( "hit number 2", "session data restored" ); +cmp_ok $expired, '<', $cookie{expires}, 'cookie expiration was extended'; +$expired = $cookie{expires}; -$m->get_ok( "http://localhost/stream", "get stream" ); -$m->content_contains( "hit number 3", "session data restored" ); +$res = $get->('/page'); +ok $res->is_success, 'get page'; +like $res->content, qr/hit number 5/, 'session data restored'; sleep 1; -$m->get_ok( "http://localhost/stream", "get page" ); -$m->content_contains( "hit number 4", "session data restored" ); +$res = $get->('/stream'); +ok $res->is_success, 'get stream'; +like $res->content, qr/hit number 6/, 'session data restored'; -my $updated_expired; -$m->cookie_jar->scan( sub { $updated_expired = $_[8]; } ); -cmp_ok( $expired, "<", $updated_expired, "cookie expiration was extended" ); +cmp_ok $expired, '<', $cookie{expires}, 'streaming also extends cookie'; -$expired = $m->cookie_jar->scan( sub { $expired = $_[8] } ); -$m->get_ok( "http://localhost/page", "get page again"); -$m->content_contains( "hit number 5", "session data restored (blah)" ); - -sleep 1; +$res = $get->('/deleteme'); +ok $res->is_success, 'get page'; +is $res->content, '1', 'session id changed'; -$m->get_ok( "http://localhost/stream", "get stream" ); -$m->content_contains( "hit number 6", "session data restored" ); +$res = $get->('https://localhost/page'); +ok $res->is_success, 'get page over HTTPS - init session'; +like $res->content, qr/hit number 1/, 'first hit'; -$m->cookie_jar->scan( sub { $updated_expired = $_[8]; } ); -cmp_ok( $expired, "<", $updated_expired, "streaming also extends cookie" ); +$res = $get->('http://localhost/page'); +ok $res->is_success, 'get page again over HTTP'; +like $res->content, qr/hit number 1/, 'first hit again - cookie not sent'; -$m->get_ok( "http://localhost/deleteme", "get page" ); -$m->content_is( 1, 'session id changed' ); +$res = $get->('https://localhost/page'); +ok $res->is_success, 'get page over HTTPS'; +like $res->content, qr/hit number 2/, 'second hit'; -$m->get_ok( "https://localhost/page", "get page over HTTPS - init session"); -$m->content_contains( "hit number 1", "first hit" ); -$m->get_ok( "http://localhost/page", "get page again over HTTP"); -$m->content_contains( "hit number 1", "first hit again - cookie not sent" ); -$m->get_ok( "https://localhost/page", "get page over HTTPS"); -$m->content_contains( "hit number 2", "second hit" ); +done_testing;