-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework halt to use with_return from Return::MultiLevel.
Once #485 is merged, use the with_return handler to immediately stop processing a before hook or route handler. Includes tests for use in a reoute handler and a before hook. Resolves #472.
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
|
||
subtest 'halt within routes' => sub { | ||
{ | ||
|
||
package App; | ||
use Dancer2; | ||
|
||
get '/' => sub { 'hello' }; | ||
get '/halt' => sub { | ||
header 'X-Foo' => 'foo'; | ||
halt; | ||
}; | ||
get '/shortcircuit' => sub { | ||
context->response->content('halted'); | ||
halt; | ||
redirect '/'; # won't get executed as halt returns immediately. | ||
}; | ||
} | ||
use Dancer2::Test apps => ['App']; | ||
|
||
response_status_is [ GET => '/shortcircuit' ] => 200; | ||
response_content_is [ GET => '/shortcircuit' ] => "halted"; | ||
|
||
my $expected_headers = [ | ||
'X-Foo' => 'foo', | ||
'Server' => "Perl Dancer2 $Dancer2::VERSION", | ||
]; | ||
response_headers_include [ GET => '/halt' ] => $expected_headers; | ||
|
||
}; | ||
|
||
subtest 'halt in before halt' => sub { | ||
{ | ||
package App; | ||
use Dancer2; | ||
|
||
hook before => sub { | ||
my $context = shift; | ||
$context->response->content('I was halted'); | ||
halt if $context->request->dispatch_path eq '/shortcircuit'; | ||
}; | ||
|
||
} | ||
|
||
response_status_is [ GET => '/shortcircuit' ] => 200; | ||
response_content_is [ GET => '/shortcircuit' ] => "I was halted"; | ||
|
||
}; | ||
|
||
done_testing; |