Add test that env does not get trampled
[catagits/Catalyst-Runtime.git] / t / middleware-stash.t
CommitLineData
4b0b7489 1use warnings;
2use strict;
3
4{
5
878f6271 6 package MyMiddleware;
7 $INC{'MyMiddleware'} = __FILE__;
8
9 our $INNER_VAR_EXPOSED;
10
11 use base 'Plack::Middleware';
12
13 sub call {
14 my ($self, $env) = @_;
15
16 my $res = $self->app->($env);
17
18 return $self->response_cb($res, sub{
19 my $inner = shift;
20
21 $INNER_VAR_EXPOSED = $env->{inner_var_from_catalyst};
22
23 return;
24 });
25
26 }
27
4b0b7489 28 package MyAppChild::Controller::User;
29 $INC{'MyAppChild/Controller/User.pm'} = __FILE__;
30
31 use base 'Catalyst::Controller';
32 use Test::More;
33
34 sub stash :Local {
35 my ($self, $c) = @_;
36 $c->stash->{inner} = "inner";
37 $c->res->body( "inner: ${\$c->stash->{inner}}, outer: ${\$c->stash->{outer}}");
38
878f6271 39 $c->req->env->{inner_var_from_catalyst} = 'station';
40
efa13fa4 41 is_deeply [sort {$a cmp $b} keys(%{$c->stash})], ['inner','outer'], 'both keys in stash';
4b0b7489 42 }
43
44 package MyAppChild;
45 $INC{'MyAppChild.pm'} = __FILE__;
46
47 use Catalyst;
48 MyAppChild->setup;
49
50 package MyAppParent::Controller::User;
51 $INC{'MyAppParent/Controller/User.pm'} = __FILE__;
52
53 use base 'Catalyst::Controller';
54 use Test::More;
55
56 sub stash :Local {
57 my ($self, $c) = @_;
58 $c->stash->{outer} = "outer";
59 $c->res->from_psgi_response( MyAppChild->to_app->($c->req->env) );
60
efa13fa4 61 is_deeply [keys(%{$c->stash})], ['outer'], 'only one key in stash';
4b0b7489 62 }
63
64 package MyAppParent;
65 use Catalyst;
66 MyAppParent->setup;
67
68}
69
70use Test::More;
71use Catalyst::Test 'MyAppParent';
72
73my $res = request '/user/stash';
74is $res->content, 'inner: inner, outer: outer', 'got expected response';
878f6271 75is $MyMiddleware::INNER_VAR_EXPOSED, 'station', 'env does not get trampled';
4b0b7489 76
77done_testing;