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
|
use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::URL;
use Mojolicious::Lite;
use Test::Mojo;
# Rebase hook
app->hook(
before_dispatch => sub {
my $c = shift;
$c->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
$c->req->url->path->leading_slash(0);
}
);
# Current route hook
app->hook(
after_dispatch => sub {
my $c = shift;
$c->res->headers->header('X-Route' => $c->current_route);
}
);
get '/' => 'root';
get '/foo';
get '/bar' => sub {
my $c = shift;
$c->flash(just => 'works!')->flash({works => 'too!'});
$c->redirect_to($c->url_for('foo'));
};
get '/baz' => sub { shift->render('root') };
my $t = Test::Mojo->new;
# Rebased root
$t->get_ok('/')->status_is(200)->header_is('X-Route' => 'root')
->content_is(<<'EOF');
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png">
<link href="//example.com/base.css" rel="stylesheet">
<a href="mailto:sri@example.com">Contact</a>
http://example.com/rebased/
http://example.com/rebased/foo
/rebased/foo
http://example.com/
root
Welcome to the root!
EOF
# Rebased route
$t->get_ok('/foo')->status_is(200)->header_is('X-Route' => 'foo')
->content_is(<<EOF);
http://example.com/rebased/
<link href="/rebased/b.css" media="test" rel="stylesheet">
<img alt="Test" src="/rebased/images/test.png">
http://example.com/rebased/foo
http://example.com/rebased
/rebased
http://example.com/
foo
EOF
# Rebased route with flash
ok !$t->ua->cookie_jar->find($t->ua->server->url->path('/foo'))->[0],
'no session cookie';
$t->get_ok('/bar')->status_is(302)->header_is('X-Route' => 'bar')
->header_is(Location => '/rebased/foo');
ok $t->ua->cookie_jar->find($t->ua->server->url->path('/foo'))->[0],
'session cookie';
# Rebased route with message from flash
$t->get_ok('/foo')->status_is(200)->content_is(<<EOF);
http://example.com/rebased/works!too!
<link href="/rebased/b.css" media="test" rel="stylesheet">
<img alt="Test" src="/rebased/images/test.png">
http://example.com/rebased/foo
http://example.com/rebased
/rebased
http://example.com/
foo
EOF
# Rebased route sharing a template
$t->get_ok('/baz')->status_is(200)->header_is('X-Route' => 'baz')
->content_is(<<'EOF');
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png">
<link href="//example.com/base.css" rel="stylesheet">
<a href="mailto:sri@example.com">Contact</a>
http://example.com/rebased/baz
http://example.com/rebased/foo
/rebased/foo
http://example.com/
baz
EOF
# Does not exist
$t->get_ok('/yada')->status_is(404)->header_is('X-Route' => '');
done_testing();
__DATA__
@@ root.html.ep
%= $c->req->url->base
%= javascript '/mojo/jquery/jquery.js'
%= image '/images/test.png'
%= stylesheet '//example.com/base.css'
%= link_to Contact => 'mailto:sri@example.com'
%= $c->req->url->to_abs
%= url_for('foo')->to_abs
%= url_for 'foo'
%= url_for('foo')->base
%= current_route
% if (current_route 'root') {
Welcome to the root!
% }
@@ foo.html.ep
<%= $c->req->url->base %><%= flash 'just' || '' %><%= flash 'works' || '' %>
%= stylesheet '/b.css', media => 'test'
%= image '/images/test.png', alt => 'Test'
%= $c->req->url->to_abs
%= url_for('root')->to_abs
%= url_for 'root'
%= url_for('root')->base
%= current_route
|