2 use warnings FATAL => 'all';
3 no warnings::illegalproto;
7 use HTTP::Request::Common qw(GET POST);
14 use Web::Simple 'MiscTest';
17 sub dispatch_request { @dispatch }
20 my $app = MiscTest->new;
21 sub run_request { $app->run_test_request( @_ ); }
28 middleware_as_only_route();
29 route_returns_middleware_plus_extra();
30 route_returns_undef();
34 sub app_is_non_plack {
36 my $r = HTTP::Response->new( 999 );
38 my $d = Web::Dispatch->new( app => $r );
41 like $@, qr/No idea how we got here with HTTP::Response/,
42 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
46 sub plack_app_return {
50 sub new { bless {}, $_[0] }
65 my $get = run_request( GET => 'http://localhost/' );
67 cmp_ok $get->code, '==', 999,
68 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
71 sub broken_route_def {
73 @dispatch = ( '/' => "" );
75 my $get = run_request( GET => 'http://localhost/' );
77 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
78 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
92 eval { run_request( GET => 'http://localhost/' ) };
94 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
95 "if a route returns an arrayref with a single sub in it, then that sub is returned as a response by WD, causing HTTP::Message::PSGI to choke";
98 sub array_with_no_sub {
105 eval { run_request( GET => 'http://localhost/' ) };
107 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
108 "if a route returns an arrayref with a scalar that is not a sub, then WD returns that array out of the PSGI app (and causes HTTP::Message::PSGI to choke)";
112 sub middleware_as_only_route {
113 @dispatch = ( bless {}, "Plack::Middleware" );
115 my $get = run_request( GET => 'http://localhost/' );
117 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
118 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
119 "the error message mentions the middleware class";
122 sub route_returns_middleware_plus_extra {
125 return ( bless( {}, "Plack::Middleware" ), "" );
129 my $get = run_request( GET => 'http://localhost/' );
131 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
133 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
134 "the error message mentions the middleware class";
137 sub route_returns_undef {
154 my $get = run_request( GET => 'http://localhost/' );
156 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";