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( @_ ); }
26 invalid_psgi_responses();
27 middleware_as_only_route();
28 route_returns_middleware_plus_extra();
29 route_returns_undef();
33 sub app_is_non_plack {
35 my $r = HTTP::Response->new( 999 );
37 my $d = Web::Dispatch->new( app => $r );
40 like $@, qr/No idea how we got here with HTTP::Response/,
41 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
45 sub plack_app_return {
49 sub new { bless {}, $_[0] }
64 my $get = run_request( GET => 'http://localhost/' );
66 cmp_ok $get->code, '==', 999,
67 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
70 sub broken_route_def {
72 @dispatch = ( '/' => "" );
74 my $get = run_request( GET => 'http://localhost/' );
76 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
77 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
80 sub invalid_psgi_responses {
84 [ [ sub { } ], "an arrayref with a single sub in it" ],
85 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
86 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
89 for my $response ( @responses ) {
90 @dispatch = ( sub (/) { $response->[0] } );
92 eval { run_request( GET => 'http://localhost/' ) };
94 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
96 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
102 sub middleware_as_only_route {
103 @dispatch = ( bless {}, "Plack::Middleware" );
105 my $get = run_request( GET => 'http://localhost/' );
107 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
108 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
109 "the error message mentions the middleware class";
112 sub route_returns_middleware_plus_extra {
115 return ( bless( {}, "Plack::Middleware" ), "" );
119 my $get = run_request( GET => 'http://localhost/' );
121 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
123 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
124 "the error message mentions the middleware class";
127 sub route_returns_undef {
144 my $get = run_request( GET => 'http://localhost/' );
146 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";