now prepare_action is in Context - fixing test libs (for example for test t/aggregate...
[catagits/Catalyst-Runtime.git] / t / lib / TestApp / Context.pm
1 package TestApp::Context;
2 use Moose;
3 extends 'Catalyst::Context'; 
4
5 if (eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) {
6     with 'CatalystX::LeakChecker';
7
8     has leaks => (
9         is      => 'ro',
10         default => sub { [] },
11     );
12 }
13
14 sub found_leaks {
15     my ($ctx, @leaks) = @_;
16     push @{ $ctx->leaks }, @leaks;
17 }
18
19 sub count_leaks {
20     my ($ctx) = @_;
21     return scalar @{ $ctx->leaks };
22 }
23
24 sub execute {
25     my $c      = shift;
26     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
27     my $action = $_[1]->reverse;
28
29     my $method;
30
31     if ( $action =~ /->(\w+)$/ ) {
32         $method = $1;
33     }
34     elsif ( $action =~ /\/(\w+)$/ ) {
35         $method = $1;
36     }
37     elsif ( $action =~ /^(\w+)$/ ) {
38         $method = $action;
39     }
40
41     if ( $class && $method && $method !~ /^_/ ) {
42         my $executed = sprintf( "%s->%s", $class, $method );
43         my @executed = $c->response->headers->header('X-Catalyst-Executed');
44         push @executed, $executed;
45         $c->response->headers->header(
46             'X-Catalyst-Executed' => join ', ',
47             @executed
48         );
49     }
50     no warnings 'recursion';
51     return $c->SUPER::execute(@_);
52 }
53
54 after prepare_action => sub{
55     my $c = shift;
56     $c->res->header( 'X-Catalyst-Action' => $c->req->action );
57 };
58
59 1;
60