| 1 | package Poet::t::PSGIHandler; | ||||||||||||
| 2 | 1 | $Poet::t::PSGIHandler::VERSION = '0.16'; | |||||||||||
| 3 | 1 | ▶ | use Test::Class::Most parent => 'Poet::Test::Class'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Capture::Tiny qw(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Guard; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Poet::Tools qw(dirname mkpath trim write_file); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | my $poet = __PACKAGE__->initialize_temp_env( | |||||||||||
| 9 | conf => { | ||||||||||||
| 10 | layer => 'production', | ||||||||||||
| 11 | 'foo.bar' => 5, | ||||||||||||
| 12 | 'server.load_modules' => ['TestApp::Foo'] | ||||||||||||
| 13 | } | ||||||||||||
| 14 | ); | ||||||||||||
| 15 | 1 | unlink( glob( $poet->comps_path("*.mc") ) ); | |||||||||||
| 16 | 1 | write_file( $poet->lib_path("TestApp/Foo.pm"), "package TestApp::Foo;\nsub bar {}\n1;\n" ); | |||||||||||
| 17 | |||||||||||||
| 18 | 13 | ▶ | sub mech { | ||||||||||
|
Subroutine
mech: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 19 | 13 | my $self = shift; | |||||||||||
| 20 | 13 | my $mech = $self->SUPER::mech( env => $poet ); | |||||||||||
| 21 | 13 | @{ $mech->requests_redirectable } = (); | |||||||||||
| 22 | 13 | return $mech; | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 14 | ▶ | sub add_comp { | ||||||||||
|
Subroutine
add_comp: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 26 | 14 | my ( $self, %params ) = @_; | |||||||||||
| 27 | 14 | ▶ | my $path = $params{path} or die "must pass path"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 14 | ▶ | my $src = $params{src} or die "must pass src"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 14 | my $file = $poet->comps_dir . $path; | |||||||||||
| 30 | 14 | mkpath( dirname($file), 0, 0775 ); | |||||||||||
| 31 | 14 | write_file( $file, $src ); | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 12 | ▶ | sub try_psgi_comp { | ||||||||||
|
Subroutine
try_psgi_comp: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||
| 35 | 12 | my ( $self, %params ) = @_; | |||||||||||
| 36 | 12 | ▶ | my $path = $params{path} or die "must pass path"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 12 | ( my $uri = $path ) =~ s/\.mc$//; | |||||||||||
| 38 | 12 | ▶ | my $qs = $params{qs} || ''; | ||||||||||
|
Condition100%2/2
$params{'qs'} || ''
MC/DC100%1/1
$params{'qs'} || ''
$params{'qs'}
Truth table100%2/2
A
A= $params{'qs'}
| |||||||||||||
| 39 | 12 | ▶ | my $expect_code = defined( $params{expect_code} ) ? $params{expect_code} : 200; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | |||||||||||||
| 41 | 12 | $self->add_comp(%params); | |||||||||||
| 42 | |||||||||||||
| 43 | 12 | my $mech = $self->mech(); | |||||||||||
| 44 | { | ||||||||||||
| 45 | |||||||||||||
| 46 | # Silence 'PSGI error' diagnostics if we're expecting error | ||||||||||||
| 47 | 12 | ▶ | Test::More->builder->no_diag(1) if $expect_code == 500; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 12 | ▶ | scope_guard { Test::More->builder->no_diag(0) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 12 | $mech->get( $uri . $qs ); | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 12 | ▶ | if ( my $expect_content = $params{expect_content} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | |||||||||||||
| 54 | 12 | ▶ | if ( ref($expect_content) eq 'Regexp' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 2 | $mech->content_like( $expect_content, "$path - content" ); | |||||||||||
| 56 | } | ||||||||||||
| 57 | else { | ||||||||||||
| 58 | 10 | is( trim( $mech->content ), trim($expect_content), "$path - content" ); | |||||||||||
| 59 | } | ||||||||||||
| 60 | 12 | is( $mech->status, $expect_code, "$path - code" ); | |||||||||||
| 61 | 12 | ▶ | if ( my $expect_headers = $params{expect_headers} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 4 | ▶ | while ( my ( $hdr, $value ) = each(%$expect_headers) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 4 | cmp_deeply( $mech->res->header($hdr), $value, "$path - header $hdr" ); | |||||||||||
| 64 | } | ||||||||||||
| 65 | } | ||||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 1 | ▶ | sub test_get_pl : Tests { | ||||||||||
|
Subroutine
test_get_pl: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||
| 71 | 1 | $self->add_comp( | |||||||||||
| 72 | path => '/getpl.mc', | ||||||||||||
| 73 | src => 'path = <% $m->req->path %>' | ||||||||||||
| 74 | ); | ||||||||||||
| 75 | 1 | my $cmd = sprintf( "%s /getpl", $poet->bin_path("get.pl") ); | |||||||||||
| 76 | 1 | ▶ | my $output = Capture::Tiny::capture_merged { system($cmd) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 77 | 1 | is( $output, 'path = /getpl', "get.pl output" ); | |||||||||||
| 78 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | |||||||||||||
| 80 | 1 | ▶ | sub test_basic : Tests { | ||||||||||
|
Subroutine
test_basic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 81 | 1 | my $self = shift; | |||||||||||
| 82 | 1 | $self->try_psgi_comp( | |||||||||||
| 83 | path => '/basic.mc', | ||||||||||||
| 84 | src => 'path = <% $m->req->path %>', | ||||||||||||
| 85 | expect_content => 'path = /basic', | ||||||||||||
| 86 | ); | ||||||||||||
| 87 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 88 | |||||||||||||
| 89 | 1 | ▶ | sub test_error : Tests { | ||||||||||
|
Subroutine
test_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 90 | 1 | my $self = shift; | |||||||||||
| 91 | 1 | $self->try_psgi_comp( | |||||||||||
| 92 | path => '/error.mc', | ||||||||||||
| 93 | src => '% die "bleah";', | ||||||||||||
| 94 | expect_code => 500, | ||||||||||||
| 95 | expect_content => qr/bleah/, | ||||||||||||
| 96 | ); | ||||||||||||
| 97 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 98 | |||||||||||||
| 99 | 1 | ▶ | sub test_not_found : Tests { | ||||||||||
|
Subroutine
test_not_found: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 100 | 1 | my $self = shift; | |||||||||||
| 101 | 1 | my $mech = $self->mech(); | |||||||||||
| 102 | 1 | $mech->get("/does/not/exist"); | |||||||||||
| 103 | 1 | is( $mech->status, 404, "status 404" ); | |||||||||||
| 104 | 1 | like( $mech->content, qr/Not found/, "default not found page" ); | |||||||||||
| 105 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 106 | |||||||||||||
| 107 | 1 | ▶ | sub test_args : Tests { | ||||||||||
|
Subroutine
test_args: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 108 | 1 | my $self = shift; | |||||||||||
| 109 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 141 | |||||||||||||
| 142 | 1 | ▶ | sub test_abort : Tests { | ||||||||||
|
Subroutine
test_abort: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 143 | 1 | my $self = shift; | |||||||||||
| 144 | 1 | $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 | 1 | $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 | 1 | $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 | 1 | $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 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 197 | |||||||||||||
| 198 | 1 | ▶ | sub test_import : Tests { | ||||||||||
|
Subroutine
test_import: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 199 | 1 | my $self = shift; | |||||||||||
| 200 | 1 | my $root_dir = $poet->root_dir; | |||||||||||
| 201 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 221 | |||||||||||||
| 222 | 1 | ▶ | sub test_visit : Tests { | ||||||||||
|
Subroutine
test_visit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 223 | 1 | my $self = shift; | |||||||||||
| 224 | |||||||||||||
| 225 | 1 | Mason::Request->_reset_next_id(); | |||||||||||
| 226 | 1 | $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 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 256 | |||||||||||||
| 257 | 1 | ▶ | sub test_cache : Tests { | ||||||||||
|
Subroutine
test_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 258 | 1 | my $self = shift; | |||||||||||
| 259 | |||||||||||||
| 260 | 1 | my $expected_root_dir = $poet->root_dir . "/data/cache"; | |||||||||||
| 261 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 273 | |||||||||||||
| 274 | 1 | ▶ | sub test_misc : Tests { | ||||||||||
|
Subroutine
test_misc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 275 | 1 | my $self = shift; | |||||||||||
| 276 | 1 | $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 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 282 | |||||||||||||
| 283 | 1 | 1; | |||||||||||