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 }
21 my $app = MiscTest->new;
22 sub run_request { $app->run_test_request( @_ ); }
29 invalid_psgi_responses();
30 middleware_as_only_route();
31 route_returns_middleware_plus_extra();
32 route_returns_undef();
33 matcher_nonsub_pair();
37 sub app_is_non_plack {
39 my $r = HTTP::Response->new( 999 );
41 my $d = Web::Dispatch->new( dispatch_app => $r );
44 like $@, qr/No idea how we got here with HTTP::Response/,
45 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
54 sub to_app { [ 999, [], ["ok"] ] }
57 my $d = Web::Dispatch->new( dispatch_object => ObjectApp->new );
60 cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
64 my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
65 my $res = $d->call( {} );
67 cmp_ok $res->[0], '==', 999,
68 "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
71 sub plack_app_return {
75 sub new { bless {}, $_[0] }
90 my $get = run_request( GET => 'http://localhost/' );
92 cmp_ok $get->code, '==', 999,
93 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
96 sub broken_route_def {
98 @dispatch = ( '/' => "" );
100 my $get = run_request( GET => 'http://localhost/' );
102 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
103 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
106 sub invalid_psgi_responses {
110 [ [ sub { } ], "an arrayref with a single sub in it" ],
111 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
112 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
115 for my $response ( @responses ) {
116 @dispatch = ( sub (/) { $response->[0] } );
118 eval { run_request( GET => 'http://localhost/' ) };
120 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
122 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
128 sub middleware_as_only_route {
129 @dispatch = ( bless {}, "Plack::Middleware" );
131 my $get = run_request( GET => 'http://localhost/' );
133 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
134 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
135 "the error message mentions the middleware class";
138 sub route_returns_middleware_plus_extra {
141 return ( bless( {}, "Plack::Middleware" ), "" );
145 my $get = run_request( GET => 'http://localhost/' );
147 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
149 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
150 "the error message mentions the middleware class";
153 sub route_returns_undef {
170 my $get = run_request( GET => 'http://localhost/' );
172 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
175 sub matcher_nonsub_pair {
176 @dispatch = ( match_true() => 5 );
178 my $get = run_request( GET => 'http://localhost/' );
180 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
181 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
182 "the error message points out the broken definition";