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( @_ ); }
27 invalid_psgi_responses();
28 middleware_as_only_route();
29 route_returns_middleware_plus_extra();
30 route_returns_undef();
31 matcher_nonsub_pair();
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";
82 sub invalid_psgi_responses {
86 [ [ sub { } ], "an arrayref with a single sub in it" ],
87 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
88 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
91 for my $response ( @responses ) {
92 @dispatch = ( sub (/) { $response->[0] } );
94 eval { run_request( GET => 'http://localhost/' ) };
96 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
98 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
104 sub middleware_as_only_route {
105 @dispatch = ( bless {}, "Plack::Middleware" );
107 my $get = run_request( GET => 'http://localhost/' );
109 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
110 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
111 "the error message mentions the middleware class";
114 sub route_returns_middleware_plus_extra {
117 return ( bless( {}, "Plack::Middleware" ), "" );
121 my $get = run_request( GET => 'http://localhost/' );
123 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
125 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
126 "the error message mentions the middleware class";
129 sub route_returns_undef {
146 my $get = run_request( GET => 'http://localhost/' );
148 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
151 sub matcher_nonsub_pair {
152 @dispatch = ( match_true() => 5 );
154 my $get = run_request( GET => 'http://localhost/' );
156 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
157 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
158 "the error message points out the broken definition";