Skip to content

Commit

Permalink
fixed multiline header support in hash representation of Mojo::Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 13, 2013
1 parent f43d247 commit f57a325
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@
- Improved tests.
- Fixed bug in Mojo::Headers that prevented multiline headers from being
parsed correctly.
- Fixed multiline header support in hash representation of Mojo::Headers.

3.89 2013-03-04
- Improved documentation.
Expand Down
28 changes: 7 additions & 21 deletions lib/Mojo/Headers.pm
Expand Up @@ -132,23 +132,9 @@ sub remove {

sub to_hash {
my ($self, $multi) = @_;

my %hash;
for my $header (@{$self->names}) {
my @headers = $self->header($header);

# Multi line
if ($multi) { $hash{$header} = [@headers] }

# Flat
else {

# Turn single value arrays into strings
@$_ == 1 and $_ = $_->[0] for @headers;
$hash{$header} = @headers > 1 ? [@headers] : $headers[0];
}
}

$hash{$_} = $multi ? [$self->header($_)] : scalar $self->header($_)
for @{$self->names};
return \%hash;
}

Expand Down Expand Up @@ -365,8 +351,8 @@ Parse headers from a hash reference.
=head2 header
my $string = $headers->header('Foo');
my @lines = $headers->header('Foo');
my $value = $headers->header('Foo');
my @values = $headers->header('Foo');
$headers = $headers->header(Foo => 'one value');
$headers = $headers->header(Foo => 'first value', 'second value');
$headers = $headers->header(Foo => ['first line', 'second line']);
Expand Down Expand Up @@ -477,7 +463,7 @@ resulted in C<Referer> becoming an official header.
=head2 remove
$headers = $headers->remove('Content-Type');
$headers = $headers->remove('Foo');
Remove a header.
Expand Down Expand Up @@ -549,8 +535,8 @@ Shortcut for the C<TE> header.
my $single = $headers->to_hash;
my $multi = $headers->to_hash(1);
Turn headers into hash reference, nested array references to represent multi
line values are disabled by default.
Turn headers into hash reference, nested array references to represent
multiline values are disabled by default.
say $headers->to_hash->{DNT};
Expand Down
20 changes: 12 additions & 8 deletions t/mojo/headers.t
Expand Up @@ -122,8 +122,8 @@ my @array = $headers->header('X-Test');
is_deeply \@array, [[23, 24], ['single line'], [25, 26]], 'right structure';
is_deeply $headers->to_hash(1),
{'X-Test' => [[23, 24], ['single line'], [25, 26]]}, 'right structure';
is_deeply $headers->to_hash,
{'X-Test' => [[23, 24], 'single line', [25, 26]]}, 'right structure';
is_deeply $headers->to_hash, {'X-Test' => '23, 24, single line, 25, 26'},
'right structure';
my $string = $headers->header('X-Test');
is $string, "23, 24, single line, 25, 26", 'right format';

Expand Down Expand Up @@ -155,14 +155,19 @@ Foo: first again
EOF
ok $headers->is_finished, 'parser is finished';
my $multi = [['first', 'second', 'third'], ['first again', 'second again']];
$hash = {'Content-Type' => 'text/plain', Foo => $multi};
is_deeply $headers->to_hash, $hash, 'right structure';
$hash = {'Content-Type' => [['text/plain']], Foo => $multi};
is_deeply $headers->to_hash(1), $hash, 'right structure';
is_deeply [$headers->header('Foo')], $multi, 'right structure';
is scalar $headers->header('Foo'),
'first, second, third, first again, second again', 'right value';
$headers = Mojo::Headers->new->parse($headers->to_string . "\x0d\x0a\x0d\x0a");
ok $headers->is_finished, 'parser is finished';
is_deeply $headers->to_hash, $hash, 'successful roundtrip';
is_deeply $headers->to_hash(1), $hash, 'successful roundtrip';
$hash = {
'Content-Type' => 'text/plain',
Foo => 'first, second, third, first again, second again'
};
is_deeply $headers->to_hash, $hash, 'right structure';

# Set headers from hash
$headers = Mojo::Headers->new;
Expand All @@ -179,9 +184,8 @@ $headers = Mojo::Headers->new;
$headers->from_hash(
{'X-Test' => [[23, 24], ['single line'], [25, 26]], 'X-Test2' => 'foo'});
$hash = $headers->to_hash;
is_deeply $hash->{'X-Test'}, [[23, 24], 'single line', [25, 26]],
'right structure';
is_deeply $hash->{'X-Test2'}, 'foo', 'right structure';
is $hash->{'X-Test'}, '23, 24, single line, 25, 26', 'right value';
is $hash->{'X-Test2'}, 'foo', 'right value';
$hash = $headers->to_hash(1);
is_deeply $hash->{'X-Test'}, [[23, 24], ['single line'], [25, 26]],
'right structure';
Expand Down

0 comments on commit f57a325

Please sign in to comment.