6941513b353934236aca5b1de48e8c6cca116653
[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 invalid_psgi_responses();
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
80 sub invalid_psgi_responses {
81     undef $@;
82
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" ],
86     );
87
88     for my $response ( @responses ) {
89         @dispatch = ( sub (/) { $response->[0] } );
90
91         eval { run_request( GET => 'http://localhost/' ) };
92
93         like $@, qr/Can't call method "request" on an undefined value .*MockHTTP/,
94           sprintf(
95             "if a route returns %s, then that is returned as a response by WD, causing HTTP::Message::PSGI to choke",
96             $response->[1] );
97         undef $@;
98     }
99 }
100
101 sub middleware_as_only_route {
102     @dispatch = ( bless {}, "Plack::Middleware" );
103
104     my $get = run_request( GET => 'http://localhost/' );
105
106     cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
107     like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
108       "the error message mentions the middleware class";
109 }
110
111 sub route_returns_middleware_plus_extra {
112     @dispatch = (
113         sub (/) {
114             return ( bless( {}, "Plack::Middleware" ), "" );
115         }
116     );
117
118     my $get = run_request( GET => 'http://localhost/' );
119
120     cmp_ok $get->code, '==', 500, "a route returning a middleware and at least one other variable causes a bail";
121     like $get->content,
122       qr[Multiple results but first one is a middleware \(Plack::Middleware=],
123       "the error message mentions the middleware class";
124 }
125
126 sub route_returns_undef {
127     @dispatch = (
128         sub (/) {
129             (
130                 sub(/) {
131                     undef;
132                 },
133                 sub(/) {
134                     [ 900, [], [""] ];
135                 }
136             );
137         },
138         sub () {
139             [ 400, [], [""] ];
140         }
141     );
142
143     my $get = run_request( GET => 'http://localhost/' );
144
145     cmp_ok $get->code, '==', 900, "a route that returns undef causes WD to ignore it and resume dispatching";
146 }