Release commit for 0.020
[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;
10
11my @dispatch;
12
13{
14 use Web::Simple 'MiscTest';
15
16 package MiscTest;
17 sub dispatch_request { @dispatch }
18}
19
20my $app = MiscTest->new;
21sub run_request { $app->run_test_request( @_ ); }
22
23app_is_non_plack();
24plack_app_return();
25broken_route_def();
26array_with_sub();
27array_with_no_sub();
28middleware_as_only_route();
29route_returns_middleware_plus_extra();
30route_returns_undef();
31
32done_testing();
33
34sub 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
46sub 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
71sub 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
81sub array_with_sub {
82 @dispatch = (
83 sub (/) {
84 [
85 sub {
86 [ 999, [], [""] ];
87 },
88 ];
89 }
90 );
91
1a0ea82a 92 eval { run_request( GET => 'http://localhost/' ) };
35075f9d 93
1a0ea82a 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";
35075f9d 96}
97
98sub 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
112sub 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
122sub 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
137sub 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}