| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Plack::Test; |
| 6 |
use Plack::Builder; |
| 7 |
use HTTP::Request::Common; |
| 8 |
|
| 9 |
# Multi-app dispatch only happens through the runner's psgi_app -- to_app is a |
| 10 |
# single application's PSGI sub and knows nothing about its neighbours. Every |
| 11 |
# test below therefore builds its app list with Dancer2->psgi_app([...]), which |
| 12 |
# also pins the dispatch *order* to the order given rather than to whatever |
| 13 |
# order the packages happened to be compiled in. |
| 14 |
# |
| 15 |
# 'logger => null' keeps the suite quiet; these tests assert on responses. |
| 16 |
|
| 17 |
{ |
| 18 |
package MultiFirst; |
| 19 |
use Dancer2; |
| 20 |
set logger => 'null'; |
| 21 |
set session => 'Simple'; |
| 22 |
|
| 23 |
get '/first-only' => sub { 'FIRST' }; |
| 24 |
|
| 25 |
# Both applications declare these two paths. The first app in the list |
| 26 |
# should answer them, and the second app's version should never run. |
| 27 |
get '/shared/status' => sub { status 404; 'FIRST-SET-404' }; |
| 28 |
get '/shared/error' => sub { send_error 'first says gone' => 404 }; |
| 29 |
get '/shared/ok' => sub { 'FIRST-OK' }; |
| 30 |
|
| 31 |
get '/to-second' => sub { |
| 32 |
session who => 'set-in-first'; |
| 33 |
forward '/second-only'; |
| 34 |
}; |
| 35 |
|
| 36 |
get '/landing' => sub { |
| 37 |
'LANDED-IN-FIRST who=' . ( session('who') // 'undef' ); |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
package MultiSecond; |
| 43 |
use Dancer2; |
| 44 |
set logger => 'null'; |
| 45 |
set session => 'Simple'; |
| 46 |
|
| 47 |
get '/second-only' => sub { |
| 48 |
'SECOND who=' . ( session('who') // 'undef' ); |
| 49 |
}; |
| 50 |
|
| 51 |
get '/shared/status' => sub { 'SECOND-SHOULD-NOT-RUN' }; |
| 52 |
get '/shared/error' => sub { 'SECOND-SHOULD-NOT-RUN' }; |
| 53 |
get '/shared/ok' => sub { 'SECOND-OK' }; |
| 54 |
|
| 55 |
get '/to-first' => sub { |
| 56 |
session who => 'set-in-second'; |
| 57 |
forward '/landing'; |
| 58 |
}; |
| 59 |
} |
| 60 |
|
| 61 |
{ |
| 62 |
package MultiUri; |
| 63 |
use Dancer2; |
| 64 |
set logger => 'null'; |
| 65 |
|
| 66 |
get 'item' => '/item/:id' => sub { 'item' }; |
| 67 |
get 'two_parts' => '/a/:one/b/:two' => sub { 'two parts' }; |
| 68 |
get 'by_regex' => qr{^/re/(\d+)$} => sub { 'regex' }; |
| 69 |
|
| 70 |
# uri_for_route dies on bad input, and a die inside a route would be |
| 71 |
# swallowed into a 500 page. Each generating route therefore reports |
| 72 |
# whether it lived and with what, so the test can assert on the outcome |
| 73 |
# rather than on the shape of an error page. |
| 74 |
sub _try { |
| 75 |
my $code = shift; |
| 76 |
my $result = eval { $code->() }; |
| 77 |
return defined $result ? "LIVED $result" : "DIED $@"; |
| 78 |
} |
| 79 |
|
| 80 |
get '/uri/ok' => sub { _try( sub { uri_for_route( 'item', { id => 7 } ) } ) }; |
| 81 |
get '/uri/zero' => sub { _try( sub { uri_for_route( 'item', { id => 0 } ) } ) }; |
| 82 |
get '/uri/empty' => sub { _try( sub { uri_for_route( 'item', { id => '' } ) } ) }; |
| 83 |
get '/uri/missing' => sub { _try( sub { uri_for_route('item') } ) }; |
| 84 |
get '/uri/partial' => sub { _try( sub { uri_for_route( 'two_parts', { one => 'x' } ) } ) }; |
| 85 |
get '/uri/regex' => sub { _try( sub { uri_for_route( 'by_regex', { x => 1 } ) } ) }; |
| 86 |
get '/uri/plain' => sub { _try( sub { uri_for('/item/7') } ) }; |
| 87 |
} |
| 88 |
|
| 89 |
sub multi_app { Plack::Test->create( Dancer2->psgi_app( [@_] ) ) } |
| 90 |
|
| 91 |
subtest 'a request the first app cannot match is offered to the next one' => sub { |
| 92 |
my $test = multi_app(qw< MultiFirst MultiSecond >); |
| 93 |
|
| 94 |
is( $test->request( GET '/first-only' )->content, 'FIRST', |
| 95 |
'the first app answers a path only it declares' ); |
| 96 |
|
| 97 |
my $res = $test->request( GET '/second-only' ); |
| 98 |
is( $res->code, 200, 'a path only the second app declares is still served' ); |
| 99 |
is( $res->content, 'SECOND who=undef', |
| 100 |
'the second app answered it, not a 404 from the first' ); |
| 101 |
}; |
| 102 |
|
| 103 |
subtest 'a path no app declares gets one 404 from the first app' => sub { |
| 104 |
my $test = multi_app(qw< MultiFirst MultiSecond >); |
| 105 |
|
| 106 |
my $res = $test->request( GET '/declared/nowhere' ); |
| 107 |
is( $res->code, 404, 'exhausting every app produces a 404' ); |
| 108 |
like( $res->header('Content-Type'), qr{^text/html}, |
| 109 |
'it is the rendered error page, not a bare PSGI triplet' ); |
| 110 |
like( $res->content, qr{/declared/nowhere}, |
| 111 |
'the error page names the path that was not found' ); |
| 112 |
}; |
| 113 |
|
| 114 |
subtest 'a deliberate 404 from the first app is not treated as "no match"' => sub { |
| 115 |
my $test = multi_app(qw< MultiFirst MultiSecond >); |
| 116 |
|
| 117 |
# The distinction this subtest exists for: "no route matched" makes the |
| 118 |
# dispatcher try the next app, but a route that matched and *chose* 404 |
| 119 |
# must end the request. Both apps declare these paths, so if the |
| 120 |
# distinction collapses the second app's 200 leaks out in place of the 404. |
| 121 |
my $status = $test->request( GET '/shared/status' ); |
| 122 |
is( $status->code, 404, 'the status the matched route set is kept' ); |
| 123 |
is( $status->content, 'FIRST-SET-404', |
| 124 |
'the first app\'s body is returned' ); |
| 125 |
unlike( $status->content, qr/SECOND/, |
| 126 |
'the second app was never consulted' ); |
| 127 |
|
| 128 |
my $error = $test->request( GET '/shared/error' ); |
| 129 |
is( $error->code, 404, 'send_error in a matched route also ends the request' ); |
| 130 |
unlike( $error->content, qr/SECOND/, |
| 131 |
'the second app was not consulted for send_error either' ); |
| 132 |
|
| 133 |
is( $test->request( GET '/shared/ok' )->content, 'FIRST-OK', |
| 134 |
'and an ordinary shared path is answered by the first app' ); |
| 135 |
}; |
| 136 |
|
| 137 |
subtest 'apps are consulted in the order they were listed' => sub { |
| 138 |
# Same two apps, opposite order. This is what makes the subtest above a |
| 139 |
# statement about matching rather than about which package loaded first. |
| 140 |
my $test = multi_app(qw< MultiSecond MultiFirst >); |
| 141 |
|
| 142 |
is( $test->request( GET '/shared/ok' )->content, 'SECOND-OK', |
| 143 |
'the app listed first answers a path both declare' ); |
| 144 |
is( $test->request( GET '/first-only' )->content, 'FIRST', |
| 145 |
'the app listed second still gets what only it declares' ); |
| 146 |
}; |
| 147 |
|
| 148 |
subtest 'a forward across apps keeps the session' => sub { |
| 149 |
my $test = multi_app(qw< MultiFirst MultiSecond >); |
| 150 |
|
| 151 |
is( |
| 152 |
$test->request( GET '/to-second' )->content, |
| 153 |
'SECOND who=set-in-first', |
| 154 |
'a session created in the first app is visible after forwarding to the second', |
| 155 |
); |
| 156 |
|
| 157 |
# The reverse direction re-enters an app earlier in the list, which is a |
| 158 |
# different path through the dispatch loop. |
| 159 |
is( |
| 160 |
$test->request( GET '/to-first' )->content, |
| 161 |
'LANDED-IN-FIRST who=set-in-second', |
| 162 |
'and a session created in the second app survives a forward back to the first', |
| 163 |
); |
| 164 |
}; |
| 165 |
|
| 166 |
subtest 'uri_for_route refuses rather than emitting an unsubstituted token' => sub { |
| 167 |
my $test = multi_app('MultiUri'); |
| 168 |
|
| 169 |
is( $test->request( GET '/uri/ok' )->content, |
| 170 |
'LIVED http://localhost/item/7', |
| 171 |
'a supplied route parameter is substituted' ); |
| 172 |
|
| 173 |
for my $case ( |
| 174 |
[ '/uri/missing', qr/uses the parameter 'id', which was not provided/, |
| 175 |
'a route parameter with no value at all is refused' ], |
| 176 |
[ '/uri/partial', qr/uses the parameter 'two', which was not provided/, |
| 177 |
'supplying only some of the parameters is refused too' ], |
| 178 |
) { |
| 179 |
my ( $path, $expected, $name ) = @$case; |
| 180 |
my $content = $test->request( GET $path )->content; |
| 181 |
like( $content, qr/^DIED/, "$name (it dies)" ); |
| 182 |
like( $content, $expected, $name ); |
| 183 |
unlike( $content, qr/:(?:id|two)/, |
| 184 |
'no URL containing an unsubstituted :token is produced' ); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
subtest 'uri_for_route accepts a route parameter of 0, but not undef or ""' => sub { |
| 189 |
my $test = multi_app('MultiUri'); |
| 190 |
|
| 191 |
# Fixed behavior: the parameter is tested for definedness rather than |
| 192 |
# for truth, so 0 -- an ordinary database ID, list index or page number |
| 193 |
# -- is substituted like any other value. Previously the check was for |
| 194 |
# truth, so a 0 was refused as though it had not been given at all. |
| 195 |
is( $test->request( GET '/uri/zero' )->content, |
| 196 |
'LIVED http://localhost/item/0', |
| 197 |
'a route parameter of 0 is substituted, not rejected' ); |
| 198 |
|
| 199 |
# The empty string is a different case from 0, and is still refused: |
| 200 |
# ':id' compiles to ([^/]+), which matches at least one character, so |
| 201 |
# substituting '' would yield '/item/' -- a URI that cannot match the |
| 202 |
# route it was generated from. Refusing beats handing back a URL that |
| 203 |
# 404s against its own application. |
| 204 |
my $empty = $test->request( GET '/uri/empty' )->content; |
| 205 |
like( $empty, qr/^DIED/, 'an empty route parameter is refused' ); |
| 206 |
like( $empty, qr/was given an empty value for the parameter 'id'/, |
| 207 |
'and says the value was empty rather than missing' ); |
| 208 |
unlike( $empty, qr/which was not provided/, |
| 209 |
'not reusing the "not provided" wording for a value that was provided' ); |
| 210 |
|
| 211 |
# A parameter that was genuinely never supplied must still die, with the |
| 212 |
# same message as before, and must be distinguishable from the empty |
| 213 |
# case above -- that distinction is the point of the two messages. |
| 214 |
my $content = $test->request( GET '/uri/missing' )->content; |
| 215 |
like( $content, qr/^DIED/, 'a route parameter with no value at all is still refused' ); |
| 216 |
like( $content, qr/uses the parameter 'id', which was not provided/, |
| 217 |
'with the existing message' ); |
| 218 |
}; |
| 219 |
|
| 220 |
subtest 'uri_for_route refuses a regex route instead of guessing' => sub { |
| 221 |
my $test = multi_app('MultiUri'); |
| 222 |
|
| 223 |
my $content = $test->request( GET '/uri/regex' )->content; |
| 224 |
like( $content, qr/^DIED/, 'a named regex route cannot be turned into a URL' ); |
| 225 |
like( $content, qr/does not support regexp route paths/, |
| 226 |
'and says so, rather than producing a URL built from the pattern' ); |
| 227 |
}; |
| 228 |
|
| 229 |
subtest 'a mounted app keeps its mount path in generated URLs' => sub { |
| 230 |
my $psgi = Dancer2->psgi_app( [qw< MultiFirst MultiSecond MultiUri >] ); |
| 231 |
my $test = Plack::Test->create( builder { mount '/sub' => $psgi } ); |
| 232 |
my $prefix = 'http://localhost/sub'; |
| 233 |
|
| 234 |
is( $test->request( GET "$prefix/second-only" )->code, 200, |
| 235 |
'dispatch still reaches every app under a mount path' ); |
| 236 |
|
| 237 |
is( $test->request( GET "$prefix/uri/plain" )->content, |
| 238 |
"LIVED $prefix/item/7", |
| 239 |
'uri_for includes the mount path' ); |
| 240 |
|
| 241 |
is( $test->request( GET "$prefix/uri/ok" )->content, |
| 242 |
"LIVED $prefix/item/7", |
| 243 |
'uri_for_route includes the mount path' ); |
| 244 |
}; |
| 245 |
|
| 246 |
done_testing(); |