Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more modern dispatcher tests
  • Loading branch information
kraih committed Feb 25, 2015
1 parent cc883b8 commit 4782364
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 56 deletions.
9 changes: 3 additions & 6 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -4,18 +4,15 @@ use Mojo::Base -base;
# No imports, for security reasons!
use Carp ();
use Mojo::ByteStream;
use Mojo::Transaction::HTTP;
use Mojo::URL;
use Mojo::Util;
use Mojolicious;
use Mojolicious::Routes::Match;
use Scalar::Util ();
use Time::HiRes ();

has app => sub { Mojolicious->new };
has [qw(app tx)];
has match =>
sub { Mojolicious::Routes::Match->new(root => shift->app->routes) };
has tx => sub { Mojo::Transaction::HTTP->new };

# Reserved stash values
my %RESERVED = map { $_ => 1 } (
Expand Down Expand Up @@ -402,8 +399,8 @@ implements the following new ones.
my $app = $c->app;
$c = $c->app(Mojolicious->new);
A reference back to the application that dispatched to this controller,
defaults to a L<Mojolicious> object.
A reference back to the application that dispatched to this controller, usually
a L<Mojolicious> object.
# Use application logger
$c->app->log->debug('Hello Mojo');
Expand Down
79 changes: 34 additions & 45 deletions t/mojolicious/dispatch.t
Expand Up @@ -19,12 +19,12 @@ package main;
use Mojo::Base -strict;

use Test::More;
use Mojo::Transaction::HTTP;
use Mojo::Upload;
use Mojolicious::Controller;
use Mojolicious;

# Fresh controller
my $c = Mojolicious::Controller->new;
my $app = Mojolicious->new(secrets => ['works']);
my $c = $app->build_controller;
is $c->url_for('/'), '/', 'routes are working';

# Set
Expand All @@ -33,7 +33,8 @@ is $c->stash('foo'), 'bar', 'set and return a stash value';

# Ref value
my $stash = $c->stash;
is_deeply $stash, {foo => 'bar'}, 'return a hash reference';
is ref $stash, 'HASH', 'return a hash reference';
is $stash->{foo}, 'bar', 'right value';

# Replace
$c->stash(foo => 'baz');
Expand All @@ -56,15 +57,18 @@ $stash = $c->stash;
delete $stash->{foo};
delete $stash->{0};
delete $stash->{zero};
is_deeply $stash, {}, 'elements can be deleted';
is $c->stash->{foo}, undef, 'element has been deleted';
is $c->stash->{0}, undef, 'element has been deleted';
is $c->stash->{zero}, undef, 'element has been deleted';
$c->stash('foo' => 'zoo');
delete $c->stash->{foo};
is_deeply $c->stash, {}, 'elements can be deleted';
is $c->stash->{foo}, undef, 'element has been deleted';

# Set via hash
$c->stash({a => 1, b => 2});
$stash = $c->stash;
is_deeply $stash, {a => 1, b => 2}, 'set via hash reference';
is $c->stash->{a}, 1, 'right value';
is $c->stash->{b}, 2, 'right value';

# Override captures
is $c->param('foo'), undef, 'no value';
Expand All @@ -80,7 +84,7 @@ is $c->param(foo => ['ba;r', 'baz'])->param('foo'), 'baz', 'right value';
is_deeply $c->every_param('foo'), ['ba;r', 'baz'], 'right values';

# Reserved stash values are hidden
$c = Mojolicious::Controller->new;
$c = $app->build_controller;
is $c->param(action => 'test')->param('action'), undef, 'value is reserved';
is $c->param(app => 'test')->param('app'), undef, 'value is reserved';
is $c->param(cb => 'test')->param('cb'), undef, 'value is reserved';
Expand All @@ -102,19 +106,17 @@ is $c->param(text => 'test')->param('text'), undef, 'value is reserved';
is $c->param(variant => 'test')->param('variant'), undef, 'value is reserved';

# Controller with application and routes
$c = Test::Controller->new;
$c = $app->controller_class('Test::Controller')->build_controller;
my $d = $c->app->routes;
ok $d, 'initialized';
$d->namespaces(['Test']);
$d->route('/')->over([])->to(controller => 'foo', action => 'home');
$d->route('/foo/(capture)')->to(controller => 'foo', action => 'bar');

# Cache
$c = Test::Controller->new;
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/');
ok $d->dispatch($c), 'dispatched';
is $c->stash->{controller}, 'foo', 'right value';
is $c->stash->{action}, 'home', 'right value';
Expand All @@ -123,22 +125,18 @@ is $c->match->stack->[0]{action}, 'home', 'right value';
ok $c->render_called, 'rendered';
my $cache = $d->cache->get('GET:/:0');
ok $cache, 'route has been cached';
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/');
$d->match($c);
is $c->stash->{controller}, undef, 'no value';
is $c->stash->{action}, undef, 'no value';
is $c->match->stack->[0]{controller}, 'foo', 'right value';
is $c->match->stack->[0]{action}, 'home', 'right value';
ok !$c->render_called, 'not rendered';
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/');
ok $d->dispatch($c), 'dispatched';
is $c->stash->{controller}, 'foo', 'right value';
is $c->stash->{action}, 'home', 'right value';
Expand All @@ -148,21 +146,16 @@ ok $c->render_called, 'rendered';
is_deeply $d->cache->get('GET:/:0'), $cache, 'cached route has been reused';

# 404 clean stash
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/not_found');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/not_found');
ok !$d->dispatch($c), 'not dispatched';
is_deeply $c->stash, {}, 'empty stash';
ok !$c->render_called, 'nothing rendered';

