Skip to content

Instantly share code, notes, and snippets.

@leejo
Last active February 1, 2016 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leejo/8fc1712f34120838570c to your computer and use it in GitHub Desktop.
Save leejo/8fc1712f34120838570c to your computer and use it in GitHub Desktop.
Route ordering change
We have two routes, defined in this order:
```
$r->route('/cause/:cause_id/campaigns/:type/:paginate/:page/',
type=>[qw(active archived)],
paginate => [qw(page)],
cause_id => qr/\d+/,
page => qr/\d+/)
->to(
paginate => [qw(page)])->to(
controller => 'Public::Cause',
action => 'campaigns',
paginate => 'page',
page => 1,
type => 'active'
);
$r->route('/cause/:cause_id/campaigns/:campaign_id/',
cause_id => qr/\d+/,
campaign_id => qr/\d+/)
->to(
controller => 'Public::Cause',
action => 'campaign'
);
```
With https://github.com/kraih/mojo/commit/c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e#diff-2650135fc08c671d8b875d9b8480f498
the second route now doesn't get hit as the first route will now match. Is this expected behaviour?
@leejo
Copy link
Author

leejo commented Feb 1, 2016

FWIW here's the output of routes -v before c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e:

/cause/:cause_id/campaigns/:campaign_id                                                        ....  *         causecause_idcampaignscampaign_id                                           ^\/cause/((?^:\d+))/campaigns/((?^:\d+))
/cause/:cause_id/campaigns/:campaign_id/projects/:type/:paginate/:page                         ....  *         causecause_idcampaignscampaign_idprojectstypepaginatepage                   ^\/cause/((?^:\d+))/campaigns/((?^:\d+))/projects(?:/(closed|active)?)?(?:/(page)?)?(?:/((?^:\d+))?)?

and after c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e:

/cause/:cause_id/campaigns/:campaign_id                                                        ....  *         causecause_idcampaignscampaign_id                                           ^\/cause/((?^:\d+))/campaigns/((?^:\d+))/?(?:\.([^/]+))?$
/cause/:cause_id/campaigns/:campaign_id/projects/:type/:paginate/:page                         ....  *         causecause_idcampaignscampaign_idprojectstypepaginatepage                   ^\/cause/((?^:\d+))/campaigns/((?^:\d+))/projects(?:/(closed|active)?)?(?:/(page)?)?(?:/((?^:\d+))?)?/?(?:\.([^/]+))?$

@leejo
Copy link
Author

leejo commented Feb 1, 2016

The test:

#!perl

use strict;
use warnings;

use Test::Most;
use Test::GivenGain::Mojo;

my $t = Test::GivenGain::Mojo->new->t;

$t->get_ok( '/cause/6203/campaigns/17047/' )
    ->status_is( 200 )
    ->content_like( qr/\| Caring4Girls/ )
;

done_testing();

Output pre c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e (Notice the different "Routing to controller" log entry):

givengain@precise64:/var/www/givengain/givengain_www$ /opt/tools/bin/prove -I /var/www/mojo/lib -v t/mojo/050_route_ordering.t
t/mojo/050_route_ordering.t .. [Mon Feb  1 13:47:35 2016] [debug] Reading configuration file "/var/www/givengain/givengain_www/mojorouter/mojo_router.conf"
[Mon Feb  1 13:47:35 2016] [info] Installing hook for maintenance_mode: /var/www/givengain/givengain_www/MAINTENANCE -> /content_public/maintenance.htm
[Mon Feb  1 13:47:35 2016] [debug] Redirecting domain: localhost
[Mon Feb  1 13:47:35 2016] [debug] GET "/cause/6203/campaigns/17047/"
[Mon Feb  1 13:47:35 2016] [debug] Routing to controller "MojoRouter::Public::Cause" and action "campaign"
[Mon Feb  1 13:47:36 2016] [debug] template: en/public/cause/campaign
Argument "" isn't numeric in numeric gt (>) at templates/en/public/cause/campaign.html.tt line 78.
[Mon Feb  1 13:47:37 2016] [debug] 200 OK (1.559216s, 0.641/s)

ok 1 - GET /cause/6203/campaigns/17047/
ok 2 - 200 OK
ok 3 - content is similar
1..3
ok
All tests successful.
Files=1, Tests=3, 10 wallclock secs ( 0.03 usr  0.04 sys +  1.86 cusr  3.51 csys =  5.44 CPU)
Result: PASS

Output post c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e:

t/mojo/050_route_ordering.t .. [Mon Feb  1 13:48:03 2016] [debug] Reading configuration file "/var/www/givengain/givengain_www/mojorouter/mojo_router.conf"
[Mon Feb  1 13:48:03 2016] [info] Installing hook for maintenance_mode: /var/www/givengain/givengain_www/MAINTENANCE -> /content_public/maintenance.htm
[Mon Feb  1 13:48:03 2016] [debug] Redirecting domain: localhost
[Mon Feb  1 13:48:03 2016] [debug] GET "/cause/6203/campaigns/17047/"
[Mon Feb  1 13:48:03 2016] [debug] Routing to controller "MojoRouter::Public::Cause" and action "campaigns"
[Mon Feb  1 13:48:04 2016] [debug] template: en/public/cause/campaigns
[Mon Feb  1 13:48:04 2016] [debug] 200 OK (0.731346s, 1.367/s)

ok 1 - GET /cause/6203/campaigns/17047/
ok 2 - 200 OK
not ok 3 - content is similar
1..3

#   Failed test 'content is similar'
#     doesn't match '(?^:\| Caring4Girls)'
# Looks like you failed 1 test of 3.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests

Test Summary Report
-------------------
t/mojo/050_route_ordering.t (Wstat: 256 Tests: 3 Failed: 1)
  Failed test:  3
  Non-zero exit status: 1
Files=1, Tests=3,  8 wallclock secs ( 0.02 usr  0.07 sys +  1.33 cusr  3.38 csys =  4.80 CPU)
Result: FAIL

The only change between the pre and post test run is this:

[leejohnson@lees-macbook-air J0 C8560 14:26:54 * (HEAD detached at 86597e3)]
*/Volumes/code_partition/mojo > git checkout c9b65981e8e64ed3e3c82d6fa8b8872f79ff3d8e
Previous HEAD position was 86597e3... bring back 451 status code
HEAD is now at c9b6598... improve router performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment