Skip to content

Commit

Permalink
Documented url_for() in before_dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Nov 5, 2015
1 parent f518bd1 commit 5b89091
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/Mojolicious/Plugin/RequestBase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ and a request sent to Request to C<https://example.com/myapp/foo>:
# https://example.com/foo (Probably not what you want)
$c->req->url->to_abs;
=head2 Hooks
=head2 before_dispatch
In a L<before_dispatch|Mojolicious/HOOKS> the router has not yet started,
so you need to pass in the request path to get the expected result:
hook before_dispatch => sub {
my $c = shift;
# https://example.com/myapp/foo
$c->url_for($c->req->url->path)->to_abs;
# https://example.com/foo (Probably not what you want)
$c->url_for->to_abs;
};
=head1 DESCRIPTION
Simple plugin to support Request Base header. Just load it and set
Expand Down
10 changes: 10 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ use Test::More;
use Mojolicious::Lite;
use Test::Mojo;

my @before;

plugin 'RequestBase';

hook before_dispatch => sub {
my $c = shift;
@before = ($c->url_for->to_abs, $c->url_for($c->req->url->path)->to_abs);
};

get '/' => sub {
my $c = shift;
$c->render(text => $c->url_for('login'));
Expand Down Expand Up @@ -53,4 +60,7 @@ $t->get_ok('/some/path', {'X-Request-Base' => 'http://example.com/foo'})
->status_is(200)->json_is('/abs_canonicalize', '/foo/some/path')
->json_is('/canonicalize', '/some/path');

is $before[0], "http://example.com/foo", "before_dispatch url_for";
is $before[1], "http://example.com/foo/some/path", "before_dispatch url";

done_testing;

0 comments on commit 5b89091

Please sign in to comment.