# No escaping
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/foo/hello');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('POST');
$c->req->url->parse('/foo/hello');
$c->stash(test => 23);
ok $d->dispatch($c), 'dispatched';
is $c->stash->{controller}, 'foo', 'right value';
Expand Down Expand Up @@ -200,11 +193,9 @@ is $c->param('bar'), 'baz', 'right value';
ok $c->render_called, 'rendered';

# Escaping
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo/hello%20there');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/foo/hello%20there');
ok $d->dispatch($c), 'dispatched';
is $c->stash->{controller}, 'foo', 'right value';
is $c->stash->{action}, 'bar', 'right value';
Expand All @@ -215,11 +206,9 @@ is $c->param('capture'), 'hello there', 'right value';
ok $c->render_called, 'rendered';

# Escaping UTF-8
$c = Test::Controller->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82');
$c->tx($tx);
$c = $app->build_controller;
$c->req->method('GET');
$c->req->url->parse('/foo/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82');
ok $d->dispatch($c), 'dispatched';
is $c->stash->{controller}, 'foo', 'right value';
is $c->stash->{action}, 'bar', 'right value';
Expand Down
12 changes: 7 additions & 5 deletions t/mojolicious/renderer.t
Expand Up @@ -2,10 +2,11 @@ use Mojo::Base -strict;

use Test::More;
use Mojo::Util 'decode';
use Mojolicious::Controller;
use Mojolicious;

# Partial rendering
my $c = Mojolicious::Controller->new;
my $app = Mojolicious->new(secrets => ['works']);
my $c = $app->build_controller;
$c->app->log->level('fatal');
is $c->render_to_string(text => 'works'), 'works', 'renderer is working';

Expand Down Expand Up @@ -63,7 +64,7 @@ like $log, qr/Cookie "foo" is bigger than 4096 bytes/, 'right message';
$c->app->log->unsubscribe(message => $cb);

# Nested helpers
my $first = Mojolicious::Controller->new;
my $first = $app->build_controller;
$first->helpers->app->log->level('fatal');
$first->app->helper('myapp.multi_level.test' => sub {'works!'});
ok $first->app->renderer->get_helper('myapp'), 'found helper';
Expand All @@ -79,7 +80,8 @@ $first->myapp->defaults(foo => 'bar');
is $first->myapp->defaults('foo'), 'bar', 'right result';
is $first->helpers->myapp->defaults('foo'), 'bar', 'right result';
is $first->app->myapp->defaults('foo'), 'bar', 'right result';
my $second = Mojolicious::Controller->new;
my $app2 = Mojolicious->new(secrets => ['works']);
my $second = $app2->build_controller;
$second->app->log->level('fatal');
is $second->app->renderer->get_helper('myapp'), undef, 'no helper';
is $second->app->renderer->get_helper('myapp.defaults'), undef, 'no helper';
Expand Down Expand Up @@ -114,7 +116,7 @@ my $template_class = decode 'UTF-8',
is decode('UTF-8', $second->render_to_string(inline => "<%= __PACKAGE__ =%>")),
$template_class, 'same class';
ok $template_class->can('stash'), 'helpers are active';
undef $second;
undef $app2;
ok !$helper_class->can('defaults'), 'helpers have been cleaned up';
ok !$template_class->can('stash'), 'helpers have been cleaned up';

Expand Down

0 comments on commit 4782364

Please sign in to comment.