| 1 |
#! perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
plan tests => 208; |
| 9 |
|
| 10 |
require HTTP::Message; |
| 11 |
use Config qw(%Config); |
| 12 |
|
| 13 |
my($m, $m2, @parts); |
| 14 |
|
| 15 |
$m = HTTP::Message->new; |
| 16 |
ok($m); |
| 17 |
is(ref($m), "HTTP::Message"); |
| 18 |
is(ref($m->headers), "HTTP::Headers"); |
| 19 |
is($m->as_string, "\n"); |
| 20 |
is($m->headers->as_string, ""); |
| 21 |
is($m->headers_as_string, ""); |
| 22 |
is($m->content, ""); |
| 23 |
|
| 24 |
$m->header("Foo", 1); |
| 25 |
is($m->as_string, "Foo: 1\n\n"); |
| 26 |
|
| 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 |
|
| 38 |
$m2 = HTTP::Message->new($m->headers); |
| 39 |
$m2->header(bar => 2); |
| 40 |
is($m->as_string, "Foo: 1\n\n"); |
| 41 |
is($m2->as_string, "Bar: 2\nFoo: 1\n\n"); |
| 42 |
is($m2->dump, "Bar: 2\nFoo: 1\n\n(no content)\n"); |
| 43 |
is($m2->dump(no_content => ""), "Bar: 2\nFoo: 1\n\n\n"); |
| 44 |
is($m2->dump(no_content => "-"), "Bar: 2\nFoo: 1\n\n-\n"); |
| 45 |
$m2->content('0'); |
| 46 |
is($m2->dump(no_content => "-"), "Bar: 2\nFoo: 1\n\n0\n"); |
| 47 |
is($m2->dump(no_content => "0"), "Bar: 2\nFoo: 1\n\n\\x30\n"); |
| 48 |
|
| 49 |
$m2 = HTTP::Message->new($m->headers, "foo"); |
| 50 |
is($m2->as_string, "Foo: 1\n\nfoo\n"); |
| 51 |
is($m2->as_string("<<\n"), "Foo: 1<<\n<<\nfoo"); |
| 52 |
$m2 = HTTP::Message->new($m->headers, "foo\n"); |
| 53 |
is($m2->as_string, "Foo: 1\n\nfoo\n"); |
| 54 |
|
| 55 |
$m = HTTP::Message->new([a => 1, b => 2], "abc"); |
| 56 |
is($m->as_string, "A: 1\nB: 2\n\nabc\n"); |
| 57 |
|
| 58 |
$m = HTTP::Message->parse(""); |
| 59 |
is($m->as_string, "\n"); |
| 60 |
$m = HTTP::Message->parse("\n"); |
| 61 |
is($m->as_string, "\n"); |
| 62 |
$m = HTTP::Message->parse("\n\n"); |
| 63 |
is($m->as_string, "\n\n"); |
| 64 |
is($m->content, "\n"); |
| 65 |
|
| 66 |
$m = HTTP::Message->parse("foo"); |
| 67 |
is($m->as_string, "\nfoo\n"); |
| 68 |
$m = HTTP::Message->parse("foo: 1"); |
| 69 |
is($m->as_string, "Foo: 1\n\n"); |
| 70 |
$m = HTTP::Message->parse("foo_bar: 1"); |
| 71 |
is($m->as_string, "Foo_bar: 1\n\n"); |
| 72 |
$m = HTTP::Message->parse("foo: 1\n\nfoo"); |
| 73 |
is($m->as_string, "Foo: 1\n\nfoo\n"); |
| 74 |
$m = HTTP::Message->parse(<<EOT); |
| 75 |
FOO : 1 |
| 76 |
2 |
| 77 |
3 |
| 78 |
4 |
| 79 |
bar: |
| 80 |
1 |
| 81 |
Baz: 1 |
| 82 |
|
| 83 |
foobarbaz |
| 84 |
EOT |
| 85 |
is($m->as_string, <<EOT); |
| 86 |
Bar: |
| 87 |
1 |
| 88 |
Baz: 1 |
| 89 |
FOO: 1 |
| 90 |
2 |
| 91 |
3 |
| 92 |
4 |
| 93 |
|
| 94 |
foobarbaz |
| 95 |
EOT |
| 96 |
|
| 97 |
$m = HTTP::Message->parse(<<EOT); |
| 98 |
Date: Fri, 18 Feb 2005 18:33:46 GMT |
| 99 |
Connection: close |
| 100 |
Content-Type: text/plain |
| 101 |
|
| 102 |
foo:bar |
| 103 |
second line |
| 104 |
EOT |
| 105 |
is($m->content(""), <<EOT); |
| 106 |
foo:bar |
| 107 |
second line |
| 108 |
EOT |
| 109 |
is($m->as_string, <<EOT); |
| 110 |
Connection: close |
| 111 |
Date: Fri, 18 Feb 2005 18:33:46 GMT |
| 112 |
Content-Type: text/plain |
| 113 |
|
| 114 |
EOT |
| 115 |
|
| 116 |
$m = HTTP::Message->parse(" abc\nfoo: 1\n"); |
| 117 |
is($m->as_string, "\n abc\nfoo: 1\n"); |
| 118 |
$m = HTTP::Message->parse(" foo : 1\n"); |
| 119 |
is($m->as_string, "\n foo : 1\n"); |
| 120 |
$m = HTTP::Message->parse("\nfoo: bar\n"); |
| 121 |
is($m->as_string, "\nfoo: bar\n"); |
| 122 |
|
| 123 |
$m = HTTP::Message->new([a => 1, b => 2], "abc"); |
| 124 |
is($m->content("foo\n"), "abc"); |
| 125 |
is($m->content, "foo\n"); |
| 126 |
|
| 127 |
$m->add_content("bar"); |
| 128 |
is($m->content, "foo\nbar"); |
| 129 |
$m->add_content(\"\n"); |
| 130 |
is($m->content, "foo\nbar\n"); |
| 131 |
|
| 132 |
is(ref($m->content_ref), "SCALAR"); |
| 133 |
is(${$m->content_ref}, "foo\nbar\n"); |
| 134 |
${$m->content_ref} =~ s/[ao]/i/g; |
| 135 |
is($m->content, "fii\nbir\n"); |
| 136 |
|
| 137 |
$m->clear; |
| 138 |
is($m->headers->header_field_names, 0); |
| 139 |
is($m->content, ""); |
| 140 |
|
| 141 |
is($m->parts, undef); |
| 142 |
$m->parts(HTTP::Message->new, |
| 143 |
HTTP::Message->new([a => 1], "foo"), |
| 144 |
HTTP::Message->new(undef, "bar\n"), |
| 145 |
); |
| 146 |
is($m->parts->as_string, "\n"); |
| 147 |
|
| 148 |
my $str = $m->as_string; |
| 149 |
$str =~ s/\r/<CR>/g; |
| 150 |
is($str, <<EOT); |
| 151 |
Content-Type: multipart/mixed; boundary=xYzZY |
| 152 |
|
| 153 |
--xYzZY<CR> |
| 154 |
<CR> |
| 155 |
<CR> |
| 156 |
--xYzZY<CR> |
| 157 |
A: 1<CR> |
| 158 |
<CR> |
| 159 |
foo<CR> |
| 160 |
--xYzZY<CR> |
| 161 |
<CR> |
| 162 |
bar |
| 163 |
<CR> |
| 164 |
--xYzZY--<CR> |
| 165 |
EOT |
| 166 |
|
| 167 |
$m2 = HTTP::Message->new; |
| 168 |
$m2->parts($m); |
| 169 |
|
| 170 |
$str = $m2->as_string; |
| 171 |
$str =~ s/\r/<CR>/g; |
| 172 |
ok($str =~ /boundary=(\S+)/); |
| 173 |
|
| 174 |
|
| 175 |
is($str, <<EOT); |
| 176 |
Content-Type: multipart/mixed; boundary=$1 |
| 177 |
|
| 178 |
--$1<CR> |
| 179 |
Content-Type: multipart/mixed; boundary=xYzZY<CR> |
| 180 |
<CR> |
| 181 |
--xYzZY<CR> |
| 182 |
<CR> |
| 183 |
<CR> |
| 184 |
--xYzZY<CR> |
| 185 |
A: 1<CR> |
| 186 |
<CR> |
| 187 |
foo<CR> |
| 188 |
--xYzZY<CR> |
| 189 |
<CR> |
| 190 |
bar |
| 191 |
<CR> |
| 192 |
--xYzZY--<CR> |
| 193 |
<CR> |
| 194 |
--$1--<CR> |
| 195 |
EOT |
| 196 |
|
| 197 |
@parts = $m2->parts; |
| 198 |
is(@parts, 1); |
| 199 |
|
| 200 |
@parts = $parts[0]->parts; |
| 201 |
is(@parts, 3); |
| 202 |
is($parts[1]->header("A"), 1); |
| 203 |
|
| 204 |
$m2->parts([HTTP::Message->new]); |
| 205 |
@parts = $m2->parts; |
| 206 |
is(@parts, 1); |
| 207 |
|
| 208 |
$m2->parts([]); |
| 209 |
@parts = $m2->parts; |
| 210 |
is(@parts, 0); |
| 211 |
|
| 212 |
$m->clear; |
| 213 |
$m2->clear; |
| 214 |
|
| 215 |
$m = HTTP::Message->new([content_type => "message/http; boundary=aaa", |
| 216 |
], |
| 217 |
<<EOT); |
| 218 |
GET / HTTP/1.1 |
| 219 |
Host: www.example.com:8008 |
| 220 |
|
| 221 |
EOT |
| 222 |
|
| 223 |
@parts = $m->parts; |
| 224 |
is(@parts, 1); |
| 225 |
$m2 = $parts[0]; |
| 226 |
is(ref($m2), "HTTP::Request"); |
| 227 |
is($m2->method, "GET"); |
| 228 |
is($m2->uri, "/"); |
| 229 |
is($m2->protocol, "HTTP/1.1"); |
| 230 |
is($m2->header("Host"), "www.example.com:8008"); |
| 231 |
is($m2->content, ""); |
| 232 |
|
| 233 |
$m->content(<<EOT); |
| 234 |
HTTP/1.0 200 OK |
| 235 |
Content-Type: text/plain |
| 236 |
|
| 237 |
Hello |
| 238 |
EOT |
| 239 |
|
| 240 |
$m2 = $m->parts; |
| 241 |
is(ref($m2), "HTTP::Response"); |
| 242 |
is($m2->protocol, "HTTP/1.0"); |
| 243 |
is($m2->code, "200"); |
| 244 |
is($m2->message, "OK"); |
| 245 |
is($m2->content_type, "text/plain"); |
| 246 |
is($m2->content, "Hello\n"); |
| 247 |
|
| 248 |
eval { $m->parts(HTTP::Message->new, HTTP::Message->new) }; |
| 249 |
ok($@); |
| 250 |
|
| 251 |
$m->add_part(HTTP::Message->new([a=>[1..3]], "a")); |
| 252 |
$str = $m->as_string; |
| 253 |
$str =~ s/\r/<CR>/g; |
| 254 |
is($str, <<EOT); |
| 255 |
Content-Type: multipart/mixed; boundary=xYzZY |
| 256 |
|
| 257 |
--xYzZY<CR> |
| 258 |
Content-Type: message/http; boundary=aaa<CR> |
| 259 |
<CR> |
| 260 |
HTTP/1.0 200 OK |
| 261 |
Content-Type: text/plain |
| 262 |
|
| 263 |
Hello |
| 264 |
<CR> |
| 265 |
--xYzZY<CR> |
| 266 |
A: 1<CR> |
| 267 |
A: 2<CR> |
| 268 |
A: 3<CR> |
| 269 |
<CR> |
| 270 |
a<CR> |
| 271 |
--xYzZY--<CR> |
| 272 |
EOT |
| 273 |
|
| 274 |
$m->add_part(HTTP::Message->new([b=>[1..3]], "b")); |
| 275 |
|
| 276 |
$str = $m->as_string; |
| 277 |
$str =~ s/\r/<CR>/g; |
| 278 |
is($str, <<EOT); |
| 279 |
Content-Type: multipart/mixed; boundary=xYzZY |
| 280 |
|
| 281 |
--xYzZY<CR> |
| 282 |
Content-Type: message/http; boundary=aaa<CR> |
| 283 |
<CR> |
| 284 |
HTTP/1.0 200 OK |
| 285 |
Content-Type: text/plain |
| 286 |
|
| 287 |
Hello |
| 288 |
<CR> |
| 289 |
--xYzZY<CR> |
| 290 |
A: 1<CR> |
| 291 |
A: 2<CR> |
| 292 |
A: 3<CR> |
| 293 |
<CR> |
| 294 |
a<CR> |
| 295 |
--xYzZY<CR> |
| 296 |
B: 1<CR> |
| 297 |
B: 2<CR> |
| 298 |
B: 3<CR> |
| 299 |
<CR> |
| 300 |
b<CR> |
| 301 |
--xYzZY--<CR> |
| 302 |
EOT |
| 303 |
|
| 304 |
$m = HTTP::Message->new; |
| 305 |
$m->add_part(HTTP::Message->new([a=>[1..3]], "a")); |
| 306 |
is($m->header("Content-Type"), "multipart/mixed; boundary=xYzZY"); |
| 307 |
$str = $m->as_string; |
| 308 |
$str =~ s/\r/<CR>/g; |
| 309 |
is($str, <<EOT); |
| 310 |
Content-Type: multipart/mixed; boundary=xYzZY |
| 311 |
|
| 312 |
--xYzZY<CR> |
| 313 |
A: 1<CR> |
| 314 |
A: 2<CR> |
| 315 |
A: 3<CR> |
| 316 |
<CR> |
| 317 |
a<CR> |
| 318 |
--xYzZY--<CR> |
| 319 |
EOT |
| 320 |
|
| 321 |
$m = HTTP::Message->new(['Content-Type' => 'multipart/mixed']); |
| 322 |
$m->add_part(HTTP::Message->new([], 'foo and a lot more content')); |
| 323 |
is($m->header("Content-Type"), "multipart/mixed; boundary=xYzZY"); |
| 324 |
@parts = $m->parts; |
| 325 |
is($parts[0]->content, 'foo and a lot more content'); |
| 326 |
like($parts[0]->dump(maxlength => 4), qr/foo \.\.\./); |
| 327 |
like($parts[0]->dump(maxlength => 0), qr/foo and a lot/); |
| 328 |
eval { $m->encode; }; |
| 329 |
like($@, qr/Can't encode multipart/); |
| 330 |
$m->content_type('message/http'); |
| 331 |
eval { $m->encode; }; |
| 332 |
like($@, qr/Can't encode message/); |
| 333 |
|
| 334 |
$m = HTTP::Message->new; |
| 335 |
$m->content_ref(\my $foo); |
| 336 |
is($m->content_ref, \$foo); |
| 337 |
$foo = "foo"; |
| 338 |
is($m->content, "foo"); |
| 339 |
$m->add_content("bar"); |
| 340 |
is($foo, "foobar"); |
| 341 |
is($m->as_string, "\nfoobar\n"); |
| 342 |
$m->content_type("message/foo"); |
| 343 |
$m->parts(HTTP::Message->new(["h", "v"], "C")); |
| 344 |
is($foo, "H: v\r\n\r\nC"); |
| 345 |
$foo =~ s/C/c/; |
| 346 |
$m2 = $m->parts; |
| 347 |
is($m2->content, "c"); |
| 348 |
|
| 349 |
$m = HTTP::Message->new; |
| 350 |
$foo = []; |
| 351 |
$m->content($foo); |
| 352 |
is($m->content, $foo); |
| 353 |
is(${$m->content_ref}, $foo); |
| 354 |
is(${$m->content_ref([])}, $foo); |
| 355 |
isnt($m->content_ref, $foo); |
| 356 |
eval {$m->add_content("x")}; |
| 357 |
like($@, qr/^Can't append to ARRAY content/); |
| 358 |
|
| 359 |
$foo = sub { "foo" }; |
| 360 |
$m->content($foo); |
| 361 |
is($m->content, $foo); |
| 362 |
is(${$m->content_ref}, $foo); |
| 363 |
|
| 364 |
$m->content_ref($foo); |
| 365 |
is($m->content, $foo); |
| 366 |
is($m->content_ref, $foo); |
| 367 |
|
| 368 |
eval {$m->content_ref("foo")}; |
| 369 |
like($@, qr/^Setting content_ref to a non-ref/); |
| 370 |
|
| 371 |
$m->content_ref(\"foo"); |
| 372 |
eval {$m->content("bar")}; |
| 373 |
like($@, qr/^Modification of a read-only value/); |
| 374 |
|
| 375 |
$foo = "foo"; |
| 376 |
$m->content_ref(\$foo); |
| 377 |
is($m->content("bar"), "foo"); |
| 378 |
is($foo, "bar"); |
| 379 |
is($m->content, "bar"); |
| 380 |
is($m->content_ref, \$foo); |
| 381 |
|
| 382 |
$m = HTTP::Message->new; |
| 383 |
$m->content("fo=6F"); |
| 384 |
is($m->decoded_content, "fo=6F"); |
| 385 |
$m->header("Content-Encoding", "quoted-printable"); |
| 386 |
is($m->decoded_content, "foo"); |
| 387 |
|
| 388 |
for my $encoding (qw/gzip x-gzip/) { |
| 389 |
$m = HTTP::Message->new; |
| 390 |
$m->header("Content-Encoding", "$encoding, base64"); |
| 391 |
$m->content_type("text/plain; charset=UTF-8"); |
| 392 |
$m->content("H4sICFWAq0ECA3h4eAB7v3u/R6ZCSUZqUarCoxm7uAAZKHXiEAAAAA==\n"); |
| 393 |
|
| 394 |
$@ = ""; |
| 395 |
is(eval { $m->decoded_content }, "\x{FEFF}Hi there \x{263A}\n"); |
| 396 |
is($@ || "", ""); |
| 397 |
is($m->content, "H4sICFWAq0ECA3h4eAB7v3u/R6ZCSUZqUarCoxm7uAAZKHXiEAAAAA==\n"); |
| 398 |
|
| 399 |
$m2 = $m->clone; |
| 400 |
ok($m2->decode); |
| 401 |
is($m2->header("Content-Encoding"), undef); |
| 402 |
like($m2->content, qr/Hi there/); |
| 403 |
|
| 404 |
ok(grep { $_ eq "$encoding" } $m->decodable); |
| 405 |
|
| 406 |
my $tmp = MIME::Base64::decode($m->content); |
| 407 |
$m->content($tmp); |
| 408 |
$m->header("Content-Encoding", "$encoding"); |
| 409 |
$@ = ""; |
| 410 |
is(eval { $m->decoded_content }, "\x{FEFF}Hi there \x{263A}\n"); |
| 411 |
is($@ || "", ""); |
| 412 |
is($m->content, $tmp); |
| 413 |
|
| 414 |
my $m2 = HTTP::Message->new([ |
| 415 |
"Content-Type" => "text/plain", |
| 416 |
], |
| 417 |
"Hi there\n" |
| 418 |
); |
| 419 |
ok($m2->encode($encoding)); |
| 420 |
is($m2->header("Content-Encoding"), $encoding); |
| 421 |
unlike($m2->content, qr/Hi there/); |
| 422 |
is($m2->decoded_content, "Hi there\n"); |
| 423 |
ok($m2->decode); |
| 424 |
is($m2->content, "Hi there\n"); |
| 425 |
} |
| 426 |
|
| 427 |
$m->remove_header("Content-Encoding"); |
| 428 |
$m->content("a\xFF"); |
| 429 |
|
| 430 |
is($m->decoded_content, "a\x{FFFD}"); |
| 431 |
is($m->decoded_content(charset_strict => 1), undef); |
| 432 |
|
| 433 |
$m->header("Content-Encoding", "foobar"); |
| 434 |
is($m->decoded_content, undef); |
| 435 |
like($@, qr/^Don't know how to decode Content-Encoding 'foobar'/); |
| 436 |
|
| 437 |
my $err = 0; |
| 438 |
eval { |
| 439 |
$m->decoded_content(raise_error => 1); |
| 440 |
$err++; |
| 441 |
}; |
| 442 |
like($@, qr/Don't know how to decode Content-Encoding 'foobar'/); |
| 443 |
is($err, 0); |
| 444 |
|
| 445 |
eval { |
| 446 |
HTTP::Message->new([], "\x{263A}"); |
| 447 |
}; |
| 448 |
like($@, qr/bytes/); |
| 449 |
$m = HTTP::Message->new; |
| 450 |
eval { |
| 451 |
$m->add_content("\x{263A}"); |
| 452 |
}; |
| 453 |
like($@, qr/bytes/); |
| 454 |
eval { |
| 455 |
$m->content("\x{263A}"); |
| 456 |
}; |
| 457 |
like($@, qr/bytes/); |
| 458 |
|
| 459 |
# test the add_content_utf8 method |
| 460 |
$m = HTTP::Message->new(["Content-Type", "text/plain; charset=UTF-8"]); |
| 461 |
$m->add_content_utf8("\x{263A}"); |
| 462 |
$m->add_content_utf8("-\xC5"); |
| 463 |
is($m->content, "\xE2\x98\xBA-\xC3\x85"); |
| 464 |
is($m->decoded_content, "\x{263A}-\x{00C5}"); |
| 465 |
|
| 466 |
$m = HTTP::Message->new([ |
| 467 |
"Content-Type", "text/plain", |
| 468 |
], |
| 469 |
"Hello world!" |
| 470 |
); |
| 471 |
$m->content_length(length $m->content); |
| 472 |
$m->encode("deflate"); |
| 473 |
$m->dump(prefix => "# "); |
| 474 |
is($m->dump(prefix => "| "), <<'EOT'); |
| 475 |
| Content-Encoding: deflate |
| 476 |
| Content-Type: text/plain |
| 477 |
| |
| 478 |
| x\x9C\xF3H\xCD\xC9\xC9W(\xCF/\xCAIQ\4\0\35\t\4^ |
| 479 |
EOT |
| 480 |
for my $encoding (qw/identity none/) { |
| 481 |
my $m2 = $m->clone; |
| 482 |
$m2->encode("base64", $encoding); |
| 483 |
is($m2->as_string, <<"EOT"); |
| 484 |
Content-Encoding: deflate, base64, $encoding |
| 485 |
Content-Type: text/plain |
| 486 |
|
| 487 |
eJzzSM3JyVcozy/KSVEEAB0JBF4= |
| 488 |
EOT |
| 489 |
is($m2->decoded_content, "Hello world!"); |
| 490 |
} |
| 491 |
|
| 492 |
# Raw RFC 1951 deflate |
| 493 |
$m = HTTP::Message->new([ |
| 494 |
"Content-Type" => "text/plain", |
| 495 |
"Content-Encoding" => "deflate, base64", |
| 496 |
], |
| 497 |
"80jNyclXCM8vyklRBAA=" |
| 498 |
); |
| 499 |
is($m->decoded_content, "Hello World!"); |
| 500 |
ok(!$m->header("Client-Warning")); |
| 501 |
|
| 502 |
|
| 503 |
if (eval "require Compress::Raw::Bzip2") { |
| 504 |
for my $encoding (qw/x-bzip2 bzip2/) { |
| 505 |
$m = HTTP::Message->new([ |
| 506 |
"Content-Type" => "text/plain", |
| 507 |
"Content-Encoding" => "$encoding, base64", |
| 508 |
], |
| 509 |
"QlpoOTFBWSZTWcvLx0QAAAHVgAAQYAAAQAYEkIAgADEAMCBoYlnQeSEMvxdyRThQkMvLx0Q=\n" |
| 510 |
); |
| 511 |
is($m->decoded_content, "Hello world!\n"); |
| 512 |
ok($m->decode); |
| 513 |
is($m->content, "Hello world!\n"); |
| 514 |
|
| 515 |
if (eval "require IO::Compress::Bzip2") { |
| 516 |
$m = HTTP::Message->new([ |
| 517 |
"Content-Type" => "text/plain", |
| 518 |
], |
| 519 |
"Hello world!" |
| 520 |
); |
| 521 |
ok($m->encode($encoding)); |
| 522 |
is($m->header("Content-Encoding"), $encoding); |
| 523 |
like($m->content, qr/^BZh.*\0/); |
| 524 |
is($m->decoded_content, "Hello world!"); |
| 525 |
ok($m->decode); |
| 526 |
is($m->content, "Hello world!"); |
| 527 |
} |
| 528 |
else { |
| 529 |
skip("Need IO::Compress::Bzip2", undef) for 1..6; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
else { |
| 534 |
skip("Need Compress::Raw::Bzip2", undef) for 1..18; |
| 535 |
} |
| 536 |
|
| 537 |
# test decoding of XML content |
| 538 |
$m = HTTP::Message->new(["Content-Type", "application/xml"], "\xFF\xFE<\0?\0x\0m\0l\0 \0v\0e\0r\0s\0i\0o\0n\0=\0\"\x001\0.\x000\0\"\0 \0e\0n\0c\0o\0d\0i\0n\0g\0=\0\"\0U\0T\0F\0-\x001\x006\0l\0e\0\"\0?\0>\0\n\0<\0r\0o\0o\0t\0>\0\xC9\0r\0i\0c\0<\0/\0r\0o\0o\0t\0>\0\n\0"); |
| 539 |
is($m->decoded_content, "<?xml version=\"1.0\"?>\n<root>\xC9ric</root>\n"); |
| 540 |
|
| 541 |
# DESTROY is a no-op |
| 542 |
$m->DESTROY; |
| 543 |
is($m->decoded_content, "<?xml version=\"1.0\"?>\n<root>\xC9ric</root>\n"); |
| 544 |
|
| 545 |
$m = HTTP::Message->new([ |
| 546 |
"Content-Type" => "text/plain", |
| 547 |
], |
| 548 |
"Hello World!\n" |
| 549 |
); |
| 550 |
is($m->content, "Hello World!\n"); |
| 551 |
ok($m->encode()); |
| 552 |
is($m->content, "Hello World!\n"); |
| 553 |
is($m->encode("not-an-encoding"), 0); |
| 554 |
is($m->content, "Hello World!\n"); |
| 555 |
ok($m->encode("rot13")); |
| 556 |
is($m->header("Content-Encoding"), "rot13"); |
| 557 |
is($m->content, "Uryyb Jbeyq!\n"); |
| 558 |
|
| 559 |
for my $encoding (qw/compress x-compress/) { |
| 560 |
$m = HTTP::Message->new([ |
| 561 |
"Content-Type" => "text/plain", |
| 562 |
"Content-Encoding" => $encoding, |
| 563 |
], "foo"); |
| 564 |
eval { $m->decoded_content(raise_error => 1); }; |
| 565 |
like($@, qr/Can't uncompress content/); |
| 566 |
} |
| 567 |
|
| 568 |
eval { $m = HTTP::Message->new('bad-header'); }; |
| 569 |
like($@, qr/Bad header argument/); |
| 570 |
$m = HTTP::Message->new(['Content-Encoding' => 'zog']); |
| 571 |
is($m->decode, 0); |
| 572 |
$m = HTTP::Message->new; |
| 573 |
ok($m->decode); |
| 574 |
{ |
| 575 |
my @warn; |
| 576 |
local $SIG{__WARN__} = sub { push @warn, @_ }; |
| 577 |
local $^W = 0; |
| 578 |
$m->content; |
| 579 |
is($#warn, -1); |
| 580 |
local $^W = 1; |
| 581 |
$m->content; |
| 582 |
is($#warn, 0); |
| 583 |
like($warn[0], qr/Useless content call in void context/); |
| 584 |
} |
| 585 |
is($m->content(undef), ''); |
| 586 |
eval { $m->content(\'foo'); }; |
| 587 |
like($@, qr/Can't set content to be a scalar reference/); |
| 588 |
|
| 589 |
$m = HTTP::Message->new (["Content-Type" => "text/plain",], "\xEF\xBB\xBFaa/"); |
| 590 |
is($m->content_charset, "UTF-8"); |
| 591 |
$m->content("\xFF\xFE\x00\x00aa/"); |
| 592 |
is($m->content_charset, "UTF-32LE"); |
| 593 |
$m->content("\x00\x00\xFE\xFFaa/"); |
| 594 |
is($m->content_charset, "UTF-32BE"); |
| 595 |
$m->content("\xFF\xFEaa/"); |
| 596 |
is($m->content_charset, "UTF-16LE"); |
| 597 |
$m->content("\xFE\xFFaa/"); |
| 598 |
is($m->content_charset, "UTF-16BE"); |
| 599 |
|
| 600 |
{ |
| 601 |
$m = HTTP::Message->new; |
| 602 |
local $@ = 'pre-existing error'; |
| 603 |
$m->decodable; |
| 604 |
is($@, 'pre-existing error', 'decodable() does not overwrite $@'); |
| 605 |
} |
| 606 |
|
| 607 |
$m = HTTP::Message->new(["User-Agent" => "Mozilla/5.0", "Referer" => "https://example.com/"]); |
| 608 |
ok($m->can('content')); |
| 609 |
my $method = $m->can('user_agent'); |
| 610 |
is(ref($method), 'CODE'); |
| 611 |
is(HTTP::Message->can('user_agent'), $method); |
| 612 |
is($m->$method, "Mozilla/5.0"); |
| 613 |
|
| 614 |
ok(HTTP::Message->can('content')); |
| 615 |
$method = HTTP::Message->can('referrer'); |
| 616 |
is(ref($method), 'CODE'); |
| 617 |
is($m->can('referrer'), $method); |
| 618 |
is($m->$method, "https://example.com/"); |
| 619 |
|
| 620 |
eval { $m->unknown_method; }; |
| 621 |
like $@, qr/Can't locate object method "unknown_method" via package "HTTP::Message"/; |
| 622 |
is($m->can('unknown_method'), undef); |
| 623 |
eval { HTTP::Message->unknown_method; }; |
| 624 |
like $@, qr/Can't locate object method "unknown_method" via package "HTTP::Message"/; |
| 625 |
is(HTTP::Message->can('unknown_method'), undef); |
| 626 |
eval { my $empty = ""; $m->$empty; }; |
| 627 |
like $@, qr/Can't locate object method "" via package "HTTP::Message"/; |