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