2 use warnings FATAL => 'all';
3 no warnings::illegalproto;
7 use HTTP::Request::Common qw(GET POST);
10 use Web::Dispatch::Predicates 'match_true';
15 use Web::Simple 'MiscTest';
18 sub dispatch_request { @dispatch }
19 sub string_method { [ 999, [], [""] ]; }
22 die "Passed undef to can, this blows up on 5.8" unless defined($_[1]);
27 my $app = MiscTest->new;
28 sub run_request { $app->run_test_request( @_ ); }
36 invalid_psgi_responses();
37 middleware_as_only_route();
38 route_returns_middleware_plus_extra();
39 route_returns_undef();
40 matcher_nonsub_pair();
41 matcher_undef_method();
45 sub string_method_name {
46 @dispatch = ( '/' => "string_method" );
48 my $get = run_request( GET => 'http://localhost/' );
50 cmp_ok $get->code, '==', 999, "a dispatcher that's a string matching a method on the dispatch object gets executed";
53 sub app_is_non_plack {
55 my $r = HTTP::Response->new( 999 );
57 my $d = Web::Dispatch->new( dispatch_app => $r );
60 like $@, qr/No idea how we got here with HTTP::Response/,
61 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
70 sub to_app { [ 999, [], ["ok"] ] }
73 my $o = ObjectApp->new;
74 my $d = Web::Dispatch->new( dispatch_object => $o );
77 cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
81 my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
82 my $res = $d->call( {} );
84 cmp_ok $res->[0], '==', 999,
85 "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
88 sub plack_app_return {
92 sub new { bless {}, $_[0] }
107 my $get = run_request( GET => 'http://localhost/' );
109 cmp_ok $get->code, '==', 999,
110 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
113 sub broken_route_def {
115 @dispatch = ( '/' => "" );
117 my $get = run_request( GET => 'http://localhost/' );
119 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
120 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
123 sub invalid_psgi_responses {
127 [ [ sub { } ], "an arrayref with a single sub in it" ],
128 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
129 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
132 for my $response ( @responses ) {
133 @dispatch = ( sub (/) { $response->[0] } );
135 my $message = sprintf(
136 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
140 # Somewhere between 1.0028 and 1.0031 Plack changed so that the
141 # FauxObject case became a 500 rather than a die; in case it later does
142 # the same thing for other stuff, just accept either sort of error
144 my $res = eval { run_request( GET => 'http://localhost/' ) };
147 ok $res->is_error, $message;
149 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/, $message;
155 sub middleware_as_only_route {
156 @dispatch = ( bless {}, "Plack::Middleware" );
158 my $get = run_request( GET => 'http://localhost/' );
160 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
161 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
162 "the error message mentions the middleware class";
165 sub route_returns_middleware_plus_extra {
168 return ( bless( {}, "Plack::Middleware" ), "" );
172 my $get = run_request( GET => 'http://localhost/' );
174 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
176 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
177 "the error message mentions the middleware class";
180 sub route_returns_undef {
197 my $get = run_request( GET => 'http://localhost/' );
199 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
202 sub matcher_nonsub_pair {
203 @dispatch = ( match_true() => 5 );
205 my $get = run_request( GET => 'http://localhost/' );
207 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
208 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
209 "the error message points out the broken definition";
212 sub matcher_undef_method {
213 @dispatch = ( 'GET', undef );
215 my $get = run_request( GET => 'http://localhost/' );
217 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
218 like $get->content, qr[No idea how we got here with GET],
219 "the error message points out the broken definition";