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
|
use Mojo::Base -strict;
use utf8;
# Disable IPv6 and libev
BEGIN {
$ENV{MOJO_MODE} = 'testing';
$ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More tests => 139;
use FindBin;
use lib "$FindBin::Bin/lib";
use Mojolicious::Lite;
use Test::Mojo;
package TestApp;
use Mojolicious::Lite;
# GET /hello (embedded)
get '/hello' => sub {
my $self = shift;
my $name = $self->stash('name');
my $counter = ++$self->session->{counter};
$self->render_text("Hello from the $name ($counter) app!");
};
# "Morbo will now introduce the candidates - Puny Human Number One,
# Puny Human Number Two, and Morbo's good friend Richard Nixon.
# How's the family, Morbo?
# Belligerent and numerous."
package MyTestApp::Test1;
use Mojolicious::Lite;
# GET /yada (embedded)
get '/yada' => sub {
my $self = shift;
my $name = $self->stash('name');
$self->render(text => "yada $name works!");
};
# GET /bye (embedded)
get '/bye' => sub {
my $self = shift;
my $name = $self->stash('name');
my $nb = '';
$self->render_later;
$self->ua->app(main::app())->get(
'/hello/hello' => sub {
my $tx = pop;
$self->render_text($tx->res->body . "$name! $nb");
}
);
$nb .= 'success!';
};
package MyTestApp::Test2;
use Mojolicious::Lite;
# GET / (embedded)
get '/' => sub {
my $self = shift;
my $name = $self->param('name');
my $url = $self->url_for;
$self->render_text("Bye from the $name app! $url!");
};
package MyTestApp::Basic;
use Mojo::Base 'Mojo';
sub handler {
my ($self, $c) = @_;
$c->res->code(200);
my $test = $c->param('test');
$c->res->body("Hello $test!");
$c->rendered;
}
package main;
# /foo/* (plugin app)
plugin 'PluginWithEmbeddedApp';
app->routes->namespace('MyTestApp');
# Mount full external application a few times
my $external = "$FindBin::Bin/external/myapp.pl";
plugin Mount => {'/x/1' => $external};
plugin(Mount => ('/x/♥' => $external))->to(message => 'works 2!');
plugin Mount => {'/y/1' => "$FindBin::Bin/external/myapp2.pl"};
plugin Mount => {'mojolicious.org' => $external};
plugin(Mount => ('/y/♥' => "$FindBin::Bin/external/myapp2.pl"))
->to(message => 'works 3!');
plugin Mount => {'MOJOLICIO.US/' => $external};
plugin Mount => {'*.kraih.com' => $external};
plugin(Mount => ('*.foo-bar.de/♥/123' => $external))
->to(message => 'works 3!');
# GET /hello
get '/hello' => 'works';
# GET /bye/* (dispatch to embedded app)
get('/bye' => {name => 'second embedded'})->detour('MyTestApp::Test1');
# GET /bar/* (dispatch to embedded app)
get('/bar' => {name => 'third embedded'})->detour(app => 'MyTestApp::Test1');
# GET /baz/* (dispatch to embedded app)
get('/baz')->detour('test1#', name => 'fourth embedded');
# GET /yada (dispatch to embedded app)
get('/yada')->to('test1#', name => 'fifth embedded');
# GET /yada/yada/yada (dispatch to embedded app)
get('/yada/yada/yada')
->to('test1#', path => '/yada', name => 'sixth embedded');
# GET /basic (dispatch to embedded app)
get('/basic')->detour(MyTestApp::Basic->new, test => 'lalala');
# GET /third/* (dispatch to embedded app)
get '/third/*path' =>
{app => 'MyTestApp::Test2', name => 'third embedded', path => '/'};
# GET /hello/* (dispatch to embedded app)
app->routes->route('/hello')->detour(TestApp::app())->to(name => 'embedded');
# GET /just/* (external embedded app)
get('/just' => {name => 'working'})->detour('EmbeddedTestApp');
get '/host' => {text => 'main application!'};
my $t = Test::Mojo->new;
# GET /plugin/foo (plugin app)
$t->get_ok('/plugin/foo')->status_is(200)->content_is('plugin works!');
# GET /hello (from main app)
$t->get_ok('/hello')->status_is(200)->content_is("Hello from the main app!\n");
# GET /hello/hello (from embedded app)
$t->get_ok('/hello/hello')->status_is(200)
->content_is('Hello from the embedded (1) app!');
# GET /hello/hello (from embedded app again)
$t->get_ok('/hello/hello')->status_is(200)
->content_is('Hello from the embedded (2) app!');
# GET /hello/hello (from embedded app again)
$t->get_ok('/hello/hello')->status_is(200)
->content_is('Hello from the embedded (3) app!');
# GET /bye/bye (from embedded app)
$t->get_ok('/bye/bye')->status_is(200)
->content_is('Hello from the embedded (1) app!second embedded! success!');
# GET /bar/bye (from embedded app)
$t->get_ok('/bar/bye')->status_is(200)
->content_is('Hello from the embedded (2) app!third embedded! success!');
# GET /baz/bye (from embedded app)
$t->get_ok('/baz/bye')->status_is(200)
->content_is('Hello from the embedded (3) app!fourth embedded! success!');
# GET /yada (from embedded app)
$t->get_ok('/yada')->status_is(200)->content_is('yada fifth embedded works!');
# GET /yada/yada (404 from embedded app)
$t->get_ok('/yada/yada')->status_is(404);
# GET /yada/yada/yada (from embedded app)
$t->get_ok('/yada/yada/yada')->status_is(200)
->content_is('yada sixth embedded works!');
# GET /basic (from embedded app)
$t->get_ok('/basic')->status_is(200)->content_is('Hello lalala!');
# GET /third/ (from embedded app)
$t->get_ok('/third')->status_is(200)
->content_is('Bye from the third embedded app! /third!');
# GET /just/works (from external embedded app)
$t->get_ok('/just/works')->status_is(200)->content_is("It is working!\n");
# GET /just/works/too (from external embedded app)
$t->get_ok('/just/works/too')->status_is(200)->content_is("It just works!\n");
# GET /x/1/ (full external application)
$t->get_ok('/x/1/')->status_is(200)->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/x/1/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /x/1/index.html (full external application)
$t->get_ok('/x/1/index.html')->status_is(200)
->content_is("External static file!\n");
# GET /x/1/echo (full external application)
$t->get_ok('/x/1/echo')->status_is(200)->content_is('echo: nothing!');
# GET /x/1/stream (full external application)
$t->get_ok('/x/1/stream')->status_is(200)->content_is('hello!');
# GET /x/1/url/☃ (full external application)
$t->get_ok('/x/1/url/☃')->status_is(200)
->content_is('/x/1/url/%E2%98%83 -> /x/1/%E2%98%83/stream!');
# GET /x/♥/ (full external application)
$t->get_ok('/x/♥/')->status_is(200)->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/x/%E2%99%A5/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /x/♥/index.html (full external application)
$t->get_ok('/x/♥/index.html')->status_is(200)
->content_is("External static file!\n");
# GET /x/♥/echo (full external application)
$t->get_ok('/x/♥/echo')->status_is(200)->content_is('echo: works 2!');
# GET /x/♥/stream (full external application)
$t->get_ok('/x/♥/stream')->status_is(200)->content_is('hello!');
# GET /x/♥/url/☃ (full external application)
$t->get_ok('/x/♥/url/☃')->status_is(200)
->content_is('/x/%E2%99%A5/url/%E2%98%83 -> /x/%E2%99%A5/%E2%98%83/stream!');
# GET /y/1 (full secondary external application)
$t->get_ok('/y/1')->status_is(200)->content_is("works 4!\nInsecure too!");
# GET /y/♥ (full secondary external application)
$t->get_ok('/y/♥')->status_is(200)->content_is("works 3!\nInsecure too!");
# GET /host (main application)
$t->get_ok('/host')->status_is(200)->content_is('main application!');
# GET / (full external application with domain)
$t->get_ok('/' => {Host => 'mojolicious.org'})->status_is(200)
->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /host (full external application with domain)
$t->get_ok('/host' => {Host => 'mojolicious.org'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('mojolicious.org');
# GET / (full external application with domain)
$t->get_ok('/' => {Host => 'mojolicio.us'})->status_is(200)
->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /host (full external application with domain)
$t->get_ok('/host' => {Host => 'mojolicio.us'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('mojolicio.us');
# GET / (full external application with domain)
$t->get_ok('/' => {Host => 'kraih.com'})->status_is(200)->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /host (full external application with domain)
$t->get_ok('/host' => {Host => 'KRaIH.CoM'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('kraih.com');
# GET /host (full external application with wildcard domain)
$t->get_ok('/host' => {Host => 'www.kraih.com'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('www.kraih.com');
# GET /host (full external application with wildcard domain)
$t->get_ok('/host' => {Host => 'foo.bar.kraih.com'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('foo.bar.kraih.com');
# GET /♥/123/ (full external application with a bit of everything)
$t->get_ok('/♥/123/' => {Host => 'foo-bar.de'})->status_is(200)
->content_is(<<'EOF');
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
<form action="/%E2%99%A5/123/%E2%98%83">
<input type="submit" value="☃" />
</form>
EOF
# GET /♥/123/host (full external application with a bit of everything)
$t->get_ok('/♥/123/host' => {Host => 'foo-bar.de'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('foo-bar.de');
# GET /♥/123/echo (full external application with a bit of everything)
$t->get_ok('/♥/123/echo' => {Host => 'foo-bar.de'})->status_is(200)
->content_is('echo: works 3!');
# GET /♥/123/host (full external application with a bit of everything)
$t->get_ok('/♥/123/host' => {Host => 'www.foo-bar.de'})->status_is(200)
->header_is('X-Message' => 'it works!')->content_is('www.foo-bar.de');
# GET /♥/123/echo (full external application with a bit of everything)
$t->get_ok('/♥/123/echo' => {Host => 'www.foo-bar.de'})->status_is(200)
->content_is('echo: works 3!');
# GET /♥/123/one (full external application with a bit of everything)
$t->get_ok('/♥/123/one' => {Host => 'www.foo-bar.de'})->status_is(200)
->content_is('One');
# GET /♥/123/one/two (full external application with a bit of everything)
$t->get_ok('/♥/123/one/two' => {Host => 'www.foo-bar.de'})->status_is(200)
->content_is('Two');
# GET /host (full external application with bad domain)
$t->get_ok('/' => {Host => 'mojoliciousxorg'})->status_is(404);
# GET /host (full external application with bad wildcard domain)
$t->get_ok('/' => {Host => 'www.kraihxcom'})->status_is(404);
__DATA__
@@ works.html.ep
Hello from the main app!
|