Skip to content

Commit

Permalink
preserve all request methods other than POST
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 1, 2014
1 parent 16a8dd0 commit 4d62346
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -80,19 +80,19 @@ sub redirect {
$location = $location->base($old->req->url)->to_abs unless $location->is_abs;

# Clone request if necessary
my $new = Mojo::Transaction::HTTP->new;
my $req = $old->req;
my $method = uc $req->method;
my $new = Mojo::Transaction::HTTP->new;
my $req = $old->req;
if ($code eq 307 || $code eq 308) {
return undef unless my $clone = $req->clone;
$new->req($clone);
}
else {
$method = 'GET' if $method ne 'HEAD';
my $headers = $new->req->content->headers($req->headers->clone)->headers;
my $method = uc $req->method;
my $headers = $new->req->method($method eq 'POST' ? 'GET' : $method)
->content->headers($req->headers->clone)->headers;
$headers->remove($_) for grep {/^content-/i} @{$headers->names};
}
my $headers = $new->req->method($method)->url($location)->headers;
my $headers = $new->req->url($location)->headers;
$headers->remove($_) for qw(Authorization Cookie Host Referer);
return $new->previous($old);
}
Expand Down
16 changes: 16 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -527,6 +527,22 @@ is $tx->req->body, '', 'no content';
is $tx->res->code, undef, 'no status';
is $tx->res->headers->location, undef, 'no "Location" value';

# 301 redirect with content (DELETE)
$tx = $t->tx(
DELETE => 'http://mojolicio.us/foo' => {Accept => '*/*'} => 'whatever');
$tx->res->code(301);
$tx->res->headers->location('http://example.com/bar');
is $tx->req->headers->accept, '*/*', 'right "Accept" value';
is $tx->req->body, 'whatever', 'right content';
$tx = $t->redirect($tx);
is $tx->req->method, 'DELETE', 'right method';
is $tx->req->url->to_abs, 'http://example.com/bar', 'right URL';
is $tx->req->headers->accept, '*/*', 'right "Accept" value';
is $tx->req->headers->location, undef, 'no "Location" value';
is $tx->req->body, '', 'no content';
is $tx->res->code, undef, 'no status';
is $tx->res->headers->location, undef, 'no "Location" value';

# Simple 302 redirect
$tx = $t->tx(
POST => 'http://mojolicio.us/foo' => {Accept => 'application/json'});
Expand Down

0 comments on commit 4d62346

Please sign in to comment.