|
Packit |
a09cf7 |
use strict;
|
|
Packit |
a09cf7 |
use warnings;
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
use Test::More;
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
plan tests => 188;
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
my($h, $h2);
|
|
Packit |
a09cf7 |
sub j { join("|", @_) }
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
require HTTP::Headers;
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
ok($h);
|
|
Packit |
a09cf7 |
is(ref($h), "HTTP::Headers");
|
|
Packit |
a09cf7 |
is($h->as_string, "");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(foo => "bar", foo => "baaaaz", Foo => "baz");
|
|
Packit |
a09cf7 |
is($h->as_string, "Foo: bar\nFoo: baaaaz\nFoo: baz\n");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(foo => ["bar", "baz"]);
|
|
Packit |
a09cf7 |
is($h->as_string, "Foo: bar\nFoo: baz\n");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(foo => 1, bar => 2, foo_bar => 3);
|
|
Packit |
a09cf7 |
is($h->as_string, "Bar: 2\nFoo: 1\nFoo-Bar: 3\n");
|
|
Packit |
a09cf7 |
is($h->as_string(";"), "Bar: 2;Foo: 1;Foo-Bar: 3;");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("Foo"), 1);
|
|
Packit |
a09cf7 |
is($h->header("FOO"), 1);
|
|
Packit |
a09cf7 |
is(j($h->header("foo")), 1);
|
|
Packit |
a09cf7 |
is($h->header("foo-bar"), 3);
|
|
Packit |
a09cf7 |
is($h->header("foo_bar"), 3);
|
|
Packit |
a09cf7 |
is($h->header("Not-There"), undef);
|
|
Packit |
a09cf7 |
is(j($h->header("Not-There")), "");
|
|
Packit |
a09cf7 |
is(eval { $h->header }, undef);
|
|
Packit |
a09cf7 |
ok($@);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("Foo", 11), 1);
|
|
Packit |
a09cf7 |
is($h->header("Foo", [1, 1]), 11);
|
|
Packit |
a09cf7 |
is($h->header("Foo"), "1, 1");
|
|
Packit |
a09cf7 |
is(j($h->header("Foo")), "1|1");
|
|
Packit |
a09cf7 |
is($h->header(foo => 11, Foo => 12, bar => 22), 2);
|
|
Packit |
a09cf7 |
is($h->header("Foo"), "11, 12");
|
|
Packit |
a09cf7 |
is($h->header("Bar"), 22);
|
|
Packit |
a09cf7 |
is($h->header("Bar", undef), 22);
|
|
Packit |
a09cf7 |
is(j($h->header("bar", 22)), "");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->push_header(Bar => 22);
|
|
Packit |
a09cf7 |
is($h->header("Bar"), "22, 22");
|
|
Packit |
a09cf7 |
$h->push_header(Bar => [23 .. 25]);
|
|
Packit |
a09cf7 |
is($h->header("Bar"), "22, 22, 23, 24, 25");
|
|
Packit |
a09cf7 |
is(j($h->header("Bar")), "22|22|23|24|25");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->clear;
|
|
Packit |
a09cf7 |
$h->header(Foo => 1);
|
|
Packit |
a09cf7 |
is($h->as_string, "Foo: 1\n");
|
|
Packit |
a09cf7 |
$h->init_header(Foo => 2);
|
|
Packit |
a09cf7 |
$h->init_header(Bar => 2);
|
|
Packit |
a09cf7 |
is($h->as_string, "Bar: 2\nFoo: 1\n");
|
|
Packit |
a09cf7 |
$h->init_header(Foo => [2, 3]);
|
|
Packit |
a09cf7 |
$h->init_header(Baz => [2, 3]);
|
|
Packit |
a09cf7 |
is($h->as_string, "Bar: 2\nBaz: 2\nBaz: 3\nFoo: 1\n");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
eval { $h->init_header(A => 1, B => 2, C => 3) };
|
|
Packit |
a09cf7 |
ok($@);
|
|
Packit |
a09cf7 |
is($h->as_string, "Bar: 2\nBaz: 2\nBaz: 3\nFoo: 1\n");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->clone->remove_header("Foo"), 1);
|
|
Packit |
a09cf7 |
is($h->clone->remove_header("Bar"), 1);
|
|
Packit |
a09cf7 |
is($h->clone->remove_header("Baz"), 2);
|
|
Packit |
a09cf7 |
is($h->clone->remove_header(qw(Foo Bar Baz Not-There)), 4);
|
|
Packit |
a09cf7 |
is($h->clone->remove_header("Not-There"), 0);
|
|
Packit |
a09cf7 |
is(j($h->clone->remove_header("Foo")), 1);
|
|
Packit |
a09cf7 |
is(j($h->clone->remove_header("Bar")), 2);
|
|
Packit |
a09cf7 |
is(j($h->clone->remove_header("Baz")), "2|3");
|
|
Packit |
a09cf7 |
is(j($h->clone->remove_header(qw(Foo Bar Baz Not-There))), "1|2|2|3");
|
|
Packit |
a09cf7 |
is(j($h->clone->remove_header("Not-There")), "");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(
|
|
Packit |
a09cf7 |
allow => "GET",
|
|
Packit |
a09cf7 |
content => "none",
|
|
Packit |
a09cf7 |
content_type => "text/html",
|
|
Packit |
a09cf7 |
content_md5 => "dummy",
|
|
Packit |
a09cf7 |
content_encoding => "gzip",
|
|
Packit |
a09cf7 |
content_foo => "bar",
|
|
Packit |
a09cf7 |
last_modified => "yesterday",
|
|
Packit |
a09cf7 |
expires => "tomorrow",
|
|
Packit |
a09cf7 |
etag => "abc",
|
|
Packit |
a09cf7 |
date => "today",
|
|
Packit |
a09cf7 |
user_agent => "libwww-perl",
|
|
Packit |
a09cf7 |
zoo => "foo",
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Date: today
|
|
Packit |
a09cf7 |
User-Agent: libwww-perl
|
|
Packit |
a09cf7 |
ETag: abc
|
|
Packit |
a09cf7 |
Allow: GET
|
|
Packit |
a09cf7 |
Content-Encoding: gzip
|
|
Packit |
a09cf7 |
Content-MD5: dummy
|
|
Packit |
a09cf7 |
Content-Type: text/html
|
|
Packit |
a09cf7 |
Expires: tomorrow
|
|
Packit |
a09cf7 |
Last-Modified: yesterday
|
|
Packit |
a09cf7 |
Content: none
|
|
Packit |
a09cf7 |
Content-Foo: bar
|
|
Packit |
a09cf7 |
Zoo: foo
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h2 = $h->clone;
|
|
Packit |
a09cf7 |
is($h->as_string, $h2->as_string);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->remove_content_headers->as_string, <
|
|
Packit |
a09cf7 |
Allow: GET
|
|
Packit |
a09cf7 |
Content-Encoding: gzip
|
|
Packit |
a09cf7 |
Content-MD5: dummy
|
|
Packit |
a09cf7 |
Content-Type: text/html
|
|
Packit |
a09cf7 |
Expires: tomorrow
|
|
Packit |
a09cf7 |
Last-Modified: yesterday
|
|
Packit |
a09cf7 |
Content-Foo: bar
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Date: today
|
|
Packit |
a09cf7 |
User-Agent: libwww-perl
|
|
Packit |
a09cf7 |
ETag: abc
|
|
Packit |
a09cf7 |
Content: none
|
|
Packit |
a09cf7 |
Zoo: foo
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# separate code path for the void context case, so test it as well
|
|
Packit |
a09cf7 |
$h2->remove_content_headers;
|
|
Packit |
a09cf7 |
is($h->as_string, $h2->as_string);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->clear;
|
|
Packit |
a09cf7 |
is($h->as_string, "");
|
|
Packit |
a09cf7 |
undef($h2);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
is($h->header_field_names, 0);
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new( etag => 1, foo => [2,3],
|
|
Packit |
a09cf7 |
content_type => "text/plain");
|
|
Packit |
a09cf7 |
is($h->header_field_names, 3);
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "ETag|Content-Type|Foo");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
{
|
|
Packit |
a09cf7 |
my @tmp;
|
|
Packit |
a09cf7 |
$h->scan(sub { push(@tmp, @_) });
|
|
Packit |
a09cf7 |
is(j(@tmp), "ETag|1|Content-Type|text/plain|Foo|2|Foo|3");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
@tmp = ();
|
|
Packit |
a09cf7 |
eval { $h->scan(sub { push(@tmp, @_); die if $_[0] eq "Content-Type" }) };
|
|
Packit |
a09cf7 |
ok($@);
|
|
Packit |
a09cf7 |
is(j(@tmp), "ETag|1|Content-Type|text/plain");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
@tmp = ();
|
|
Packit |
a09cf7 |
$h->scan(sub { push(@tmp, @_) });
|
|
Packit |
a09cf7 |
is(j(@tmp), "ETag|1|Content-Type|text/plain|Foo|2|Foo|3");
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# CONVENIENCE METHODS
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
is($h->date, undef);
|
|
Packit |
a09cf7 |
is($h->date(time), undef);
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "Date");
|
|
Packit |
a09cf7 |
like($h->header("Date"), qr/^[A-Z][a-z][a-z], \d\d .* GMT$/);
|
|
Packit |
a09cf7 |
{
|
|
Packit |
a09cf7 |
my $off = time - $h->date;
|
|
Packit |
a09cf7 |
ok($off == 0 || $off == 1);
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
if ($] < 5.006) {
|
|
Packit |
a09cf7 |
Test::skip("Can't call variable method", 1) for 1..13;
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
else {
|
|
Packit |
a09cf7 |
# other date fields
|
|
Packit |
a09cf7 |
for my $field (qw(expires if_modified_since if_unmodified_since
|
|
Packit |
a09cf7 |
last_modified))
|
|
Packit |
a09cf7 |
{
|
|
Packit |
a09cf7 |
eval <<'EOT'; die $@ if $@;
|
|
Packit |
a09cf7 |
is($h->$field, undef);
|
|
Packit |
a09cf7 |
is($h->$field(time), undef);
|
|
Packit |
a09cf7 |
like((time - $h->$field), qr/^[01]$/);
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "Date|If-Modified-Since|If-Unmodified-Since|Expires|Last-Modified");
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->clear;
|
|
Packit |
a09cf7 |
is($h->content_type, "");
|
|
Packit |
a09cf7 |
is($h->content_type(""), "");
|
|
Packit |
a09cf7 |
is($h->content_type("text/html"), "");
|
|
Packit |
a09cf7 |
is($h->content_type, "text/html");
|
|
Packit |
a09cf7 |
is($h->content_type(" TEXT / HTML ") , "text/html");
|
|
Packit |
a09cf7 |
is($h->content_type, "text/html");
|
|
Packit |
a09cf7 |
is(j($h->content_type), "text/html");
|
|
Packit |
a09cf7 |
is($h->content_type("text/html;\n charSet = \"ISO-8859-1\"; Foo=1 "), "text/html");
|
|
Packit |
a09cf7 |
is($h->content_type, "text/html");
|
|
Packit |
a09cf7 |
is(j($h->content_type), "text/html|charSet = \"ISO-8859-1\"; Foo=1 ");
|
|
Packit |
a09cf7 |
is($h->header("content_type"), "text/html;\n charSet = \"ISO-8859-1\"; Foo=1 ");
|
|
Packit |
a09cf7 |
ok($h->content_is_html);
|
|
Packit |
a09cf7 |
ok(!$h->content_is_xhtml);
|
|
Packit |
a09cf7 |
ok(!$h->content_is_xml);
|
|
Packit |
a09cf7 |
$h->content_type("application/vnd.wap.xhtml+xml");
|
|
Packit |
a09cf7 |
ok($h->content_is_html);
|
|
Packit |
a09cf7 |
ok($h->content_is_xhtml);
|
|
Packit |
a09cf7 |
ok($h->content_is_xml);
|
|
Packit |
a09cf7 |
$h->content_type("text/xml");
|
|
Packit |
a09cf7 |
ok(!$h->content_is_html);
|
|
Packit |
a09cf7 |
ok(!$h->content_is_xhtml);
|
|
Packit |
a09cf7 |
ok($h->content_is_xml);
|
|
Packit |
a09cf7 |
$h->content_type("application/xhtml+xml");
|
|
Packit |
a09cf7 |
ok($h->content_is_html);
|
|
Packit |
a09cf7 |
ok($h->content_is_xhtml);
|
|
Packit |
a09cf7 |
ok($h->content_is_xml);
|
|
Packit |
a09cf7 |
is($h->content_type("text/html;\n charSet = \"ISO-8859-1\"; Foo=1 "), "application/xhtml+xml");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->content_encoding, undef);
|
|
Packit |
a09cf7 |
is($h->content_encoding("gzip"), undef);
|
|
Packit |
a09cf7 |
is($h->content_encoding, "gzip");
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "Content-Encoding|Content-Type");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->content_language, undef);
|
|
Packit |
a09cf7 |
is($h->content_language("no"), undef);
|
|
Packit |
a09cf7 |
is($h->content_language, "no");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->title, undef);
|
|
Packit |
a09cf7 |
is($h->title("This is a test"), undef);
|
|
Packit |
a09cf7 |
is($h->title, "This is a test");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->user_agent, undef);
|
|
Packit |
a09cf7 |
is($h->user_agent("Mozilla/1.2"), undef);
|
|
Packit |
a09cf7 |
is($h->user_agent, "Mozilla/1.2");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->server, undef);
|
|
Packit |
a09cf7 |
is($h->server("Apache/2.1"), undef);
|
|
Packit |
a09cf7 |
is($h->server, "Apache/2.1");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->from("Gisle\@ActiveState.com"), undef);
|
|
Packit |
a09cf7 |
ok($h->header("from", "Gisle\@ActiveState.com"));
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->referer("http://www.example.com"), undef);
|
|
Packit |
a09cf7 |
is($h->referer, "http://www.example.com");
|
|
Packit |
a09cf7 |
is($h->referrer, "http://www.example.com");
|
|
Packit |
a09cf7 |
is($h->referer("http://www.example.com/#bar"), "http://www.example.com");
|
|
Packit |
a09cf7 |
is($h->referer, "http://www.example.com/");
|
|
Packit |
a09cf7 |
{
|
|
Packit |
a09cf7 |
require URI;
|
|
Packit |
a09cf7 |
my $u = URI->new("http://www.example.com#bar");
|
|
Packit |
a09cf7 |
$h->referer($u);
|
|
Packit |
a09cf7 |
is($u->as_string, "http://www.example.com#bar");
|
|
Packit |
a09cf7 |
is($h->referer->fragment, undef);
|
|
Packit |
a09cf7 |
is($h->referrer->as_string, "http://www.example.com");
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
From: Gisle\@ActiveState.com
|
|
Packit |
a09cf7 |
Referer: http://www.example.com
|
|
Packit |
a09cf7 |
User-Agent: Mozilla/1.2
|
|
Packit |
a09cf7 |
Server: Apache/2.1
|
|
Packit |
a09cf7 |
Content-Encoding: gzip
|
|
Packit |
a09cf7 |
Content-Language: no
|
|
Packit |
a09cf7 |
Content-Type: text/html;
|
|
Packit |
a09cf7 |
charSet = "ISO-8859-1"; Foo=1
|
|
Packit |
a09cf7 |
Title: This is a test
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->clear;
|
|
Packit |
a09cf7 |
is($h->www_authenticate("foo"), undef);
|
|
Packit |
a09cf7 |
is($h->www_authenticate("bar"), "foo");
|
|
Packit |
a09cf7 |
is($h->www_authenticate, "bar");
|
|
Packit |
a09cf7 |
is($h->proxy_authenticate("foo"), undef);
|
|
Packit |
a09cf7 |
is($h->proxy_authenticate("bar"), "foo");
|
|
Packit |
a09cf7 |
is($h->proxy_authenticate, "bar");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->authorization_basic, undef);
|
|
Packit |
a09cf7 |
is($h->authorization_basic("u"), undef);
|
|
Packit |
a09cf7 |
is($h->authorization_basic("u", "p"), "u:");
|
|
Packit |
a09cf7 |
is($h->authorization_basic, "u:p");
|
|
Packit |
a09cf7 |
is(j($h->authorization_basic), "u|p");
|
|
Packit |
a09cf7 |
is($h->authorization, "Basic dTpw");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is(eval { $h->authorization_basic("u2:p") }, undef);
|
|
Packit |
a09cf7 |
ok($@);
|
|
Packit |
a09cf7 |
is(j($h->authorization_basic), "u|p");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->proxy_authorization_basic("u2", "p2"), undef);
|
|
Packit |
a09cf7 |
is(j($h->proxy_authorization_basic), "u2|p2");
|
|
Packit |
a09cf7 |
is($h->proxy_authorization, "Basic dTI6cDI=");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Authorization: Basic dTpw
|
|
Packit |
a09cf7 |
Proxy-Authorization: Basic dTI6cDI=
|
|
Packit |
a09cf7 |
Proxy-Authenticate: bar
|
|
Packit |
a09cf7 |
WWW-Authenticate: bar
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Try some bad field names
|
|
Packit |
a09cf7 |
my $file = __FILE__;
|
|
Packit |
a09cf7 |
my $line;
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
eval {
|
|
Packit |
a09cf7 |
$line = __LINE__; $h->header('foo:', 1);
|
|
Packit |
a09cf7 |
};
|
|
Packit |
a09cf7 |
like($@, qr/^Illegal field name 'foo:' at \Q$file\E line $line/);
|
|
Packit |
a09cf7 |
eval {
|
|
Packit |
a09cf7 |
$line = __LINE__; $h->header('', 2);
|
|
Packit |
a09cf7 |
};
|
|
Packit |
a09cf7 |
like($@, qr/^Illegal field name '' at \Q$file\E line $line/);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
#---- old tests below -----
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(
|
|
Packit |
a09cf7 |
mime_version => "1.0",
|
|
Packit |
a09cf7 |
content_type => "text/html"
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
$h->header(URI => "http://www.oslonett.no/");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("MIME-Version"), "1.0");
|
|
Packit |
a09cf7 |
is($h->header('Uri'), "http://www.oslonett.no/");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->header("MY-header" => "foo",
|
|
Packit |
a09cf7 |
"Date" => "somedate",
|
|
Packit |
a09cf7 |
"Accept" => ["text/plain", "image/*"],
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
$h->push_header("accept" => "audio/basic");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("date"), "somedate");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
my @accept = $h->header("accept");
|
|
Packit |
a09cf7 |
is(@accept, 3);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->remove_header("uri", "date");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
my $str = $h->as_string;
|
|
Packit |
a09cf7 |
my $lines = ($str =~ tr/\n/\n/);
|
|
Packit |
a09cf7 |
is($lines, 6);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h2 = $h->clone;
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->header("accept", "*/*");
|
|
Packit |
a09cf7 |
$h->remove_header("my-header");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
@accept = $h2->header("accept");
|
|
Packit |
a09cf7 |
is(@accept, 3);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
@accept = $h->header("accept");
|
|
Packit |
a09cf7 |
is(@accept, 1);
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Check order of headers, but first remove this one
|
|
Packit |
a09cf7 |
$h2->remove_header('mime_version');
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# and add this general header
|
|
Packit |
a09cf7 |
$h2->header(Connection => 'close');
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
my @x = ();
|
|
Packit |
a09cf7 |
$h2->scan(sub {push(@x, shift);});
|
|
Packit |
a09cf7 |
is(join(";", @x), "Connection;Accept;Accept;Accept;Content-Type;MY-Header");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Check headers with embedded newlines:
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(
|
|
Packit |
a09cf7 |
a => "foo\n\n",
|
|
Packit |
a09cf7 |
b => "foo\nbar",
|
|
Packit |
a09cf7 |
c => "foo\n\nbar\n\n",
|
|
Packit |
a09cf7 |
d => "foo\n\tbar",
|
|
Packit |
a09cf7 |
e => "foo\n bar ",
|
|
Packit |
a09cf7 |
f => "foo\n bar\n baz\nbaz",
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
is($h->as_string("<<\n"), <
|
|
Packit |
a09cf7 |
A: foo<<
|
|
Packit |
a09cf7 |
B: foo<<
|
|
Packit |
a09cf7 |
bar<<
|
|
Packit |
a09cf7 |
C: foo<<
|
|
Packit |
a09cf7 |
bar<<
|
|
Packit |
a09cf7 |
D: foo<<
|
|
Packit |
a09cf7 |
\tbar<<
|
|
Packit |
a09cf7 |
E: foo<<
|
|
Packit |
a09cf7 |
bar<<
|
|
Packit |
a09cf7 |
F: foo<<
|
|
Packit |
a09cf7 |
bar<<
|
|
Packit |
a09cf7 |
baz<<
|
|
Packit |
a09cf7 |
baz<<
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Check for attempt to send a body
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(
|
|
Packit |
a09cf7 |
a => "foo\r\n\r\nevil body" ,
|
|
Packit |
a09cf7 |
b => "foo\015\012\015\012evil body" ,
|
|
Packit |
a09cf7 |
c => "foo\x0d\x0a\x0d\x0aevil body" ,
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
is (
|
|
Packit |
a09cf7 |
$h->as_string(),
|
|
Packit |
a09cf7 |
"A: foo\r\n evil body\n".
|
|
Packit |
a09cf7 |
"B: foo\015\012 evil body\n" .
|
|
Packit |
a09cf7 |
"C: foo\x0d\x0a evil body\n" ,
|
|
Packit |
a09cf7 |
"embedded CRLF are stripped out");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Check with FALSE $HTML::Headers::TRANSLATE_UNDERSCORE
|
|
Packit |
a09cf7 |
{
|
|
Packit |
a09cf7 |
local($HTTP::Headers::TRANSLATE_UNDERSCORE);
|
|
Packit |
a09cf7 |
$HTTP::Headers::TRANSLATE_UNDERSCORE = undef; # avoid -w warning
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
$h->header(abc_abc => "foo");
|
|
Packit |
a09cf7 |
$h->header("abc-abc" => "bar");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("ABC_ABC"), "foo");
|
|
Packit |
a09cf7 |
is($h->header("ABC-ABC"),"bar");
|
|
Packit |
a09cf7 |
ok($h->remove_header("Abc_Abc"));
|
|
Packit |
a09cf7 |
ok(!defined($h->header("abc_abc")));
|
|
Packit |
a09cf7 |
is($h->header("ABC-ABC"), "bar");
|
|
Packit |
a09cf7 |
}
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# Check if objects as header values works
|
|
Packit |
a09cf7 |
require URI;
|
|
Packit |
a09cf7 |
$h->header(URI => URI->new("http://www.perl.org"));
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->header("URI")->scheme, "http");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->clear;
|
|
Packit |
a09cf7 |
is($h->as_string, "");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h->content_type("text/plain");
|
|
Packit |
a09cf7 |
$h->header(content_md5 => "dummy");
|
|
Packit |
a09cf7 |
$h->header("Content-Foo" => "foo");
|
|
Packit |
a09cf7 |
$h->header(Location => "http:", xyzzy => "plugh!");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Location: http:
|
|
Packit |
a09cf7 |
Content-MD5: dummy
|
|
Packit |
a09cf7 |
Content-Type: text/plain
|
|
Packit |
a09cf7 |
Content-Foo: foo
|
|
Packit |
a09cf7 |
Xyzzy: plugh!
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
my $c = $h->remove_content_headers;
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Location: http:
|
|
Packit |
a09cf7 |
Xyzzy: plugh!
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
is($c->as_string, <
|
|
Packit |
a09cf7 |
Content-MD5: dummy
|
|
Packit |
a09cf7 |
Content-Type: text/plain
|
|
Packit |
a09cf7 |
Content-Foo: foo
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
$h->content_type("text/plain");
|
|
Packit |
a09cf7 |
$h->header(":foo_bar", 1);
|
|
Packit |
a09cf7 |
$h->push_header(":content_type", "text/html");
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), "Content-Type|:content_type|:foo_bar");
|
|
Packit |
a09cf7 |
is($h->header('Content-Type'), "text/plain");
|
|
Packit |
a09cf7 |
is($h->header(':Content_Type'), undef);
|
|
Packit |
a09cf7 |
is($h->header(':content_type'), "text/html");
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Content-Type: text/plain
|
|
Packit |
a09cf7 |
content_type: text/html
|
|
Packit |
a09cf7 |
foo_bar: 1
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
ok(!defined $h->warning('foo', 'INIT'));
|
|
Packit |
a09cf7 |
is($h->warning('bar'), 'foo');
|
|
Packit |
a09cf7 |
is($h->warning('baz', 'GET'), 'bar');
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
Warning: bar
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new;
|
|
Packit |
a09cf7 |
ok(!defined $h->header(':foo', 'bar'));
|
|
Packit |
a09cf7 |
ok(!defined $h->header(':zap', 'bang'));
|
|
Packit |
a09cf7 |
$h->push_header(':zap', ['kapow', 'shazam']);
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), ':foo|:zap');
|
|
Packit |
a09cf7 |
is(j($h->header_field_names), ':foo|:zap');
|
|
Packit |
a09cf7 |
$h->scan(sub { $_[1] .= '!' });
|
|
Packit |
a09cf7 |
is(j($h->header(':zap')), 'bang!|kapow!|shazam!');
|
|
Packit |
a09cf7 |
is(j($h->header(':foo')), 'bar');
|
|
Packit |
a09cf7 |
is($h->as_string, <
|
|
Packit |
a09cf7 |
foo: bar
|
|
Packit |
a09cf7 |
zap: bang!
|
|
Packit |
a09cf7 |
zap: kapow!
|
|
Packit |
a09cf7 |
zap: shazam!
|
|
Packit |
a09cf7 |
EOT
|
|
Packit |
a09cf7 |
is(j($h->remove_header(':zap')), 'bang!|kapow!|shazam!');
|
|
Packit |
a09cf7 |
$h->push_header(':zap', 'whomp', ':foo', 'quux');
|
|
Packit |
a09cf7 |
is(j($h->header(':foo')), 'bar|quux');
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
# [RT#30579] IE6 appens "; length = NNNN" on If-Modified-Since (can we handle it)
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new(
|
|
Packit |
a09cf7 |
if_modified_since => "Sat, 29 Oct 1994 19:43:31 GMT; length=34343"
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
is(gmtime($h->if_modified_since), "Sat Oct 29 19:43:31 1994");
|
|
Packit |
a09cf7 |
|
|
Packit |
a09cf7 |
$h = HTTP::Headers->new();
|
|
Packit |
a09cf7 |
$h->content_type('text/plain');
|
|
Packit |
a09cf7 |
$h->content_length(4);
|
|
Packit |
a09cf7 |
$h->push_header('x-foo' => 'bar');
|
|
Packit |
a09cf7 |
$h->push_header('x-foo' => 'baz');
|
|
Packit |
a09cf7 |
is(0+$h->flatten, 8);
|
|
Packit |
a09cf7 |
is_deeply(
|
|
Packit |
a09cf7 |
[ $h->flatten ],
|
|
Packit |
a09cf7 |
[
|
|
Packit |
a09cf7 |
'Content-Length',
|
|
Packit |
a09cf7 |
4,
|
|
Packit |
a09cf7 |
'Content-Type',
|
|
Packit |
a09cf7 |
'text/plain',
|
|
Packit |
a09cf7 |
'X-Foo',
|
|
Packit |
a09cf7 |
'bar',
|
|
Packit |
a09cf7 |
'X-Foo',
|
|
Packit |
a09cf7 |
'baz',
|
|
Packit |
a09cf7 |
],
|
|
Packit |
a09cf7 |
);
|
|
Packit |
a09cf7 |
|