fix a memory leak introduced by 1f8cad5e5a1875de94d63ac91d8ded4d2282c62e
[catagits/Web-Simple.git] / t / dispatch_misc.t
index 0c11f2c..842cf52 100644 (file)
@@ -7,6 +7,7 @@ use Test::More;
 use HTTP::Request::Common qw(GET POST);
 use Web::Dispatch;
 use HTTP::Response;
+use Web::Dispatch::Predicates 'match_true';
 
 my @dispatch;
 
@@ -15,27 +16,39 @@ my @dispatch;
 
     package MiscTest;
     sub dispatch_request { @dispatch }
+    sub string_method { [ 999, [], [""] ]; }
 }
 
 my $app = MiscTest->new;
 sub run_request { $app->run_test_request( @_ ); }
 
+string_method_name();
 app_is_non_plack();
+app_is_object();
+app_is_just_sub();
 plack_app_return();
 broken_route_def();
-array_with_sub();
-array_with_no_sub();
+invalid_psgi_responses();
 middleware_as_only_route();
 route_returns_middleware_plus_extra();
 route_returns_undef();
+matcher_nonsub_pair();
 
 done_testing();
 
+sub string_method_name {
+    @dispatch = ( '/' => "string_method" );
+
+    my $get = run_request( GET => 'http://localhost/' );
+
+    cmp_ok $get->code, '==', 999, "a dispatcher that's a string matching a method on the dispatch object gets executed";
+}
+
 sub app_is_non_plack {
 
     my $r = HTTP::Response->new( 999 );
 
-    my $d = Web::Dispatch->new( app => $r );
+    my $d = Web::Dispatch->new( dispatch_app => $r );
     eval { $d->call };
 
     like $@, qr/No idea how we got here with HTTP::Response/,
@@ -43,6 +56,29 @@ sub app_is_non_plack {
     undef $@;
 }
 
+sub app_is_object {
+    {
+
+        package ObjectApp;
+        use Moo;
+        sub to_app { [ 999, [], ["ok"] ] }
+    }
+
+    my $o = ObjectApp->new;
+    my $d = Web::Dispatch->new( dispatch_object => $o );
+    my $res = $d->call;
+
+    cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
+}
+
+sub app_is_just_sub {
+    my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
+    my $res = $d->call( {} );
+
+    cmp_ok $res->[0], '==', 999,
+      "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
+}
+
 sub plack_app_return {
     {
 
@@ -78,35 +114,26 @@ sub broken_route_def {
     like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
 }
 
-sub array_with_sub {
-    @dispatch = (
-        sub (/) {
-            [
-                sub {
-                    [ 999, [], [""] ];
-                },
-            ];
-        }
-    );
-
-    eval { run_request( GET => 'http://localhost/' ) };
-
-    like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
-"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";
-}
+sub invalid_psgi_responses {
+    undef $@;
 
-sub array_with_no_sub {
-    @dispatch = (
-        sub (/) {
-            ["moo"];
-        }
+    my @responses = (
+        [ [ sub { } ], "an arrayref with a single sub in it" ],
+        [ ["moo"], "an arrayref with a scalar that is not a sub" ],
+        [ bless( {}, "FauxObject" ), "an object without to_app method" ],
     );
 
-    eval { run_request( GET => 'http://localhost/' ) };
+    for my $response ( @responses ) {
+        @dispatch = ( sub (/) { $response->[0] } );
 
-    like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
-"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)";
-    undef $@;
+        eval { run_request( GET => 'http://localhost/' ) };
+
+        like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
+          sprintf(
+            "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
+            $response->[1] );
+        undef $@;
+    }
 }
 
 sub middleware_as_only_route {
@@ -155,3 +182,13 @@ sub route_returns_undef {
 
     cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
 }
+
+sub matcher_nonsub_pair {
+    @dispatch = ( match_true() => 5 );
+
+    my $get = run_request( GET => 'http://localhost/' );
+
+    cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
+    like $get->content, qr[No idea how we got here with Web::Dispatch::M],
+      "the error message points out the broken definition";
+}