Commit | Line | Data |
35075f9d |
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; |
efbff594 |
10 | use Web::Dispatch::Predicates 'match_true'; |
35075f9d |
11 | |
12 | my @dispatch; |
13 | |
14 | { |
15 | use Web::Simple 'MiscTest'; |
16 | |
17 | package MiscTest; |
18 | sub dispatch_request { @dispatch } |
b2e37c62 |
19 | sub string_method { [ 999, [], [""] ]; } |
35075f9d |
20 | } |
21 | |
22 | my $app = MiscTest->new; |
23 | sub run_request { $app->run_test_request( @_ ); } |
24 | |
b2e37c62 |
25 | string_method_name(); |
35075f9d |
26 | app_is_non_plack(); |
e5250d96 |
27 | app_is_object(); |
1f8cad5e |
28 | app_is_just_sub(); |
35075f9d |
29 | plack_app_return(); |
30 | broken_route_def(); |
19b1ef0b |
31 | invalid_psgi_responses(); |
35075f9d |
32 | middleware_as_only_route(); |
33 | route_returns_middleware_plus_extra(); |
34 | route_returns_undef(); |
efbff594 |
35 | matcher_nonsub_pair(); |
35075f9d |
36 | |
37 | done_testing(); |
38 | |
b2e37c62 |
39 | sub string_method_name { |
40 | @dispatch = ( '/' => "string_method" ); |
41 | |
42 | my $get = run_request( GET => 'http://localhost/' ); |
43 | |
44 | cmp_ok $get->code, '==', 999, "a dispatcher that's a string matching a method on the dispatch object gets executed"; |
45 | } |
46 | |
35075f9d |
47 | sub app_is_non_plack { |
48 | |
49 | my $r = HTTP::Response->new( 999 ); |
50 | |
e5250d96 |
51 | my $d = Web::Dispatch->new( dispatch_app => $r ); |
35075f9d |
52 | eval { $d->call }; |
53 | |
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"; |
56 | undef $@; |
57 | } |
58 | |
e5250d96 |
59 | sub app_is_object { |
60 | { |
61 | |
62 | package ObjectApp; |
63 | use Moo; |
64 | sub to_app { [ 999, [], ["ok"] ] } |
65 | } |
66 | |
1f727762 |
67 | my $o = ObjectApp->new; |
68 | my $d = Web::Dispatch->new( dispatch_object => $o ); |
e5250d96 |
69 | my $res = $d->call; |
70 | |
71 | cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method"; |
72 | } |
73 | |
1f8cad5e |
74 | sub app_is_just_sub { |
75 | my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } ); |
76 | my $res = $d->call( {} ); |
77 | |
78 | cmp_ok $res->[0], '==', 999, |
79 | "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved"; |
80 | } |
81 | |
35075f9d |
82 | sub plack_app_return { |
83 | { |
84 | |
85 | package FauxPlackApp; |
86 | sub new { bless {}, $_[0] } |
87 | |
88 | sub to_app { |
89 | return sub { |
90 | [ 999, [], [""] ]; |
91 | }; |
92 | } |
93 | } |
94 | |
95 | @dispatch = ( |
96 | sub (/) { |
97 | FauxPlackApp->new; |
98 | } |
99 | ); |
100 | |
101 | my $get = run_request( GET => 'http://localhost/' ); |
102 | |
103 | cmp_ok $get->code, '==', 999, |
104 | "when a route returns a thing that look like a Plack app, the web app redispatches to that thing"; |
105 | } |
106 | |
107 | sub broken_route_def { |
108 | |
109 | @dispatch = ( '/' => "" ); |
110 | |
111 | my $get = run_request( GET => 'http://localhost/' ); |
112 | |
113 | cmp_ok $get->code, '==', 500, "a route definition by hash that doesn't pair a sub with a route dies"; |
114 | like $get->content, qr[No idea how we got here with /], "the error message points out the broken definition"; |
115 | } |
116 | |
19b1ef0b |
117 | sub invalid_psgi_responses { |
118 | undef $@; |
35075f9d |
119 | |
19b1ef0b |
120 | my @responses = ( |
121 | [ [ sub { } ], "an arrayref with a single sub in it" ], |
122 | [ ["moo"], "an arrayref with a scalar that is not a sub" ], |
23dee6a3 |
123 | [ bless( {}, "FauxObject" ), "an object without to_app method" ], |
35075f9d |
124 | ); |
125 | |
19b1ef0b |
126 | for my $response ( @responses ) { |
127 | @dispatch = ( sub (/) { $response->[0] } ); |
35075f9d |
128 | |
e0f92e3e |
129 | my $message = sprintf( |
19b1ef0b |
130 | "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke", |
e0f92e3e |
131 | $response->[1] |
132 | ); |
133 | |
134 | # Somewhere between 1.0028 and 1.0031 Plack changed so that the |
135 | # FauxObject case became a 500 rather than a die; in case it later does |
136 | # the same thing for other stuff, just accept either sort of error |
137 | |
138 | my $res = eval { run_request( GET => 'http://localhost/' ) }; |
139 | |
140 | if ($res) { |
141 | ok $res->is_error, $message; |
142 | } else { |
143 | like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/, $message; |
144 | } |
19b1ef0b |
145 | undef $@; |
146 | } |
35075f9d |
147 | } |
148 | |
149 | sub middleware_as_only_route { |
150 | @dispatch = ( bless {}, "Plack::Middleware" ); |
151 | |
152 | my $get = run_request( GET => 'http://localhost/' ); |
153 | |
154 | cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail"; |
155 | like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=], |
156 | "the error message mentions the middleware class"; |
157 | } |
158 | |
159 | sub route_returns_middleware_plus_extra { |
160 | @dispatch = ( |
161 | sub (/) { |
162 | return ( bless( {}, "Plack::Middleware" ), "" ); |
163 | } |
164 | ); |
165 | |
166 | my $get = run_request( GET => 'http://localhost/' ); |
167 | |
168 | cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail"; |
169 | like $get->content, |
170 | qr[Multiple results but first one is a middleware \(Plack::Middleware=], |
171 | "the error message mentions the middleware class"; |
172 | } |
173 | |
174 | sub route_returns_undef { |
175 | @dispatch = ( |
176 | sub (/) { |
177 | ( |
178 | sub(/) { |
179 | undef; |
180 | }, |
181 | sub(/) { |
182 | [ 900, [], [""] ]; |
183 | } |
184 | ); |
185 | }, |
186 | sub () { |
187 | [ 400, [], [""] ]; |
188 | } |
189 | ); |
190 | |
191 | my $get = run_request( GET => 'http://localhost/' ); |
192 | |
193 | cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching"; |
194 | } |
efbff594 |
195 | |
196 | sub matcher_nonsub_pair { |
197 | @dispatch = ( match_true() => 5 ); |
198 | |
199 | my $get = run_request( GET => 'http://localhost/' ); |
200 | |
201 | cmp_ok $get->code, '==', 500, "a route definition that pairs a WD::Matcher a non-sub dies"; |
202 | like $get->content, qr[No idea how we got here with Web::Dispatch::M], |
203 | "the error message points out the broken definition"; |
204 | } |