X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fdispatch_misc.t;h=88ddf5d720dcc7a2d1a2e41ef114c64ab6d0dd86;hb=038c2bdcbc5a7713052190aeb69df434a609a8f6;hp=3876e0051ec960bba7a9e406aa6c971a1f3f798a;hpb=23dee6a3ce7616c9e6050f61c46b2e3942eeaa3f;p=catagits%2FWeb-Simple.git diff --git a/t/dispatch_misc.t b/t/dispatch_misc.t index 3876e00..88ddf5d 100644 --- a/t/dispatch_misc.t +++ b/t/dispatch_misc.t @@ -2,11 +2,12 @@ use strict; use warnings FATAL => 'all'; no warnings::illegalproto; -use Test::More; +use Test::More 0.88; use HTTP::Request::Common qw(GET POST); use Web::Dispatch; use HTTP::Response; +use Web::Dispatch::Predicates 'match_true'; my @dispatch; @@ -15,26 +16,45 @@ my @dispatch; package MiscTest; sub dispatch_request { @dispatch } + sub string_method { [ 999, [], [""] ]; } + + sub can { + die "Passed undef to can, this blows up on 5.8" unless defined($_[1]); + shift->SUPER::can(@_) + } } 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(); invalid_psgi_responses(); middleware_as_only_route(); route_returns_middleware_plus_extra(); route_returns_undef(); +matcher_nonsub_pair(); +matcher_undef_method(); 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/, @@ -42,6 +62,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 { { @@ -89,12 +132,22 @@ sub invalid_psgi_responses { for my $response ( @responses ) { @dispatch = ( sub (/) { $response->[0] } ); - eval { run_request( GET => 'http://localhost/' ) }; - - like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/, - sprintf( + my $message = sprintf( "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke", - $response->[1] ); + $response->[1] + ); + + # Somewhere between 1.0028 and 1.0031 Plack changed so that the + # FauxObject case became a 500 rather than a die; in case it later does + # the same thing for other stuff, just accept either sort of error + + my $res = eval { run_request( GET => 'http://localhost/' ) }; + + if ($res) { + ok $res->is_error, $message; + } else { + like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/, $message; + } undef $@; } } @@ -145,3 +198,23 @@ 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"; +} + +sub matcher_undef_method { + @dispatch = ( 'GET', undef ); + + 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 GET], + "the error message points out the broken definition"; +}