File Coverage

blib/lib/Poet/t/PSGIHandler.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Poet::t::PSGIHandler;
2             $Poet::t::PSGIHandler::VERSION = '0.16';
3 1     1   881 use Test::Class::Most parent => 'Poet::Test::Class';
  1         29238  
  1         7  
4             use Capture::Tiny qw();
5             use Guard;
6             use Poet::Tools qw(dirname mkpath trim write_file);
7              
8             my $poet = __PACKAGE__->initialize_temp_env(
9             conf => {
10             layer => 'production',
11             'foo.bar' => 5,
12             'server.load_modules' => ['TestApp::Foo']
13             }
14             );
15             unlink( glob( $poet->comps_path("*.mc") ) );
16             write_file( $poet->lib_path("TestApp/Foo.pm"), "package TestApp::Foo;\nsub bar {}\n1;\n" );
17              
18             sub mech {
19             my $self = shift;
20             my $mech = $self->SUPER::mech( env => $poet );
21             @{ $mech->requests_redirectable } = ();
22             return $mech;
23             }
24              
25             sub add_comp {
26             my ( $self, %params ) = @_;
27             my $path = $params{path} or die "must pass path";
28             my $src = $params{src} or die "must pass src";
29             my $file = $poet->comps_dir . $path;
30             mkpath( dirname($file), 0, 0775 );
31             write_file( $file, $src );
32             }
33              
34             sub try_psgi_comp {
35             my ( $self, %params ) = @_;
36             my $path = $params{path} or die "must pass path";
37             ( my $uri = $path ) =~ s/\.mc$//;
38             my $qs = $params{qs} || '';
39             my $expect_code = defined( $params{expect_code} ) ? $params{expect_code} : 200;
40              
41             $self->add_comp(%params);
42              
43             my $mech = $self->mech();
44             {
45              
46             # Silence 'PSGI error' diagnostics if we're expecting error
47             Test::More->builder->no_diag(1) if $expect_code == 500;
48             scope_guard { Test::More->builder->no_diag(0) };
49             $mech->get( $uri . $qs );
50             }
51              
52             if ( my $expect_content = $params{expect_content} ) {
53              
54             if ( ref($expect_content) eq 'Regexp' ) {
55             $mech->content_like( $expect_content, "$path - content" );
56             }
57             else {
58             is( trim( $mech->content ), trim($expect_content), "$path - content" );
59             }
60             is( $mech->status, $expect_code, "$path - code" );
61             if ( my $expect_headers = $params{expect_headers} ) {
62             while ( my ( $hdr, $value ) = each(%$expect_headers) ) {
63             cmp_deeply( $mech->res->header($hdr), $value, "$path - header $hdr" );
64             }
65             }
66             }
67             }
68              
69             sub test_get_pl : Tests {
70             my $self = shift;
71             $self->add_comp(
72             path => '/getpl.mc',
73             src => 'path = <% $m->req->path %>'
74             );
75             my $cmd = sprintf( "%s /getpl", $poet->bin_path("get.pl") );
76             my $output = Capture::Tiny::capture_merged { system($cmd) };
77             is( $output, 'path = /getpl', "get.pl output" );
78             }
79              
80             sub test_basic : Tests {
81             my $self = shift;
82             $self->try_psgi_comp(
83             path => '/basic.mc',
84             src => 'path = <% $m->req->path %>',
85             expect_content => 'path = /basic',
86             );
87             }
88              
89             sub test_error : Tests {
90             my $self = shift;
91             $self->try_psgi_comp(
92             path => '/error.mc',
93             src => '% die "bleah";',
94             expect_code => 500,
95             expect_content => qr/bleah/,
96             );
97             }
98              
99             sub test_not_found : Tests {
100             my $self = shift;
101             my $mech = $self->mech();
102             $mech->get("/does/not/exist");
103             is( $mech->status, 404, "status 404" );
104             like( $mech->content, qr/Not found/, "default not found page" );
105             }
106              
107             sub test_args : Tests {
108             my $self = shift;
109             $self->try_psgi_comp(
110             path => '/args.mc',
111             qs => '?a=1&a=2&b=3&b=4&c=5&c=6&d=7&d=8',
112             src => '
113             <%args>
114             $.a
115             $.b => (isa => "Int")
116             $.c => (isa => "ArrayRef");
117             $.d => (isa => "ArrayRef[Int]", default => sub { [10] });
118             $.e => (isa => "ArrayRef[Int]", default => sub { [10] });
119             </%args>
120              
121             a = <% $.a %>
122             b = <% $.b %>
123             c = <% join(",", @{$.c}) %>
124             d = <% join(",", @{$.d}) %>
125             e = <% join(",", @{$.e}) %>
126              
127             % my $args = $.args;
128             <% Mason::Util::dump_one_line($args) %>
129             ',
130             expect_content => <<EOF,
131             a = 2
132             b = 4
133             c = 5,6
134             d = 7,8
135             e = 10
136              
137             {a => '2',b => '4',c => ['5','6'],d => ['7','8']}
138             EOF
139             );
140             }
141              
142             sub test_abort : Tests {
143             my $self = shift;
144             $self->try_psgi_comp(
145             path => '/redirect.mc',
146             src => '
147             will not be printed
148             % $m->redirect("http://www.google.com/");
149             will also not be printed
150             ',
151             expect_content => ' ',
152             expect_code => 302,
153             expect_headers => { Location => 'http://www.google.com/' },
154             );
155             $self->try_psgi_comp(
156             path => '/go_to_redirect.mc',
157             src => '
158             <%init>
159             $m->go("/redirect");
160             </%init>
161             ',
162             expect_content => ' ',
163             expect_code => 302,
164             expect_headers => { Location => 'http://www.google.com/' },
165             );
166             $self->try_psgi_comp(
167             path => '/visit_redirect.mc',
168             src => '
169             <%init>
170             $m->visit("/redirect");
171             </%init>
172             ',
173             expect_content => ' ',
174             expect_code => 302,
175             expect_headers => { Location => 'http://www.google.com/' },
176             );
177             $self->try_psgi_comp(
178             path => '/redirect_301.mc',
179             src => '
180             will not be printed
181             % $m->redirect("http://www.yahoo.com/", 301);
182             ',
183             expect_content => ' ',
184             expect_code => 301,
185             expect_headers => { Location => 'http://www.yahoo.com/' },
186             );
187             $self->try_psgi_comp(
188             path => '/not_found.mc',
189             src => '
190             will not be printed
191             % $m->clear_and_abort(404);
192             ',
193             expect_content => qr/Not found/,
194             expect_code => 404,
195             );
196             }
197              
198             sub test_import : Tests {
199             my $self = shift;
200             my $root_dir = $poet->root_dir;
201             $self->try_psgi_comp(
202             path => '/import.mc',
203             src => '
204             foo.bar = <% $conf->get("foo.bar") %>
205             root_dir = <% $poet->root_dir %>
206             <% dh_live({baz => "blargh"}) %>
207             ',
208             expect_content =>
209             sprintf( "
210             foo.bar = 5
211             root_dir = %s
212             <pre>
213             \[dh_live at %s/comps/import.mc line 4.] [$$] {
214             baz => 'blargh'
215             }
216              
217             </pre>
218             ", $root_dir, $root_dir )
219             );
220             }
221              
222             sub test_visit : Tests {
223             my $self = shift;
224              
225             Mason::Request->_reset_next_id();
226             $self->add_comp(
227             path => '/subreq/other.mc',
228             src => '
229             id=<% $m->id %>
230             <% $m->page->cmeta->path %>
231             <% $m->request_path %>
232             <% Mason::Util::dump_one_line($m->request_args) %>
233             ',
234             );
235             $self->try_psgi_comp(
236             path => '/subreq/visit.mc',
237             src => '
238             begin
239             id=<% $m->id %>
240             <%perl>$m->visit("/subreq/other", foo => 5);</%perl>
241             id=<% $m->id %>
242             end
243             ',
244             expect_content => "
245             begin
246             id=0
247             id=1
248             /subreq/other.mc
249             /subreq/other
250             \{foo => 5}
251             id=0
252             end
253             "
254             );
255             }
256              
257             sub test_cache : Tests {
258             my $self = shift;
259              
260             my $expected_root_dir = $poet->root_dir . "/data/cache";
261             $self->try_psgi_comp(
262             path => '/cache.mc',
263             src => '
264             chi_root_class: <% $m->cache->chi_root_class %>
265             root_dir: <% $m->cache->root_dir %>
266             ',
267             expect_content => "
268             chi_root_class: Poet::Cache
269             root_dir: $expected_root_dir
270             ",
271             );
272             }
273              
274             sub test_misc : Tests {
275             my $self = shift;
276             $self->try_psgi_comp(
277             path => '/misc.mc',
278             src => 'TestApp::Foo = <% TestApp::Foo->can("bar") ? "loaded" : "not loaded" %>',
279             expect_content => 'TestApp::Foo = loaded',
280             );
281             }
282              
283             1;