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 $d = Web::Dispatch->new( dispatch_object => ObjectApp->new );
70 cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
74 my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
75 my $res = $d->call( {} );
77 cmp_ok $res->[0], '==', 999,
78 "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
81 sub plack_app_return {
85 sub new { bless {}, $_[0] }
100 my $get = run_request( GET => 'http://localhost/' );
102 cmp_ok $get->code, '==', 999,
103 "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
106 sub broken_route_def {
108 @dispatch = ( '/' => "" );
110 my $get = run_request( GET => 'http://localhost/' );
112 cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
113 like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
116 sub invalid_psgi_responses {
120 [ [ sub { } ], "an arrayref with a single sub in it" ],
121 [ ["moo"], "an arrayref with a scalar that is not a sub" ],
122 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
125 for my $response ( @responses ) {
126 @dispatch = ( sub (/) { $response->[0] } );
128 eval { run_request( GET => 'http://localhost/' ) };
130 like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
132 "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
138 sub middleware_as_only_route {
139 @dispatch = ( bless {}, "Plack::Middleware" );
141 my $get = run_request( GET => 'http://localhost/' );
143 cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
144 like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
145 "the error message mentions the middleware class";
148 sub route_returns_middleware_plus_extra {
151 return ( bless( {}, "Plack::Middleware" ), "" );
155 my $get = run_request( GET => 'http://localhost/' );
157 cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
159 qr[Multiple results but first one is a middleware \(Plack::Middleware=],
160 "the error message mentions the middleware class";
163 sub route_returns_undef {
180 my $get = run_request( GET => 'http://localhost/' );
182 cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
185 sub matcher_nonsub_pair {
186 @dispatch = ( match_true() => 5 );
188 my $get = run_request( GET => 'http://localhost/' );
190 cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
191 like $get->content, qr[No idea how we got here with Web::Dispatch::M],
192 "the error message points out the broken definition";