consolidate tests for invalid psgi responses
[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" ],
35075f9d 86 );
87
19b1ef0b 88 for my $response ( @responses ) {
89 @dispatch = ( sub (/) { $response->[0] } );
35075f9d 90
19b1ef0b 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 }
35075f9d 99}
100
101sub 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
111sub 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
126sub 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}