add test for to_app-less object as invalid psgi response, completing coverage of...
[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();
19b1ef0b 26invalid_psgi_responses();
35075f9d 27middleware_as_only_route();
28route_returns_middleware_plus_extra();
29route_returns_undef();
30
31done_testing();
32
33sub 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
45sub 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
70sub 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 80sub 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
102sub 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
112sub 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
127sub 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}