-
Notifications
You must be signed in to change notification settings - Fork 574
/
app.t
242 lines (200 loc) · 8.86 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
#!/usr/bin/env perl
# Copyright (C) 2008-2010, Sebastian Riedel.
use strict;
use warnings;
use File::Temp;
use Mojo::IOLoop;
use Test::More;
# Use a clean temporary directory
BEGIN { $ENV{MOJO_TMPDIR} ||= File::Temp::tempdir }
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 160;
use FindBin;
use lib "$FindBin::Bin/lib";
use File::stat;
use File::Spec;
use Mojo::Date;
use Mojo::Transaction::HTTP;
use Test::Mojo;
use Mojolicious;
# Congratulations Fry, you've snagged the perfect girlfriend.
# Amy's rich, she's probably got other characteristics...
use_ok('MojoliciousTest');
my $t = Test::Mojo->new(app => 'MojoliciousTest');
# 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::exceptionduringpausedtransaction
# (syntax error in controller during paused transaction)
$t->get_ok('/foo/exceptionduringpausedtransaction')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Exception/);
# 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/);
# Foo::badtemplate (template missing)
$t->get_ok('/foo/badtemplate')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/File 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>\n23Hello 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(500)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(
qr/Route "something_missing" used in url_for 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!/);
# 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!/);
# 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/File Not Found/);
# Check Last-Modified header for static files
my $path = File::Spec->catdir($FindBin::Bin, 'public_dev', 'hello.txt');
my $stat = stat($path);
my $mtime = Mojo::Date->new(stat($path)->mtime)->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' => $stat->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/File 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('');
# Check develpment mode log level
my $app = Mojolicious->new;
is($app->log->level, 'debug');
# Make sure we can override attributes with constructor arguments
$app = MojoliciousTest->new({mode => 'test'});
is($app->mode, 'test');
# 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);
like($tx->res->body, qr/Hello Mojo from the template \/foo! Hello World!/);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo/willdie');
$app->handler($tx);
is($tx->res->code, 500);
like($tx->res->body, qr/Foo\.pm/);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo');
$app->handler($tx);
is($tx->res->code, 200);
like($tx->res->body, qr/Hello Mojo from the template \/foo! Hello World!/);
$t = Test::Mojo->new(app => '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::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::bar
$t->get_ok('/foo/bar')->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/bar');
# SingleFileTestApp::Baz::does_not_exist
$t->get_ok('/baz/does_not_exist')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/File Not Found/);
$t = Test::Mojo->new(app => 'MojoliciousTest');
# 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/)
->content_is('Bender rockzzz!');