Skip to content

Commit

Permalink
added redirects method to Mojo::Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 31, 2013
1 parent 3bfbf40 commit ff95336
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.23 2013-07-31
- Added redirects method to Mojo::Transaction.

4.22 2013-07-30
- Improved Mojo::Server to use FindBin more defensively.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Headers.pm
Expand Up @@ -452,7 +452,7 @@ Shortcut for the C<Location> header.
my $names = $headers->names;
Generate a list of all currently defined headers.
Return a list of all currently defined headers.
=head2 origin
Expand Down
15 changes: 15 additions & 0 deletions lib/Mojo/Transaction.pm
Expand Up @@ -38,6 +38,14 @@ sub is_websocket {undef}

sub is_writing { (shift->{state} // 'write') eq 'write' }

sub redirects {
my $self = shift;
my @redirects;
return \@redirects unless my $previous = $self->previous;
do { unshift @redirects, $previous } while $previous = $previous->previous;
return \@redirects;
}

sub remote_address {
my $self = shift;

Expand Down Expand Up @@ -244,6 +252,13 @@ Check if transaction is writing.
Resume transaction.
=head2 redirects
my $redirects = $tx->redirects;
Return a list of all previous transactions that preceded this followup
transaction.
=head2 remote_address
my $address = $tx->remote_address;
Expand Down
9 changes: 2 additions & 7 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -372,14 +372,9 @@ sub _remove {

sub _redirect {
my ($self, $c, $old) = @_;

# Follow redirect unless the maximum has been reached already
return undef unless my $new = $self->transactor->redirect($old);
my $redirects = delete $c->{redirects} || 0;
return undef unless $redirects < $self->max_redirects;
my $id = $self->_start($new, delete $c->{cb});

return $self->{connections}{$id}{redirects} = $redirects + 1;
return undef unless @{$old->redirects} < $self->max_redirects;
return $self->_start($new, delete $c->{cb});
}

sub _server {
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/user_agent_online.t
Expand Up @@ -241,6 +241,10 @@ is $tx->res->code, 200, 'right status';
is $tx->previous->req->method, 'GET', 'right method';
is $tx->previous->req->url, 'http://www.wikipedia.org/wiki/Perl', 'right url';
is $tx->previous->res->code, 301, 'right status';
is $tx->redirects->[-1]->req->method, 'GET', 'right method';
is $tx->redirects->[-1]->req->url, 'http://www.wikipedia.org/wiki/Perl',
'right url';
is $tx->redirects->[-1]->res->code, 301, 'right status';

# Custom chunked request
$tx = Mojo::Transaction::HTTP->new;
Expand Down
13 changes: 13 additions & 0 deletions t/mojolicious/lite_app.t
Expand Up @@ -359,6 +359,8 @@ get '/redirect_named' => sub {
shift->redirect_to('index', format => 'txt')->render(text => 'Redirecting!');
};

get '/redirect_twice' => sub { shift->redirect_to('/redirect_named') };

get '/redirect_no_render' => sub {
shift->redirect_to('index', {format => 'txt'});
};
Expand Down Expand Up @@ -916,6 +918,17 @@ $t->get_ok('/redirect_named')->status_is(302)
->header_is('Content-Length' => 12)
->header_like(Location => qr!/template.txt$!)->content_is('Redirecting!');

# Redirect twice
$t->ua->max_redirects(3);
$t->get_ok('/redirect_twice')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->text_is('div#☃' => 'Redirect works!');
my $redirects = $t->tx->redirects;
is scalar @$redirects, 2, 'two redirects';
is $redirects->[0]->req->url->path, '/redirect_twice', 'right path';
is $redirects->[1]->req->url->path, '/redirect_named', 'right path';
$t->ua->max_redirects(0);

# Redirect without rendering
$t->get_ok('/redirect_no_render')->status_is(302)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down

0 comments on commit ff95336

Please sign in to comment.