Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Test::More tests => 2;
- use Plack::Test;
- use Plack::Builder;
- use Plack::Middleware::HTTPExceptions;
- use HTTP::Request::Common;
- use HTTP::Throwable::Factory qw(http_throw);
- sub psgi_app {
- my $env = shift;
- if ($env->{PATH_INFO} eq '/api') {
- http_throw(NotAcceptable => {});
- }
- };
- my $psgi_app = \&psgi_app;
- # temporary holding spot for HTTP::Throwable to allow Throwable-based error responses
- # (by default, Dancer2 will rethrow any exception it receives as 500, so override this behavior)
- our $_SAVEERR;
- package DancerApp {
- use Scalar::Util qw(blessed);
- use HTTP::Throwable::Factory qw(http_throw);
- use Dancer2;
- set serializer => 'JSON';
- hook after_error => sub {
- my $response = shift;
- if (my $e = $_SAVEERR) {
- undef $_SAVEERR;
- status $e->{'status_code'};
- halt;
- }
- };
- hook on_route_exception => sub {
- my ($app, $error) = @_;
- # if a Throwable is present, then save it
- if (blessed $error && $error->does('HTTP::Throwable')) {
- $_SAVEERR = $error;
- }
- };
- get '/api' => sub {
- http_throw(NotAcceptable => {});
- };
- };
- my $dancer_app = DancerApp->to_app;
- my $plack_psgi = builder {
- enable "HTTPExceptions";
- $psgi_app
- };
- my $plack_dancer = builder {
- $dancer_app
- };
- # PSGI:
- my $t = Plack::Test->create($plack_psgi);
- my $r = $t->request(GET '/api');
- is($r->code, 406, 'GET /api');
- # DANCER:
- my $t2 = Plack::Test->create($plack_dancer);
- my $r = $t2->request(GET '/api');
- is($r->code, 406, 'GET /api');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement