Debian Codegraph @

t/request.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use Dancer2::Core::App;
6 use Dancer2::Core::Request;
7
8 diag "If you want extra speed, install URL::Encode::XS"
9 if !$Dancer2::Core::Request::XS_URL_DECODE;
10 diag "If you want extra speed, install CGI::Deurl::XS"
11 if !$Dancer2::Core::Request::XS_PARSE_QUERY_STRING;
12
13 sub run_test {
14 my $env = {
15 'psgi.url_scheme' => 'http',
16 REQUEST_METHOD => 'GET',
17 SCRIPT_NAME => '/foo',
18 PATH_INFO => '/bar/baz',
19 REQUEST_URI => '/foo/bar/baz',
20 QUERY_STRING => 'foo=42&bar=12&bar=13&bar=14',
21 SERVER_NAME => 'localhost',
22 SERVER_PORT => 5000,
23 SERVER_PROTOCOL => 'HTTP/1.1',
24 REMOTE_ADDR => '127.0.0.1',
25 HTTP_X_FORWARDED_FOR => '127.0.0.2',
26 HTTP_X_FORWARDED_HOST => 'secure.frontend',
27 HTTP_X_FORWARDED_PROTOCOL => 'https',
28 REMOTE_HOST => 'localhost',
29 HTTP_USER_AGENT => 'Mozilla',
30 REMOTE_USER => 'sukria',
31 HTTP_COOKIE => 'cookie.a=foo=bar; cookie.b=1234abcd; no.value.cookie',
32 };
33
34 my $req = Dancer2::Core::Request->new( env => $env );
35
36 note "tests for accessors";
37
38 is $req->agent, 'Mozilla';
39 is $req->user_agent, 'Mozilla';
40 is $req->remote_address, '127.0.0.1';
41 is $req->address, '127.0.0.1';
42 is $req->forwarded_for_address, '127.0.0.2';
43 is $req->remote_host, 'localhost';
44 is $req->protocol, 'HTTP/1.1';
45 is $req->port, 5000;
46 is $req->request_uri, '/foo/bar/baz';
47 is $req->uri, '/foo/bar/baz';
48 is $req->user, 'sukria';
49 is $req->script_name, '/foo';
50 is $req->scheme, 'http';
51 is $req->referer, undef;
52 ok( !$req->secure );
53 is $req->method, 'GET';
54 is $req->request_method, 'GET';
55 ok( $req->is_get );
56 ok( !$req->is_post );
57 ok( !$req->is_put );
58 ok( !$req->is_delete );
59 ok( !$req->is_patch );
60 ok( !$req->is_head );
61
62 is $req->id, 1;
63 is $req->to_string, '[#1] GET /bar/baz';
64
65 note "tests params";
66 is_deeply { $req->params }, { foo => 42, bar => [ 12, 13, 14 ] };
67
68 note "tests cookies";
69 is( keys %{ $req->cookies }, 2, "multiple cookies extracted" );
70
71 my $forward = Dancer2::Core::App->new( request => $req )
72 ->make_forward_to('/somewhere');
73 is $forward->path_info, '/somewhere';
74 is $forward->method, 'GET';
75 note "tests for uri_for";
76 is $req->base, 'http://localhost:5000/foo';
77 is $req->uri_for( 'bar', { baz => 'baz' } ),
78 'http://localhost:5000/foo/bar?baz=baz';
79
80 is $req->uri_for('/bar'), 'http://localhost:5000/foo/bar';
81 is $req->uri_for( '/bar', undef, 1 ),
82 'http://localhost:5000/foo/bar',
83 'uri_for returns a URI (with $dont_escape)';
84
85 is $req->request_uri, '/foo/bar/baz';
86 is $req->path_info, '/bar/baz';
87
88 {
89 local $env->{SCRIPT_NAME} = '';
90 is $req->uri_for('/foo'), 'http://localhost:5000/foo';
91 }
92
93 {
94 local $env->{SERVER_NAME} = 0;
95 is $req->base, 'http://0:5000/foo';
96 local $env->{HTTP_HOST} = 'oddhostname:5000';
97 is $req->base, 'http://oddhostname:5000/foo';
98 }
99
100 note "testing behind proxy"; {
101 my $req = Dancer2::Core::Request->new(
102 env => $env,
103 is_behind_proxy => 1
104 );
105 is $req->secure, 1;
106 is $req->host, $env->{HTTP_X_FORWARDED_HOST};
107 is $req->scheme, 'https';
108 }
109
110 note "testing behind proxy when optional headers are not set"; {
111 # local modifications to env:
112 local $env->{HTTP_HOST} = 'oddhostname:5000';
113 delete local $env->{'HTTP_X_FORWARDED_FOR'};
114 delete local $env->{'HTTP_X_FORWARDED_HOST'};
115 delete local $env->{'HTTP_X_FORWARDED_PROTOCOL'};
116 my $req = Dancer2::Core::Request->new(
117 env => $env,
118 is_behind_proxy => 1
119 );
120 is ! $req->secure, 1;
121 is $req->host, 'oddhostname:5000';
122 is $req->scheme, 'http';
123 }
124
125 note "testing path and uri_base"; {
126 # Base env used for path and uri_base tests
127 my $base = {
128 'psgi.url_scheme' => 'http',
129 REQUEST_METHOD => 'GET',
130 QUERY_STRING => '',
131 SERVER_NAME => 'localhost',
132 SERVER_PORT => 5000,
133 SERVER_PROTOCOL => 'HTTP/1.1',
134 };
135
136 # PATH_INFO not set
137 my $env = {
138 %$base,
139 SCRIPT_NAME => '/foo',
140 PATH_INFO => '',
141 REQUEST_URI => '/foo',
142 };
143 my $req = Dancer2::Core::Request->new( env => $env );
144 is( $req->path, '/', 'path corrent when empty PATH_INFO' );
145 is( $req->uri_base, 'http://localhost:5000/foo',
146 'uri_base correct when empty PATH_INFO'
147 );
148
149 # SCRIPT_NAME not set
150 $env = {
151 %$base,
152 SCRIPT_NAME => '',
153 PATH_INFO => '/foo',
154 REQUEST_URI => '/foo',
155 };
156 $req = Dancer2::Core::Request->new( env => $env );
157 is( $req->path, '/foo', 'path corrent when empty SCRIPT_NAME' );
158 is( $req->uri_base, 'http://localhost:5000',
159 'uri_base handles empty SCRIPT_NAME'
160 );
161
162 # Both SCRIPT_NAME and PATH_INFO set
163 # PSGI spec does not allow SCRIPT_NAME='/', PATH_INFO='/some/path'
164 $env = {
165 %$base,
166 SCRIPT_NAME => '/foo',
167 PATH_INFO => '/bar/baz/',
168 REQUEST_URI => '/foo/bar/baz/',
169 };
170 $req = Dancer2::Core::Request->new( env => $env );
171 is( $req->path, '/bar/baz/',
172 'path corrent when both PATH_INFO and SCRIPT_NAME set'
173 );
174 is( $req->uri_base, 'http://localhost:5000/foo',
175 'uri_base correct when both PATH_INFO and SCRIPT_NAME set',
176 );
177
178 # Neither SCRIPT_NAME or PATH_INFO set
179 $env = {
180 %$base,
181 SCRIPT_NAME => '',
182 PATH_INFO => '',
183 REQUEST_URI => '/foo/',
184 };
185 $req = Dancer2::Core::Request->new( env => $env );
186 is( $req->path, '/',
187 'path corrent when calculated from REQUEST_URI'
188 );
189 is( $req->uri_base, 'http://localhost:5000',
190 'uri_base correct when calculated from REQUEST_URI',
191 );
192 }
193
194 note "testing forward";
195 $env = {
196 'REQUEST_METHOD' => 'GET',
197 'REQUEST_URI' => '/',
198 'PATH_INFO' => '/',
199 'QUERY_STRING' => 'foo=bar&number=42',
200 };
201
202 $req = Dancer2::Core::Request->new( env => $env );
203 is $req->path, '/', 'path is /';
204 is $req->method, 'GET', 'method is get';
205 is_deeply scalar( $req->params ), { foo => 'bar', number => 42 },
206 'params are parsed';
207
208 $req = Dancer2::Core::App->new( request => $req )
209 ->make_forward_to('/new/path');
210 is $req->path, '/new/path', 'path is changed';
211 is $req->method, 'GET', 'method is unchanged';
212 is_deeply scalar( $req->params ), { foo => 'bar', number => 42 },
213 'params are not touched';
214
215 $req = Dancer2::Core::App->new( request => $req )
216 ->make_forward_to(
217 '/new/path',
218 undef,
219 { method => 'POST' },
220 );
221
222 is $req->path, '/new/path', 'path is changed';
223
224 is $req->method, 'POST', 'method is changed';
225 is_deeply scalar( $req->params ), { foo => 'bar', number => 42 },
226 'params are not touched';
227
228 note "testing unicode params";
229 $env = {
230 'REQUEST_METHOD' => 'GET',
231 'REQUEST_URI' => '/',
232 'PATH_INFO' => '/',
233 'QUERY_STRING' => "M%C3%BCller=L%C3%BCdenscheid",
234 };
235 $req = Dancer2::Core::Request->new( env => $env );
236 is_deeply scalar( $req->params ), { "M\N{U+00FC}ller", "L\N{U+00FC}denscheid" },
237 'multi byte unicode chars work in param keys and values';
238 {
239 note "testing private _decode not to mangle hash";
240 my @warnings;
241 local $SIG{__WARN__} = sub {
242 push @warnings, @_;
243 };
244
245 my $h = { zzz => undef, };
246 for ( 'aaa' .. 'fff' ) {
247 $h->{$_} = $_;
248 }
249
250 my $i = Dancer2::Core::Request->_decode($h);
251 is_deeply( $i, $h, 'hash not mangled' );
252 ok( !@warnings, 'no warnings were issued' );
253 }
254 }
255
256 note "Run test with XS_URL_DECODE" if $Dancer2::Core::Request::XS_URL_DECODE;
257 note "Run test with XS_PARSE_QUERY_STRING"
258 if $Dancer2::Core::Request::XS_PARSE_QUERY_STRING;
259 run_test();
260 if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) {
261 note "Run test without XS_PARSE_QUERY_STRING";
262 $Dancer2::Core::Request::XS_PARSE_QUERY_STRING = 0;
263 $Dancer2::Core::Request::_id = 0;
264 run_test();
265 }
266 if ($Dancer2::Core::Request::XS_URL_DECODE) {
267 note "Run test without XS_URL_DECODE";
268 $Dancer2::Core::Request::XS_URL_DECODE = 0;
269 $Dancer2::Core::Request::_id = 0;
270 run_test();
271 }
272
273 done_testing;