From: Oskari Okko Ojala Date: Wed, 11 May 2011 12:14:47 +0000 (+0300) Subject: Tests for redirects X-Git-Tag: 5.90003~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=d67d5f8734a887d0dbd2eeaa5623cf1116a4b626 Tests for redirects --- diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index ce3ee75..ed51778 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -82,6 +82,28 @@ sub body_semipredicate : Local { $c->res->body('Body'); } + +sub test_redirect :Global { + my ($self, $c) = @_; + # Don't set content_type + # Don't set body + $c->res->redirect('/go_here'); +} + +sub test_redirect_with_contenttype :Global { + my ($self, $c) = @_; + # set content_type but don't set body + $c->res->content_type('image/jpeg'); + $c->res->redirect('/go_here'); +} + +sub test_redirect_with_content :Global { + my ($self, $c) = @_; + $c->res->content_type('text/plain'); + $c->res->body('Please kind sir, I beg you to go to /go_here.'); + $c->res->redirect('/go_here'); +} + sub end : Private { my ($self,$c) = @_; } diff --git a/t/live_redirect_body.t b/t/live_redirect_body.t new file mode 100644 index 0000000..b1e5b4e --- /dev/null +++ b/t/live_redirect_body.t @@ -0,0 +1,45 @@ +use FindBin; +use lib "$FindBin::Bin/lib"; +use Catalyst::Test 'TestApp', {default_host => 'default.com'}; +use Catalyst::Request; + +use Test::More tests => 12; + + # test redirect + { + my $request = + HTTP::Request->new( GET => 'http://localhost:3000/test_redirect' ); + + ok( my $response = request($request), 'Request' ); + is( $response->code, 302, 'Response Code' ); + + # When no body and no content_type has been set, redirecting should set both. + is( $response->header( 'Content-Type' ), 'text/html; charset=utf-8', 'Content Type' ); + like( $response->content, qr//, 'Content contains HTML body' ); + } + + # test redirect without a body and but with a content_type set explicitly by the developer + { + my $request = + HTTP::Request->new( GET => 'http://localhost:3000/test_redirect_with_contenttype' ); + + ok( my $response = request($request), 'Request' ); + is( $response->code, 302, 'Response Code' ); + + # When the developer has not set content body, we set it. The content type must always match the body, so it should be overwritten. + is( $response->header( 'Content-Type' ), 'text/html; charset=utf-8', 'Content Type' ); + like( $response->content, qr//, 'Content contains HTML body' ); + } + + # test redirect without a body and but with a content_type set explicitly by the developer + { + my $request = + HTTP::Request->new( GET => 'http://localhost:3000/test_redirect_with_content' ); + + ok( my $response = request($request), 'Request' ); + is( $response->code, 302, 'Response Code' ); + + # When the developer sets both the content body and content type, the set content body and content_type should get through. + is( $response->header( 'Content-Type' ), 'text/plain', 'Content Type' ); + like( $response->content, qr/kind sir/, 'Content contains content set by the Controller' ); + }