Mojolicious-Plugin-PlackMiddleware

 view release on metacpan or  search on metacpan

xt/compat/dispatcher_lite_app.t  view on Meta::CPAN

# Cleared response for /res.txt
hook before_routes => sub {
  my $c = shift;
  return unless $c->req->url->path->contains('/res.txt') && $c->param('route');
  $c->tx->res(Mojo::Message::Response->new);
};

# Set additional headers for static files
hook after_static => sub {
  my $c = shift;
  $c->res->headers->cache_control('max-age=3600, must-revalidate');
};

# Make controller available as $_
hook around_action => sub {
  my ($next, $c) = @_;
  local $_ = $c;
  return $next->();
};

# Plugin for rendering return values

xt/compat/dispatcher_lite_app.t  view on Meta::CPAN

# Allow rendering of return value
under '/' => {return => 1} => sub {1};

# Return and render argument
get '/' => sub { return pop } => 'works';

my $t = Test::Mojo->new;

# Normal route
$t->get_ok('/')->status_is(200)
  ->header_isnt('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is('works');

# Normal static file
$t->get_ok('/test.txt')->status_is(200)
  ->header_is('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is("Normal static file!\n");

# Override static file
$t->get_ok('/hello.txt')->status_is(200)
  ->content_is('Custom static file works!');

# Custom dispatcher
$t->get_ok('/custom?a=works+too')->status_is(205)->content_is('works too');

# Static file
$t->get_ok('/res.txt')->status_is(200)
  ->header_is('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is("Static response!\n");

# Custom response
$t->get_ok('/res.txt?route=1')->status_is(202)
  ->header_isnt('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is('Custom response!');

# Conditional response
$t->get_ok('/res.txt?route=1&res=1')->status_is(201)
  ->header_isnt('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is('Conditional response!');

# Another custom dispatcher
$t->get_ok('/custom_too')->status_is(200)
  ->header_isnt('Cache-Control' => 'max-age=3600, must-revalidate')
  ->content_is('this works too');

# First wrapper
$t->get_ok('/wrap')->status_is(200)->content_is('Wrapped!');

# Second wrapper
$t->get_ok('/wrap/again')->status_is(200)->content_is('Wrapped again!');

# Internal redirect to root
$t->get_ok('/not_found')->status_is(200)->content_is('works');



( run in 1.695 second using v1.01-cache-2.11-cpan-39bf76dae61 )