allow passing either psgi app, or application object to Web::Dispatch
[catagits/Web-Simple.git] / t / dispatch_misc.t
index 3876e00..2924a9d 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;
 
@@ -21,12 +22,14 @@ my $app = MiscTest->new;
 sub run_request { $app->run_test_request( @_ ); }
 
 app_is_non_plack();
+app_is_object();
 plack_app_return();
 broken_route_def();
 invalid_psgi_responses();
 middleware_as_only_route();
 route_returns_middleware_plus_extra();
 route_returns_undef();
+matcher_nonsub_pair();
 
 done_testing();
 
@@ -34,7 +37,7 @@ 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/,
@@ -42,6 +45,20 @@ sub app_is_non_plack {
     undef $@;
 }
 
+sub app_is_object {
+    {
+
+        package ObjectApp;
+        use Moo;
+        sub to_app { [ 999, [], ["ok"] ] }
+    }
+
+    my $d = Web::Dispatch->new( dispatch_object => ObjectApp->new );
+    my $res = $d->call;
+
+    cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
+}
+
 sub plack_app_return {
     {
 
@@ -145,3 +162,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";
+}