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 plain_middleware_with_route();
30 route_returns_middleware_plus_extra();
31 route_returns_undef();
35 sub app_is_non_plack {
37 my $r = HTTP::Response->new( 999 );
39 my $d = Web::Dispatch->new( app => $r );
42 like $@, qr/No idea how we got here with HTTP::Response/,
43 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
47 sub plack_app_return {
51 sub new { bless {}, $_[0] }
66 my $get = run_request( GET => 'http://localhost/' );
68 cmp_ok $get->code, '==', 999,
69 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
72 sub broken_route_def {
74 @dispatch = ( '/' => "" );
76 my $get = run_request( GET => 'http://localhost/' );
78 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
79 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
93 eval { run_request( GET => 'http://localhost/' ) };
95 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
96 "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";
99 sub array_with_no_sub {
106 eval { run_request( GET => 'http://localhost/' ) };
108 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
109 "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)";
113 sub middleware_as_only_route {
114 @dispatch = ( bless {}, "Plack::Middleware" );
116 my $get = run_request( GET => 'http://localhost/' );
118 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
119 like $get->content, qr[Middleware needs a route definition and has to be wrapped in a sub.],
120 "the error message explains the need for a route def and a code ref";
123 sub plain_middleware_with_route {
124 @dispatch = ( '' => bless {}, "Plack::Middleware" );
126 my $get = run_request( GET => 'http://localhost/' );
128 cmp_ok $get->code, '==', 500, "a route definition consisting of a string and a plain middleware causes a bail";
129 like $get->content, qr[Middleware needs to be wrapped in a sub.],
130 "the error message mentions the middleware class";
133 sub route_returns_middleware_plus_extra {
136 return ( bless( {}, "Plack::Middleware" ), "" );
140 my $get = run_request( GET => 'http://localhost/' );
142 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
144 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
145 "the error message mentions the middleware class";
148 sub route_returns_undef {
165 my $get = run_request( GET => 'http://localhost/' );
167 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";