Skip to content

Commit 74b9567

Browse files
openstrikeoalders
authored andcommitted
Increase test coverage of HTTP::Config to 100% (bar POD)
More tests for HTTP::Headers and sub-modules More tests for HTTP::Message Aliases for encodings in Message.pm More tests for requests, responses and status Skip tests breaking on older perls Avoid uninitialized warnings in Request.pm and Response.pm Silence carp about void content call in t/message.t carp about undef args and test them Remove attempt to substitute on undef value in parse()
1 parent b9f1c04 commit 74b9567

File tree

13 files changed

+417
-84
lines changed

13 files changed

+417
-84
lines changed

lib/HTTP/Message.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ sub decodable
428428
};
429429
eval {
430430
require IO::Uncompress::Bunzip2;
431-
push(@enc, "x-bzip2");
431+
push(@enc, "x-bzip2", "bzip2");
432432
};
433433
# we don't care about announcing the 'identity', 'base64' and
434434
# 'quoted-printable' stuff
@@ -460,7 +460,7 @@ sub encode
460460

461461
my $content = $self->content;
462462
for my $encoding (@enc) {
463-
if ($encoding eq "identity") {
463+
if ($encoding eq "identity" || $encoding eq "none") {
464464
# nothing to do
465465
}
466466
elsif ($encoding eq "base64") {
@@ -481,7 +481,7 @@ sub encode
481481
or die "Can't deflate content: $IO::Compress::Deflate::DeflateError";
482482
$content = $output;
483483
}
484-
elsif ($encoding eq "x-bzip2") {
484+
elsif ($encoding eq "x-bzip2" || $encoding eq "bzip2") {
485485
require IO::Compress::Bzip2;
486486
my $output;
487487
IO::Compress::Bzip2::bzip2(\$content, \$output)

lib/HTTP/Request.pm

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ sub new
1818
sub parse
1919
{
2020
my($class, $str) = @_;
21+
Carp::carp('Undefined argument to parse()') if $^W && ! defined $str;
2122
my $request_line;
22-
if ($str =~ s/^(.*)\n//) {
23+
if (defined $str && $str =~ s/^(.*)\n//) {
2324
$request_line = $1;
2425
}
2526
else {
@@ -28,10 +29,12 @@ sub parse
2829
}
2930

3031
my $self = $class->SUPER::parse($str);
31-
my($method, $uri, $protocol) = split(' ', $request_line);
32-
$self->method($method) if defined($method);
33-
$self->uri($uri) if defined($uri);
34-
$self->protocol($protocol) if $protocol;
32+
if (defined $request_line) {
33+
my($method, $uri, $protocol) = split(' ', $request_line);
34+
$self->method($method);
35+
$self->uri($uri) if defined($uri);
36+
$self->protocol($protocol) if $protocol;
37+
}
3538
$self;
3639
}
3740

lib/HTTP/Response.pm

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,31 @@ sub new
2121
sub parse
2222
{
2323
my($class, $str) = @_;
24+
Carp::carp('Undefined argument to parse()') if $^W && ! defined $str;
2425
my $status_line;
25-
if ($str =~ s/^(.*)\n//) {
26+
if (defined $str && $str =~ s/^(.*)\n//) {
2627
$status_line = $1;
2728
}
2829
else {
2930
$status_line = $str;
3031
$str = "";
3132
}
3233

33-
$status_line =~ s/\r\z//;
34+
$status_line =~ s/\r\z// if defined $status_line;
3435

3536
my $self = $class->SUPER::parse($str);
36-
my($protocol, $code, $message);
37-
if ($status_line =~ /^\d{3} /) {
38-
# Looks like a response created by HTTP::Response->new
39-
($code, $message) = split(' ', $status_line, 2);
40-
} else {
41-
($protocol, $code, $message) = split(' ', $status_line, 3);
37+
if (defined $status_line) {
38+
my($protocol, $code, $message);
39+
if ($status_line =~ /^\d{3} /) {
40+
# Looks like a response created by HTTP::Response->new
41+
($code, $message) = split(' ', $status_line, 2);
42+
} else {
43+
($protocol, $code, $message) = split(' ', $status_line, 3);
44+
}
45+
$self->protocol($protocol) if $protocol;
46+
$self->code($code) if defined($code);
47+
$self->message($message) if defined($message);
4248
}
43-
$self->protocol($protocol) if $protocol;
44-
$self->code($code) if defined($code);
45-
$self->message($message) if defined($message);
4649
$self;
4750
}
4851

t/common-req.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use strict;
22
use warnings;
33

44
use Test::More;
5-
plan tests => 61;
5+
plan tests => 64;
66

77
use HTTP::Request::Common;
88

@@ -23,6 +23,16 @@ ok($r->uri->eq("http://www.sn.no"));
2323
is($r->header('If-Match'), "abc");
2424
is($r->header("from"), "aas\@sn.no");
2525

26+
$r = HEAD "http://www.sn.no/",
27+
Content => 'foo';
28+
is($r->content, 'foo');
29+
30+
$r = HEAD "http://www.sn.no/",
31+
Content => 'foo',
32+
'Content-Length' => 50;
33+
is($r->content, 'foo');
34+
is($r->content_length, 50);
35+
2636
$r = PUT "http://www.sn.no",
2737
Content => 'foo';
2838
note $r->as_string, "\n";

t/headers-auth.t

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use warnings;
33

44
use Test::More;
55

6-
plan tests => 6;
6+
plan tests => 9;
77

88
use HTTP::Response;
99
use HTTP::Headers::Auth;
@@ -39,3 +39,10 @@ my $string = $res->as_string;
3939
like($string, qr/WWW-Authenticate: Basic realm="foo3", foo=33/);
4040
like($string, qr/WWW-Authenticate: Digest (nonce=bar, foo=foo|foo=foo, nonce=bar)/);
4141

42+
$res = HTTP::Response->new(401);
43+
my @auth = $res->proxy_authenticate('foo');
44+
is_deeply(\@auth, []);
45+
@auth = $res->proxy_authenticate('foo', 'bar');
46+
is_deeply(\@auth, ['foo', {}]);
47+
@auth = $res->proxy_authenticate('foo', {'bar' => '_'});
48+
is_deeply(\@auth, ['foo', {}, 'bar', {}]);

t/headers-etag.t

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use warnings;
33

44
use Test::More;
55

6-
plan tests => 4;
6+
plan tests => 11;
77

88
require HTTP::Headers::ETag;
99

@@ -15,6 +15,17 @@ is($h->etag, qq("tag1"));
1515
$h->etag("w/tag2");
1616
is($h->etag, qq(W/"tag2"));
1717

18+
$h->etag(" w/, weaktag");
19+
is($h->etag, qq(W/"", "weaktag"));
20+
my @list = $h->etag;
21+
is_deeply(\@list, ['W/""', '"weaktag"']);
22+
23+
$h->etag(" w/");
24+
is($h->etag, qq(W/""));
25+
26+
$h->etag(" ");
27+
is($h->etag, "");
28+
1829
$h->if_match(qq(W/"foo", bar, baz), "bar");
1930
$h->if_none_match(333);
2031

@@ -27,3 +38,8 @@ is($h->if_range, $t);
2738

2839
note $h->as_string;
2940

41+
@list = $h->if_range;
42+
is($#list, 0);
43+
is($list[0], $t);
44+
$h->if_range(undef);
45+
is($h->if_range, '');

t/headers-util.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ my @s_tests = (
2828
'basic; realm="\"foo\\\\bar\""'],
2929
);
3030

31-
plan tests => @s_tests + 2;
31+
plan tests => @s_tests + 3;
3232

3333
for (@s_tests) {
3434
my($arg, $expect) = @$_;
@@ -43,3 +43,4 @@ note "# Extra tests\n";
4343
# some extra tests
4444
is(join_header_words("foo" => undef, "bar" => "baz"), "foo; bar=baz");
4545
is(join_header_words(), "");
46+
is(join_header_words([]), "");

t/headers.t

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use warnings;
33

44
use Test::More;
55

6-
plan tests => 168;
6+
plan tests => 188;
77

88
my($h, $h2);
99
sub j { join("|", @_) }
@@ -189,6 +189,7 @@ is(j($h->header_field_names), "Date|If-Modified-Since|If-Unmodified-Since|Expire
189189

190190
$h->clear;
191191
is($h->content_type, "");
192+
is($h->content_type(""), "");
192193
is($h->content_type("text/html"), "");
193194
is($h->content_type, "text/html");
194195
is($h->content_type(" TEXT / HTML ") , "text/html");
@@ -201,6 +202,14 @@ is($h->header("content_type"), "text/html;\n charSet = \"ISO-8859-1\"; Foo=1 ");
201202
ok($h->content_is_html);
202203
ok(!$h->content_is_xhtml);
203204
ok(!$h->content_is_xml);
205+
$h->content_type("application/vnd.wap.xhtml+xml");
206+
ok($h->content_is_html);
207+
ok($h->content_is_xhtml);
208+
ok($h->content_is_xml);
209+
$h->content_type("text/xml");
210+
ok(!$h->content_is_html);
211+
ok(!$h->content_is_xhtml);
212+
ok($h->content_is_xml);
204213
$h->content_type("application/xhtml+xml");
205214
ok($h->content_is_html);
206215
ok($h->content_is_xhtml);
@@ -453,6 +462,33 @@ content_type: text/html
453462
foo_bar: 1
454463
EOT
455464

465+
$h = HTTP::Headers->new;
466+
ok(!defined $h->warning('foo', 'INIT'));
467+
is($h->warning('bar'), 'foo');
468+
is($h->warning('baz', 'GET'), 'bar');
469+
is($h->as_string, <<EOT);
470+
Warning: bar
471+
EOT
472+
473+
$h = HTTP::Headers->new;
474+
ok(!defined $h->header(':foo', 'bar'));
475+
ok(!defined $h->header(':zap', 'bang'));
476+
$h->push_header(':zap', ['kapow', 'shazam']);
477+
is(j($h->header_field_names), ':foo|:zap');
478+
is(j($h->header_field_names), ':foo|:zap');
479+
$h->scan(sub { $_[1] .= '!' });
480+
is(j($h->header(':zap')), 'bang!|kapow!|shazam!');
481+
is(j($h->header(':foo')), 'bar');
482+
is($h->as_string, <<EOT);
483+
foo: bar
484+
zap: bang!
485+
zap: kapow!
486+
zap: shazam!
487+
EOT
488+
is(j($h->remove_header(':zap')), 'bang!|kapow!|shazam!');
489+
$h->push_header(':zap', 'whomp', ':foo', 'quux');
490+
is(j($h->header(':foo')), 'bar|quux');
491+
456492
# [RT#30579] IE6 appens "; length = NNNN" on If-Modified-Since (can we handle it)
457493
$h = HTTP::Headers->new(
458494
if_modified_since => "Sat, 29 Oct 1994 19:43:31 GMT; length=34343"

t/http-config.t

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@ use strict;
22
use warnings;
33

44
use Test::More;
5-
plan tests => 16;
5+
plan tests => 28;
66

77
use HTTP::Config;
88

99
sub j { join("|", @_) }
1010

1111
my $conf = HTTP::Config->new;
1212
ok($conf->empty);
13+
is($conf->entries, 0);
1314
$conf->add_item(42);
1415
ok(!$conf->empty);
16+
is($conf->entries, 1);
1517
is(j($conf->matching_items("http://www.example.com/foo")), 42);
1618
is(j($conf->remove_items), 42);
19+
is(j($conf->remove_items), '');
1720
is($conf->matching_items("http://www.example.com/foo"), 0);
21+
is($conf->matching_items('foo', 'bar', 'baz'), 0);
22+
$conf->add({item => "http://www.example.com/foo", m_uri__HEAD => undef});
23+
is($conf->entries, 1);
24+
is($conf->matching_items("http://www.example.com/foo"), 0);
25+
SKIP: {
26+
my $res;
27+
eval { $res = $conf->matching_items(0); };
28+
skip "can fails on non-object", 2 if $@;
29+
is($res, 0);
30+
eval { $res = $conf->matching(0); };
31+
ok(!defined $res);
32+
}
1833

1934
$conf = HTTP::Config->new;
2035

@@ -27,6 +42,10 @@ $conf->add_item("not secure", m_secure => 0);
2742
$conf->add_item("slash", m_host_port => "www.example.com:80", m_path_prefix => "/");
2843
$conf->add_item("u:p", m_host_port => "www.example.com:80", m_path_prefix => "/foo");
2944
$conf->add_item("success", m_code => "2xx");
45+
is($conf->find(m_domain => ".com")->{item}, '.com');
46+
my @found = $conf->find(m_domain => ".com");
47+
is($#found, 0);
48+
is($found[0]->{item}, '.com');
3049

3150
use HTTP::Request;
3251
my $request = HTTP::Request->new(HEAD => "http://www.example.com/foo/bar");
@@ -70,6 +89,8 @@ is(j($conf->matching_items($response)), "xhtml|html|any");
7089
$response->content_type("text/html");
7190
is(j($conf->matching_items($response)), "HTML|html|text|any");
7291

92+
$response->request(undef);
93+
is(j($conf->matching_items($response)), "HTML|html|text|any");
7394

7495
{
7596
my @warnings;

0 commit comments

Comments
 (0)