Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add every_header method to Mojo::Headers and fix a redirect bug in Mo…
…jo::UserAgent::Transactor
  • Loading branch information
kraih committed Oct 22, 2016
1 parent 70b728c commit 01a86c9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.09 2016-10-16
7.09 2016-10-22
- Added every_header method to Mojo::Headers.
- Fixed redirect bug in Mojo::UserAgent::Transactor.
- Fixed a few proxy bugs in Mojo::UserAgent.

7.08 2016-09-23
Expand Down
12 changes: 12 additions & 0 deletions lib/Mojo/Headers.pm
Expand Up @@ -49,6 +49,8 @@ sub append {

sub clone { $_[0]->new->from_hash($_[0]->to_hash(1)) }

sub every_header { shift->{headers}{lc shift} || [] }

sub from_hash {
my ($self, $hash) = @_;

Expand Down Expand Up @@ -396,6 +398,16 @@ header, which has no specification yet, but is very commonly used.
Get or replace current header value, shortcut for the C<ETag> header.
=head2 every_header
my $all = $headers->every_header('Location');
Similar to L</"header">, but returns all headers sharing the same name as an
array reference.
# Get first header value
say $headers->every_header('Location')->[0];
=head2 expect
my $expect = $headers->expect;
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -76,7 +76,8 @@ sub redirect {
return undef if uc $req->method eq 'CONNECT';

# Fix location without authority and/or scheme
return undef unless my $location = $res->headers->location;
return undef
unless my $location = $res->headers->every_header('Location')->[0];
$location = Mojo::URL->new($location);
$location = $location->base($req->url)->to_abs unless $location->is_abs;
my $proto = $location->protocol;
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/headers.t
Expand Up @@ -7,6 +7,8 @@ use Mojo::Headers;
my $headers = Mojo::Headers->new;
$headers->add(Connection => 'close');
$headers->add(Connection => 'keep-alive');
is_deeply $headers->every_header('Connection'), ['close', 'keep-alive'],
'right structure';
is $headers->header('Connection'), 'close, keep-alive', 'right value';
$headers->remove('Connection');
is $headers->header('Connection'), undef, 'no value';
Expand Down
13 changes: 13 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -892,6 +892,19 @@ $tx->res->code(302);
$tx->res->headers->location('http:');
is $t->redirect($tx), undef, 'unsupported redirect';

# 302 redirect with multiple locations
$tx = $t->tx(GET => 'http://mojolicious.org/foo');
$tx->res->code(302);
$tx->res->headers->add(Location => 'http://example.com/1.html');
$tx->res->headers->add(Location => 'http://example.com/2.html');
$tx = $t->redirect($tx);
is $tx->req->method, 'GET', 'right method';
is $tx->req->url->to_abs, 'http://example.com/1.html', 'right URL';
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';

# 302 redirect (relative path and query)
$tx = $t->tx(POST => 'http://mojolicious.org/foo/bar?a=b' =>
{Accept => 'application/json'});
Expand Down

0 comments on commit 01a86c9

Please sign in to comment.