add a test for trying to dispatch on a pair of WD::Matcher and non-sub
[catagits/Web-Simple.git] / t / dispatch_misc.t
1 use strict;
2 use warnings FATAL => 'all';
3 no warnings::illegalproto;
4
5 use Test::More;
6
7 use HTTP::Request::Common qw(GET POST);
8 use Web::Dispatch;
9 use HTTP::Response;
10 use Web::Dispatch::Predicates 'match_true';
11
12 my @dispatch;
13
14 {
15     use Web::Simple 'MiscTest';
16
17     package MiscTest;
18     sub dispatch_request { @dispatch }
19 }
20
21 my $app = MiscTest->new;
22 sub run_request { $app->run_test_request( @_ ); }
23
24 app_is_non_plack();
25 plack_app_return();
26 broken_route_def();
27 invalid_psgi_responses();
28 middleware_as_only_route();
29 route_returns_middleware_plus_extra();
30 route_returns_undef();
31 matcher_nonsub_pair();
32
33 done_testing();
34
35 sub app_is_non_plack {
36
37     my $r = HTTP::Response->new( 999 );
38
39     my $d = Web::Dispatch->new( app => $r );
40     eval { $d->call };
41
42     like $@, qr/No idea how we got here with HTTP::Response/,
43       "Web::Dispatch dies when run with an app() that is a non-PSGI object";
44     undef $@;
45 }
46
47 sub plack_app_return {
48     {
49
50         package FauxPlackApp;
51         sub new { bless {}, $_[0] }
52
53         sub to_app {
54             return sub {
55                 [ 999, [], [""] ];
56             };
57         }
58     }
59
60     @dispatch = (
61         sub (/) {
62             FauxPlackApp->new;
63         }
64     );
65
66     my $get = run_request( GET => 'http://localhost/' );
67
68     cmp_ok $get->code, '==', 999,
69       "when a route returns a thing that look like a Plack app, the web app redispatches to that thing";
70 }
71
72 sub broken_route_def {
73
74     @dispatch = ( '/' => "" );
75
76     my $get = run_request( GET => 'http://localhost/' );
77
78     cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies";
79     like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition";
80 }
81
82 sub invalid_psgi_responses {
83     undef $@;
84
85     my @responses = (
86         [ [ sub { } ], "an arrayref with a single sub in it" ],
87         [ ["moo"], "an arrayref with a scalar that is not a sub" ],
88         [ bless( {}, "FauxObject" ), "an object without to_app method" ],
89     );
90
91     for my $response ( @responses ) {
92         @dispatch = ( sub (/) { $response->[0] } );
93
94         eval { run_request( GET => 'http://localhost/' ) };
95
96         like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
97           sprintf(
98             "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
99             $response->[1] );
100         undef $@;
101     }
102 }
103
104 sub middleware_as_only_route {
105     @dispatch = ( bless {}, "Plack::Middleware" );
106
107     my $get = run_request( GET => 'http://localhost/' );
108
109     cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
110     like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
111       "the error message mentions the middleware class";
112 }
113
114 sub route_returns_middleware_plus_extra {
115     @dispatch = (
116         sub (/) {
117             return ( bless( {}, "Plack::Middleware" ), "" );
118         }
119     );
120
121     my $get = run_request( GET => 'http://localhost/' );
122
123     cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
124     like $get->content,
125       qr[Multiple results but first one is a middleware \(Plack::Middleware=],
126       "the error message mentions the middleware class";
127 }
128
129 sub route_returns_undef {
130     @dispatch = (
131         sub (/) {
132             (
133                 sub(/) {
134                     undef;
135                 },
136                 sub(/) {
137                     [ 900, [], [""] ];
138                 }
139             );
140         },
141         sub () {
142             [ 400, [], [""] ];
143         }
144     );
145
146     my $get = run_request( GET => 'http://localhost/' );
147
148     cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
149 }
150
151 sub matcher_nonsub_pair {
152     @dispatch = ( match_true() => 5 );
153
154     my $get = run_request( GET => 'http://localhost/' );
155
156     cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies";
157     like $get->content, qr[No idea how we got here with Web::Dispatch::M],
158       "the error message points out the broken definition";
159 }