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