Skip to content

Commit

Permalink
Merge branch 'JaHIY-fix-halt'
Browse files Browse the repository at this point in the history
  • Loading branch information
veryrusty committed Aug 23, 2015
2 parents ab998c1 + 0266c19 commit 8abbfb1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -26,6 +26,8 @@
* GH #950: Decode characters in param keys (Patrick Zimmermann)
* GH #914: Include stack trace on default error page when
show_errors is true. (Vernon)
* GH #980, #981: halt keyword sets response content if provided,
as Dancer 1 does. (Achilles Kars)

0.161000 2015-07-08 14:57:16+02:00 Europe/Amsterdam

Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer2/Core/App.pm
Expand Up @@ -1077,7 +1077,7 @@ sub redirect {

sub halt {
my $self = shift;
$self->response->halt;
$self->response->halt( @_ );

# Short citcuit any remaining hook/route code
$self->has_with_return
Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer2/Core/DSL.pm
Expand Up @@ -193,7 +193,7 @@ sub prefix {
: $app->lexical_prefix(@_);
}

sub halt { shift->app->halt }
sub halt { shift->app->halt(@_) }

sub del { shift->_normalize_route( [qw/delete /], @_ ) }
sub get { shift->_normalize_route( [qw/get head/], @_ ) }
Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer2/Core/Error.pm
Expand Up @@ -310,7 +310,7 @@ sub throw {
$self->has_app &&
$self->app->execute_hook('core.error.after', $self->response);

$self->response->halt(1);
$self->response->is_halted(1);
return $self->response;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Dancer2/Core/Response.pm
Expand Up @@ -82,7 +82,11 @@ has is_halted => (
default => sub {0},
);

sub halt { shift->is_halted(1) }
sub halt {
my ( $self, $content ) = @_;
$self->content( $content ) if @_ > 1;
$self->is_halted(1);
}

has status => (
is => 'rw',
Expand Down
84 changes: 84 additions & 0 deletions t/dsl/halt_with_param.t
@@ -0,0 +1,84 @@
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;

subtest 'halt with parameter within routes' => sub {
{

package App;
use Dancer2;

get '/' => sub { 'hello' };
get '/halt' => sub {
header 'X-Foo' => 'foo';
halt;
};
get '/shortcircuit' => sub {
halt('halted');
redirect '/'; # won't get executed as halt returns immediately.
};
}

my $app = App->to_app;
is( ref $app, 'CODE', 'Got app' );

test_psgi $app, sub {
my $cb = shift;

{
my $res = $cb->( GET '/shortcircuit' );
is( $res->code, 200, '[/shortcircuit] Correct status' );
is( $res->content, 'halted', '[/shortcircuit] Correct content' );

}

{
my $res = $cb->( GET '/halt' );

is(
$res->server,
"Perl Dancer2 " . Dancer2->VERSION,
'[/halt] Correct Server header',
);

is(
$res->headers->header('X-Foo'),
'foo',
'[/halt] Correct X-Foo header',
);
}
};

};

subtest 'halt with parameter in before hook' => sub {
{
package App;
use Dancer2;

hook before => sub {
halt('I was halted') if request->dispatch_path eq '/shortcircuit';
};

}

my $app = App->to_app;
is( ref $app, 'CODE', 'Got app' );

test_psgi $app, sub {
my $cb = shift;
my $res = $cb->( GET '/shortcircuit' );

is( $res->code, 200, '[/shortcircuit] Correct code with before hook' );
is(
$res->content,
'I was halted',
'[/shortcircuit] Correct content with before hook',
);
};
};

done_testing;

0 comments on commit 8abbfb1

Please sign in to comment.