update distar url
[catagits/Catalyst-Runtime.git] / t / lib / TestApp.pm
CommitLineData
dd4e6fd2 1package TestApp;
dd4e6fd2 2use strict;
836e1134 3use Catalyst qw/
3d101ef9 4 Test::MangleDollarUnderScore
88e5a8b0 5 Test::Errors
6 Test::Headers
836e1134 7 Test::Plugin
4ca147fa 8 Test::Inline
836e1134 9 +TestApp::Plugin::FullyQualified
083ee5d9 10 +TestApp::Plugin::AddDispatchTypes
e5210a95 11 +TestApp::Role
836e1134 12/;
1408d0a4 13use Catalyst::Utils;
e7399d8b 14use Class::Load 'try_load_class';
dd4e6fd2 15
d9d8aa51 16use Moose;
eefc03e1 17use namespace::clean -except => [ 'meta' ];
d9d8aa51 18
950c7852 19# -----------
20# t/aggregate/unit_core_ctx_attr.t pukes until lazy is true
21package Greeting;
22use Moose;
23sub hello_notlazy { 'hello there' }
24sub hello_lazy { 'hello there' }
25
26package TestApp;
27has 'my_greeting_obj_notlazy' => (
28 is => 'ro',
29 isa => 'Greeting',
30 default => sub { Greeting->new() },
31 handles => [ qw( hello_notlazy ) ],
32 lazy => 0,
33);
34has 'my_greeting_obj_lazy' => (
35 is => 'ro',
36 isa => 'Greeting',
37 default => sub { Greeting->new() },
38 handles => [ qw( hello_lazy ) ],
39 lazy => 1,
40);
41# -----------
42
dd4e6fd2 43our $VERSION = '0.01';
44
88e5a8b0 45TestApp->config(
46 name => 'TestApp',
47 root => '/some/dir',
48 use_request_uri_for_path => 1,
bf7c9c87 49 'Controller::Action::Action' => {
50 action_args => {
51 action_action_nine => { another_extra_arg => 13 }
52 }
55046410 53 },
1bef5f59 54 encoding => 'UTF-8',
6e8520be 55 abort_chain_on_error_fix => 1,
bf7c9c87 56);
dd4e6fd2 57
85a351e5 58# Test bug found when re-adjusting the metaclass compat code in Moose
59# in 292360. Test added to Moose in 4b760d6, but leave this attribute
60# above ->setup so we have some generated methods to be double sure.
61has an_attribute_before_we_change_base_classes => ( is => 'ro');
62
fa2d8e38 63if ($::setup_leakchecker) {
64 require Scalar::Util;
65 require Devel::Cycle;
d9d8aa51 66
67 has leaks => (
68 is => 'ro',
69 default => sub { [] },
70 );
d9d8aa51 71
fa2d8e38 72 sub count_leaks {
73 my ($ctx) = @_;
74 return scalar @{ $ctx->leaks };
75 }
76
77 after finalize => sub {
78 my ($ctx) = @_;
79 my @leaks;
80
81 my $weak_ctx = $ctx;
82 Scalar::Util::weaken $weak_ctx;
83
84 Devel::Cycle::find_cycle($ctx, sub {
85 my ($path) = @_;
86 push @leaks, $path
87 if $path->[0]->[2] == $weak_ctx;
88 });
d9d8aa51 89
fa2d8e38 90 push @{ $ctx->leaks }, @leaks;
91 };
d9d8aa51 92}
93
dd4e6fd2 94TestApp->setup;
95
dd4e6fd2 96sub execute {
4d989a5d 97 my $c = shift;
98 my $class = ref( $c->component( $_[0] ) ) || $_[0];
f3414019 99 my $action = $_[1]->reverse;
dd4e6fd2 100
101 my $method;
102
4d989a5d 103 if ( $action =~ /->(\w+)$/ ) {
104 $method = $1;
dd4e6fd2 105 }
4d989a5d 106 elsif ( $action =~ /\/(\w+)$/ ) {
107 $method = $1;
dd4e6fd2 108 }
01ba879f 109 elsif ( $action =~ /^(\w+)$/ ) {
110 $method = $action;
111 }
112
ba599d1c 113 if ( $class && $method && $method !~ /^_/ ) {
1408d0a4 114 my $executed = sprintf( "%s->%s", $class, $method );
fbcc39ad 115 my @executed = $c->response->headers->header('X-Catalyst-Executed');
116 push @executed, $executed;
117 $c->response->headers->header(
118 'X-Catalyst-Executed' => join ', ',
119 @executed
120 );
1408d0a4 121 }
81f25ce6 122 no warnings 'recursion';
dd4e6fd2 123 return $c->SUPER::execute(@_);
124}
125
8153c836 126# Replace the very large HTML error page with
127# useful info if something crashes during a test
128sub finalize_error {
129 my $c = shift;
88e5a8b0 130
dbb2d5cd 131 $c->next::method(@_);
88e5a8b0 132
8153c836 133 $c->res->status(500);
134 $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
135}
136
369c09bc 137{
138 no warnings 'redefine';
139 sub Catalyst::Log::error { }
140}
4ca147fa 141
258733f1 142# Pretend to be Plugin::Session and hook finalize_headers to send a header
143
144sub finalize_headers {
145 my $c = shift;
146
147 $c->res->header('X-Test-Header', 'valid');
148
89ba65d5 149 my $call_count = $c->stash->{finalize_headers_call_count} || 0;
150 $call_count++;
151 $c->stash(finalize_headers_call_count => $call_count);
152 $c->res->header('X-Test-Header-Call-Count' => $call_count);
153
258733f1 154 return $c->maybe::next::method(@_);
155}
156
88e5a8b0 157# Make sure we can load Inline plugins.
4ca147fa 158
159package Catalyst::Plugin::Test::Inline;
bd7ed1de 160use Moose;
4ca147fa 161
f3414019 1621;