/
app.t
401 lines (336 loc) · 16.3 KB
/
app.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use Mojo::Base -strict;
# Disable Bonjour, IPv6 and libev
BEGIN {
$ENV{MOJO_MODE} = 'development';
$ENV{MOJO_NO_BONJOUR} = $ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More tests => 315;
use FindBin;
use lib "$FindBin::Bin/lib";
use File::Spec::Functions 'catdir';
use Mojo::Date;
use Mojo::Transaction::HTTP;
use Mojolicious;
use Test::Mojo;
# "Congratulations Fry, you've snagged the perfect girlfriend.
# Amy's rich, she's probably got other characteristics..."
my $t = Test::Mojo->new('MojoliciousTest');
# Application is already available
is $t->app->routes->find('something')->to_string, '/test4/:something',
'right pattern';
is $t->app->routes->find('test3')->pattern->defaults->{namespace},
'MojoliciousTestController', 'right namespace';
is $t->app->routes->find('authenticated')->pattern->defaults->{controller},
'foo', 'right controller';
is ref $t->app->routes->find('something'), 'Mojolicious::Routes::Route',
'right class';
is ref $t->app->routes->find('something')->root, 'Mojolicious::Routes',
'right class';
is $t->app->sessions->cookie_domain, '.example.com', 'right domain';
is $t->app->sessions->cookie_path, '/bar', 'right path';
is $t->app->commands->get_data('some/static/file.txt', 'MojoliciousTest'),
"Production static file with low precedence.\n\n", 'right result';
# Plugin::Test::SomePlugin2::register (security violation)
$t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
->status_is(404)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_unlike(qr/Something/)->content_like(qr/Page not found/);
# Plugin::Test::SomePlugin2::register (security violation again)
$t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
->status_is(404)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_unlike(qr/Something/)->content_like(qr/Page not found/);
# Foo::fun
my $url = $t->ua->app_url;
$url->path('/fun/time');
$t->get_ok($url, {'X-Test' => 'Hi there!'})->status_isnt(404)->status_is(200)
->header_isnt('X-Bender' => 'Bite my shiny metal ass!')
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_isnt('Have')
->content_is('Have fun!');
# Foo::baz (missing action without template)
$t->get_ok('/foo/baz')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_unlike(qr/Something/)->content_like(qr/Page not found/);
# Foo::yada (action-less template)
$t->get_ok('/foo/yada')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/look ma! no action!/);
# SyntaxError::foo (syntax error in controller)
$t->get_ok('/syntax_error/foo')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Missing right curly/);
# Foo::syntaxerror (syntax error in template)
$t->get_ok('/foo/syntaxerror')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/^Missing right curly/);
# Exceptional::this_one_dies (action dies)
$t->get_ok('/exceptional/this_one_dies')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is("doh!\n\n");
# Exceptional::this_one_might_die (bridge dies)
$t->get_ok('/exceptional_too/this_one_dies')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("double doh!\n\n");
# Exceptional::this_one_dies (action behind bridge dies)
$t->get_ok('/exceptional_too/this_one_dies', {'X-DoNotDie' => 1})
->status_is(500)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is("doh!\n\n");
# Exceptional::this_one_does_not_exist (action does not exist)
$t->get_ok('/exceptional/this_one_does_not_exist')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
# Exceptional::this_one_does_not_exist (action behind bridge does not exist)
$t->get_ok('/exceptional_too/this_one_does_not_exist', {'X-DoNotDie' => 1})
->status_is(404)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
# Foo::fun
$t->get_ok('/fun/time', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Have fun!');
# Foo::fun
$url = $t->ua->app_url;
$url->path('/fun/time');
$t->get_ok($url, {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Have fun!');
# Foo::fun
$t->get_ok('/happy/fun/time', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Have fun!');
# Foo::authenticated (authentication bridge)
$t->get_ok('/auth/authenticated', {'X-Bender' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('authenticated');
# Foo::authenticated (authentication bridge)
$t->get_ok('/auth/authenticated')->status_is(404)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
# Foo::test
$t->get_ok('/foo/test', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr#/bar/test#);
# Foo::index
$t->get_ok('/foo', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr#<body>\s+23\nHello Mojo from the template /foo! He#);
# Foo::Bar::index
$t->get_ok('/foo-bar', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr#Hello Mojo from the other template /foo-bar!#);
# Foo::something
$t->get_ok('/somethingtest', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('/test4/42');
# Foo::url_for_missing
$t->get_ok('/something_missing', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('does_not_exist');
# Foo::templateless
$t->get_ok('/foo/templateless', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Hello Mojo from a templateless renderer!/);
# Foo::withlayout
$t->get_ok('/foo/withlayout', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Same old in green Seems to work!/);
# Foo::withblock
$t->get_ok('/foo/withblock.txt', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_type_isnt('text/html')->content_type_is('text/plain')
->content_like(qr/Hello Baerbel\.\s+Hello Wolfgang\./);
# MojoliciousTest2::Foo::test
$t->get_ok('/test2', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr#/test2#);
# MojoliciousTestController::index
$t->get_ok('/test3', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/No class works!/);
# MojoliciousTestController::index (only namespace)
$t->get_ok('/test5', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('/test5');
# MojoliciousTestController::index (no namespace)
$t->get_ok('/test6', {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('/test6');
# 404
$t->get_ok('/', {'X-Test' => 'Hi there!'})->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
# Check Last-Modified header for static files
my $path = catdir($FindBin::Bin, 'public_dev', 'hello.txt');
my $size = (stat $path)[7];
my $mtime = Mojo::Date->new((stat $path)[9])->to_string;
# Static file /hello.txt
$t->get_ok('/hello.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Last-Modified' => $mtime)->header_is('Content-Length' => $size)
->content_type_is('text/plain')
->content_like(qr/Hello Mojo from a development static file!/);
# Try to access a file which is not under the web root via path
# traversal
$t->get_ok('/../../mojolicious/secret.txt')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
# Check If-Modified-Since
$t->get_ok('/hello.txt', {'If-Modified-Since' => $mtime})->status_is(304)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('');
# Embedded development static file
$t->get_ok('/some/static/file.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("Development static file with high precedence.\n");
# Embedded development template
$t->get_ok('/just/some/template')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("Development template with high precedence.\n");
# Check develpment mode log level
my $app = Mojolicious->new;
is $app->log->level, 'debug', 'right log level';
# Make sure we can override attributes with constructor arguments
$app = MojoliciousTest->new({mode => 'test'});
is $app->mode, 'test', 'right mode';
# Persistent error
$app = MojoliciousTest->new;
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo');
$app->handler($tx);
is $tx->res->code, 200, 'right status';
like $tx->res->body, qr#Hello Mojo from the template /foo! Hello World!#,
'right content';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo/willdie');
$app->handler($tx);
is $tx->res->code, 500, 'right status';
like $tx->res->body, qr/Foo\.pm/, 'right content';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo');
$app->handler($tx);
is $tx->res->code, 200, 'right status';
like $tx->res->body, qr#Hello Mojo from the template /foo! Hello World!#,
'right content';
$t = Test::Mojo->new('SingleFileTestApp');
# SingleFileTestApp::Foo::index
$t->get_ok('/foo')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Same old in green Seems to work!/);
# SingleFileTestApp (helper)
$t->get_ok('/helper')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Welcome aboard!');
# SingleFileTestApp::Foo::data_template
$t->get_ok('/foo/data_template')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("23 works!\n");
# SingleFileTestApp::Foo::data_template
$t->get_ok('/foo/data_template2')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("This one works too!\n");
# SingleFileTestApp::Foo::data_static
$t->get_ok('/foo/data_static')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is("And this one... ALL GLORY TO THE HYPNOTOAD!\n");
# SingleFileTestApp::Foo::routes
$t->get_ok('/foo/routes')->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('/foo/routes');
# SingleFileTestApp::Redispatch::handler
$t->get_ok('/redispatch')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Redispatch!');
# SingleFileTestApp::Redispatch::render
$t->get_ok('/redispatch/render')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Render!');
# SingleFileTestApp::Redispatch::handler (targeting an existing method)
$t->get_ok('/redispatch/secret')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Redispatch!');
# SingleFileTestApp::Redispatch::secret
$t->get_ok('/redispatch/secret?rly=1')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Secret!');
$t = Test::Mojo->new('MojoliciousTest');
# MojoliciousTestController::Foo::plugin_upper_case
$t->get_ok('/plugin/upper_case')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('WELCOME aboard!');
# MojoliciousTestController::Foo::plugin_camel_case
$t->get_ok('/plugin/camel_case')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Welcome aboard!');
# MojoliciousTestController::Foo::stage2
$t->get_ok('/staged', {'X-Pass' => '1'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Welcome aboard!');
# MojoliciousTestController::Foo::stage1
$t->get_ok('/staged')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('Go away!');
# MojoliciousTest::Foo::config
$t->get_ok('/stash_config')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('123');
# Shortcuts to controller#action
$t->get_ok('/shortcut/ctrl-act')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('ctrl-act');
$t->get_ok('/shortcut/ctrl')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('ctrl');
$t->get_ok('/shortcut/act')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('act');
# Session with domain
$t->get_ok('/foo/session')->status_is(200)
->header_like('Set-Cookie' => qr/; Domain=\.example\.com/)
->header_like('Set-Cookie' => qr|; Path=/bar|)
->content_is('Bender rockzzz!');
# Mixed formats
$t->get_ok('/rss.xml')->status_is(200)->content_type_is('application/rss+xml')
->content_like(qr#<\?xml version="1.0" encoding="UTF-8"\?><rss />#);