Skip to content

Commit

Permalink
fixed format detection bug in Mojolicious::Routes::Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 14, 2012
1 parent f21092d commit e34f356
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved all debug messages.
- Improved documentation.
- Improved tests.
- Fixed format detection bug in Mojolicious::Routes::Pattern.

2.80 2012-04-10
- Added support for alternative MIME types to Mojolicious::Types.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -100,9 +100,9 @@ sub shape_match {
# Format
$result->{format} ||= $self->{strict} if $detect && exists $self->{strict};
my $req = $self->reqs->{format};
return $result if defined $req && !$req;
return $result if !$detect || defined $req && !$req;
my $format = $self->format;
if ($detect && $$pathref =~ s|^/?$format||) { $result->{format} ||= $1 }
if ($$pathref =~ s|^/?$format||) { $result->{format} ||= $1 }
elsif ($req) { return if !$result->{format} }

return $result;
Expand Down
41 changes: 32 additions & 9 deletions t/mojolicious/routes.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 353;
use Test::More tests => 365;

# "They're not very heavy, but you don't hear me not complaining."
use Mojolicious::Routes;
Expand Down Expand Up @@ -184,10 +184,15 @@ $r->route('/regex/alternatives/:alternatives',
alternatives => qr/foo|bar|baz/)
->to(controller => 'regex', action => 'alternatives');

# /versioned/2.0/test
# /versioned/2.0/test.xml
$r->route('/versioned')->route('/2.0')->to(controller => 'foo')
->route('/test')->to(action => 'bar');
# /versioned/1.0/test
# /versioned/1.0/test.xml
# /versioned/2.4/test
# /versioned/2.4/test.xml
my $versioned = $r->route('/versioned');
$versioned->route('/1.0')->to(controller => 'bar')->route('/test')
->to(action => 'baz');
$versioned->route('/2.4')->to(controller => 'foo')->route('/test')
->to(action => 'bar');

# Make sure stash stays clean
my $m = Mojolicious::Routes::Match->new(GET => '/clean')->match($r);
Expand Down Expand Up @@ -710,19 +715,37 @@ $m =
is $m->stack->[0], undef, 'no value';

# Route with version
$m = Mojolicious::Routes::Match->new(GET => '/versioned/2.0/test')->match($r);
$m = Mojolicious::Routes::Match->new(GET => '/versioned/1.0/test')->match($r);
is $m->stack->[0]->{controller}, 'bar', 'right value';
is $m->stack->[0]->{action}, 'baz', 'right value';
is $m->stack->[0]->{format}, undef, 'no value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/versioned/1.0/test', 'right path';
$m =
Mojolicious::Routes::Match->new(GET => '/versioned/1.0/test.xml')
->match($r);
is $m->stack->[0]->{controller}, 'bar', 'right value';
is $m->stack->[0]->{action}, 'baz', 'right value';
is $m->stack->[0]->{format}, 'xml', 'right value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/versioned/1.0/test', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/versioned/2.4/test')->match($r);
is $m->stack->[0]->{controller}, 'foo', 'right value';
is $m->stack->[0]->{action}, 'bar', 'right value';
is $m->stack->[0]->{format}, undef, 'no value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/versioned/2.0/test', 'right path';
is $m->path_for, '/versioned/2.4/test', 'right path';
$m =
Mojolicious::Routes::Match->new(GET => '/versioned/2.0/test.xml')
Mojolicious::Routes::Match->new(GET => '/versioned/2.4/test.xml')
->match($r);
is $m->stack->[0]->{controller}, 'foo', 'right value';
is $m->stack->[0]->{action}, 'bar', 'right value';
is $m->stack->[0]->{format}, 'xml', 'right value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/versioned/2.0/test', 'right path';
is $m->path_for, '/versioned/2.4/test', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/versioned/3.0/test')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/versioned/3.4/test')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/versioned/0.3/test')->match($r);
is $m->stack->[0], undef, 'no value';

0 comments on commit e34f356

Please sign in to comment.