Skip to content

Commit 7a5395c

Browse files
authored
Merge pull request #37 from tomhukins/no_content_warning
Avoid inconsistent setting of content to undef.
2 parents 530f425 + 0348b39 commit 7a5395c

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
/MYMETA.*
1111
/HTTP-Message-*/
1212
/HTTP-Message-*.tar.gz
13+
**~

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ perl:
88
- "5.14"
99
- "5.12"
1010
- "5.10"
11+
- "5.8"
1112
before_install:
1213
- git clone git://github.com/travis-perl/helpers ~/travis-perl-helpers
1314
- source ~/travis-perl-helpers/init

MANIFEST.SKIP

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
^HTTP-Message-.*.tar.gz
55
^\.ackrc$
66
^test-
7+
^\.git.*
8+
^\.travis.yml

lib/HTTP/Message.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ sub _set_content {
143143
my $self = $_[0];
144144
_utf8_downgrade($_[1]);
145145
if (!ref($_[1]) && ref($self->{_content}) eq "SCALAR") {
146-
${$self->{_content}} = $_[1];
146+
${$self->{_content}} = defined( $_[1] ) ? $_[1] : '';
147147
}
148148
else {
149149
die "Can't set content to be a scalar reference" if ref($_[1]) eq "SCALAR";
150-
$self->{_content} = $_[1];
150+
$self->{_content} = defined( $_[1] ) ? $_[1] : '';
151151
delete $self->{_content_ref};
152152
}
153153
delete $self->{_parts} unless $_[2];
@@ -835,6 +835,9 @@ The content() method sets the raw content if an argument is given. If no
835835
argument is given the content is not touched. In either case the
836836
original raw content is returned.
837837
838+
If the C<undef> argument is given, the content is reset to its default value,
839+
which is an empty string.
840+
838841
Note that the content should be a string of bytes. Strings in perl
839842
can contain characters outside the range of a byte. The C<Encode>
840843
module can be used to turn such strings into a string of bytes.

t/distmanifest.t

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use Test::More;
55
BEGIN {
66
plan skip_all => 'these tests are for authors only!'
77
unless -d '.git' || $ENV{AUTHOR_TESTING};
8+
9+
plan skip_all => 'these tests require Test::DistManifest'
10+
unless eval{ require Test::DistManifest; };
811
}
912

10-
use Test::DistManifest;
11-
manifest_ok();
13+
Test::DistManifest::manifest_ok();

t/message.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#! perl -w
2+
13
use strict;
24
use warnings;
35

46
use Test::More;
57

6-
plan tests => 129;
8+
plan tests => 131;
79

810
require HTTP::Message;
911
use Config qw(%Config);
@@ -22,6 +24,17 @@ is($m->content, "");
2224
$m->header("Foo", 1);
2325
is($m->as_string, "Foo: 1\n\n");
2426

27+
{
28+
# A message with an undef set content
29+
# will stay consistent and have empty string
30+
# as a content
31+
my $m = HTTP::Message->new();
32+
$m->content(undef);
33+
is($m->as_string, "\n");
34+
is($m->content, "");
35+
}
36+
37+
2538
$m2 = HTTP::Message->new($m->headers);
2639
$m2->header(bar => 2);
2740
is($m->as_string, "Foo: 1\n\n");

0 commit comments

Comments
 (0)