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( @_ ); }
25 plack_streaming_app_return();
29 middleware_as_only_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 plack_streaming_app_return {
75 package FauxPlackStreamApp;
76 sub new { bless {}, $_[0] }
81 my $responder = shift;
82 $responder->([ 999, [], ["foo"] ]);
90 FauxPlackStreamApp->new;
94 my $get = run_request( GET => 'http://localhost/' );
96 cmp_ok $get->code, '==', 999,
97 "when a route returns a thing that look like a Plack Streaming app, the web app redispatches to that thing";
100 sub broken_route_def {
102 @dispatch = ( '/' => "" );
104 my $get = run_request( GET => 'http://localhost/' );
106 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
107 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
121 eval { run_request( GET => 'http://localhost/' ) };
123 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
124 "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";
127 sub array_with_no_sub {
134 eval { run_request( GET => 'http://localhost/' ) };
136 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
137 "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)";
141 sub middleware_as_only_route {
142 @dispatch = ( bless {}, "Plack::Middleware" );
144 my $get = run_request( GET => 'http://localhost/' );
146 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
147 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
148 "the error message mentions the middleware class";
151 sub route_returns_middleware_plus_extra {
154 return ( bless( {}, "Plack::Middleware" ), "" );
158 my $get = run_request( GET => 'http://localhost/' );
160 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
162 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
163 "the error message mentions the middleware class";
166 sub route_returns_undef {
183 my $get = run_request( GET => 'http://localhost/' );
185 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";