add a test for trying to dispatch on a pair of WD::Matcher and non-sub
[catagits/Web-Simple.git] / t / dispatch_misc.t
CommitLineData
35075f9d 1use strict;
2use warnings FATAL => 'all';
3no warnings::illegalproto;
4
5use Test::More;
6
7use HTTP::Request::Common qw(GET POST);
8use Web::Dispatch;
9use HTTP::Response;
efbff594 10use Web::Dispatch::Predicates 'match_true';
35075f9d 11
12my @dispatch;
13
14{
15 use Web::Simple 'MiscTest';
16
17 package MiscTest;
18 sub dispatch_request { @dispatch }
19}
20
21my $app = MiscTest->new;
22sub run_request { $app->run_test_request( @_ ); }
23
24app_is_non_plack();
25plack_app_return();
26broken_route_def();
19b1ef0b 27invalid_psgi_responses();
35075f9d 28middleware_as_only_route();
29route_returns_middleware_plus_extra();
30route_returns_undef();
efbff594 31matcher_nonsub_pair();
35075f9d 32
33done_testing();
34
35sub 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
47sub 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
72sub 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
19b1ef0b 82sub invalid_psgi_responses {
83 undef $@;
35075f9d 84
19b1ef0b 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" ],
23dee6a3 88 [ bless( {}, "FauxObject" ), "an object without to_app method" ],
35075f9d 89 );
90
19b1ef0b 91 for my $response ( @responses ) {
92 @dispatch = ( sub (/) { $response->[0] } );
35075f9d 93
19b1ef0b 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 }
35075f9d 102}
103
104sub 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
114sub 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
129sub 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}
efbff594 150
151sub 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}