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( @_ ); }
28 invalid_psgi_responses();
29 middleware_as_only_route();
30 route_returns_middleware_plus_extra();
31 route_returns_undef();
32 matcher_nonsub_pair();
36 sub app_is_non_plack {
38 my $r = HTTP::Response->new( 999 );
40 my $d = Web::Dispatch->new( dispatch_app => $r );
43 like $@, qr/No idea how we got here with HTTP::Response/,
44 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
53 sub to_app { [ 999, [], ["ok"] ] }
56 my $d = Web::Dispatch->new( dispatch_object => ObjectApp->new );
59 cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
62 sub plack_app_return {
66 sub new { bless {}, $_[0] }
81 my $get = run_request( GET => 'http://localhost/' );
83 cmp_ok $get->code, '==', 999,
84 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
87 sub broken_route_def {
89 @dispatch = ( '/' => "" );
91 my $get = run_request( GET => 'http://localhost/' );
93 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
94 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
97 sub invalid_psgi_responses {
101 [ [ sub { } ], "an arrayref with a single sub in it" ],
102 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
103 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
106 for my $response ( @responses ) {
107 @dispatch = ( sub (/) { $response->[0] } );
109 eval { run_request( GET => 'http://localhost/' ) };
111 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
113 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
119 sub middleware_as_only_route {
120 @dispatch = ( bless {}, "Plack::Middleware" );
122 my $get = run_request( GET => 'http://localhost/' );
124 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
125 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
126 "the error message mentions the middleware class";
129 sub route_returns_middleware_plus_extra {
132 return ( bless( {}, "Plack::Middleware" ), "" );
136 my $get = run_request( GET => 'http://localhost/' );
138 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
140 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
141 "the error message mentions the middleware class";
144 sub route_returns_undef {
161 my $get = run_request( GET => 'http://localhost/' );
163 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
166 sub matcher_nonsub_pair {
167 @dispatch = ( match_true() => 5 );
169 my $get = run_request( GET => 'http://localhost/' );
171 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
172 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
173 "the error message points out the broken definition";