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 }
19 sub string_method { [ 999, [], [""] ]; }
22 my $app = MiscTest->new;
23 sub run_request { $app->run_test_request( @_ ); }
31 invalid_psgi_responses();
32 middleware_as_only_route();
33 route_returns_middleware_plus_extra();
34 route_returns_undef();
35 matcher_nonsub_pair();
39 sub string_method_name {
40 @dispatch = ( '/' => "string_method" );
42 my $get = run_request( GET => 'http://localhost/' );
44 cmp_ok $get->code, '==', 999, "a dispatcher that's a string matching a method on the dispatch object gets executed";
47 sub app_is_non_plack {
49 my $r = HTTP::Response->new( 999 );
51 my $d = Web::Dispatch->new( dispatch_app => $r );
54 like $@, qr/No idea how we got here with HTTP::Response/,
55 "Web::Dispatch dies when run with an app() that is a non-PSGI object";
64 sub to_app { [ 999, [], ["ok"] ] }
67 my $o = ObjectApp->new;
68 my $d = Web::Dispatch->new( dispatch_object => $o );
71 cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
75 my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
76 my $res = $d->call( {} );
78 cmp_ok $res->[0], '==', 999,
79 "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
82 sub plack_app_return {
86 sub new { bless {}, $_[0] }
101 my $get = run_request( GET => 'http://localhost/' );
103 cmp_ok $get->code, '==', 999,
104 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
107 sub broken_route_def {
109 @dispatch = ( '/' => "" );
111 my $get = run_request( GET => 'http://localhost/' );
113 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
114 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
117 sub invalid_psgi_responses {
121 [ [ sub { } ], "an arrayref with a single sub in it" ],
122 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
123 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
126 for my $response ( @responses ) {
127 @dispatch = ( sub (/) { $response->[0] } );
129 my $message = sprintf(
130 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
134 # Somewhere between 1.0028 and 1.0031 Plack changed so that the
135 # FauxObject case became a 500 rather than a die; in case it later does
136 # the same thing for other stuff, just accept either sort of error
138 my $res = eval { run_request( GET => 'http://localhost/' ) };
141 ok $res->is_error, $message;
143 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/, $message;
149 sub middleware_as_only_route {
150 @dispatch = ( bless {}, "Plack::Middleware" );
152 my $get = run_request( GET => 'http://localhost/' );
154 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
155 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
156 "the error message mentions the middleware class";
159 sub route_returns_middleware_plus_extra {
162 return ( bless( {}, "Plack::Middleware" ), "" );
166 my $get = run_request( GET => 'http://localhost/' );
168 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
170 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
171 "the error message mentions the middleware class";
174 sub route_returns_undef {
191 my $get = run_request( GET => 'http://localhost/' );
193 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
196 sub matcher_nonsub_pair {
197 @dispatch = ( match_true() => 5 );
199 my $get = run_request( GET => 'http://localhost/' );
201 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
202 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
203 "the error message points out the broken definition";