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
|
#!/usr/bin/env perl
# Copyright (C) 2008-2010, Sebastian Riedel.
package Test::Foo;
use strict;
use warnings;
use base 'MojoX::Dispatcher::Routes::Controller';
sub bar {1}
sub home {1}
package Test::Controller;
use strict;
use warnings;
use base 'MojoX::Dispatcher::Routes::Controller';
__PACKAGE__->attr('render_called');
sub render { shift->render_called(1) }
sub reset_state {
my $self = shift;
$self->render_called(0);
my $stash = $self->stash;
delete $stash->{$_} for keys %$stash;
}
# I was all of history's greatest acting robots -- Acting Unit 0.8,
# Thespomat, David Duchovny!
package main;
use strict;
use warnings;
use utf8;
use Test::More tests => 31;
use Mojo;
use Mojo::Transaction::HTTP;
use MojoX::Dispatcher::Routes;
my $c = Test::Controller->new(app => Mojo->new);
# Silence
$c->app->log->path(undef);
$c->app->log->level('error');
my $d = MojoX::Dispatcher::Routes->new;
ok($d);
$d->namespace('Test');
$d->route('/')->to(controller => 'foo', action => 'home');
$d->route('/foo/(capture)')->to(controller => 'foo', action => 'bar');
# 404 clean stash
$c->reset_state;
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/not_found');
$c->tx($tx);
is($d->dispatch($c), 1);
is_deeply($c->stash, {});
ok(!$c->render_called);
# No escaping
$c->reset_state;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('POST');
$tx->req->url->parse('/foo/hello');
$c->tx($tx);
is($d->dispatch($c), '');
is($c->stash->{controller}, 'foo');
is($c->stash->{action}, 'bar');
is($c->stash->{capture}, 'hello');
is(ref $c->stash->{params}, 'Mojo::Parameters');
is($c->param('controller'), 'foo');
is($c->param('action'), 'bar');
is($c->param('capture'), 'hello');
ok($c->render_called);
# Escaping
$c->reset_state;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('/foo/hello%20there');
$c->tx($tx);
is($d->dispatch($c), '');
is($c->stash->{controller}, 'foo');
is($c->stash->{action}, 'bar');
is($c->stash->{capture}, 'hello there');
is(ref $c->stash->{params}, 'Mojo::Parameters');
is($c->param('controller'), 'foo');
is($c->param('action'), 'bar');
is($c->param('capture'), 'hello there');
ok($c->render_called);
# Escaping utf8
$c->reset_state;
$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);
is($d->dispatch($c), '');
is($c->stash->{controller}, 'foo');
is($c->stash->{action}, 'bar');
is($c->stash->{capture}, 'привет');
is(ref $c->stash->{params}, 'Mojo::Parameters');
is($c->param('controller'), 'foo');
is($c->param('action'), 'bar');
is($c->param('capture'), 'привет');
ok($c->render_called);
|