Debian Codegraph @

t/headers.t

raw
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5
6 use Secret ();
7 use Test::More;
8
9 plan tests => 189;
10
11 my($h, $h2);
12 sub j { join("|", @_) }
13
14
15 require HTTP::Headers;
16 $h = HTTP::Headers->new;
17 ok($h);
18 is(ref($h), "HTTP::Headers");
19 is($h->as_string, "");
20
21 $h = HTTP::Headers->new(foo => "bar", foo => "baaaaz", Foo => "baz");
22 is($h->as_string, "Foo: bar\nFoo: baaaaz\nFoo: baz\n");
23
24 $h = HTTP::Headers->new(foo => ["bar", "baz"]);
25 is($h->as_string, "Foo: bar\nFoo: baz\n");
26
27 $h = HTTP::Headers->new(foo => 1, bar => 2, foo_bar => 3);
28 is($h->as_string, "Bar: 2\nFoo: 1\nFoo-Bar: 3\n");
29 is($h->as_string(";"), "Bar: 2;Foo: 1;Foo-Bar: 3;");
30
31 is($h->header("Foo"), 1);
32 is($h->header("FOO"), 1);
33 is(j($h->header("foo")), 1);
34 is($h->header("foo-bar"), 3);
35 is($h->header("foo_bar"), 3);
36 is($h->header("Not-There"), undef);
37 is(j($h->header("Not-There")), "");
38 is(eval { $h->header }, undef);
39 ok($@);
40
41 is($h->header("Foo", 11), 1);
42 is($h->header("Foo", [1, 1]), 11);
43 is($h->header("Foo"), "1, 1");
44 is(j($h->header("Foo")), "1|1");
45 is($h->header(foo => 11, Foo => 12, bar => 22), 2);
46 is($h->header("Foo"), "11, 12");
47 is($h->header("Bar"), 22);
48 is($h->header("Bar", undef), 22);
49 is(j($h->header("bar", 22)), "");
50
51 $h->push_header(Bar => 22);
52 is($h->header("Bar"), "22, 22");
53 $h->push_header(Bar => [23 .. 25]);
54 is($h->header("Bar"), "22, 22, 23, 24, 25");
55 is(j($h->header("Bar")), "22|22|23|24|25");
56
57 $h->clear;
58 $h->header(Foo => 1);
59 is($h->as_string, "Foo: 1\n");
60 $h->init_header(Foo => 2);
61 $h->init_header(Bar => 2);
62 is($h->as_string, "Bar: 2\nFoo: 1\n");
63 $h->init_header(Foo => [2, 3]);
64 $h->init_header(Baz => [2, 3]);
65 is($h->as_string, "Bar: 2\nBaz: 2\nBaz: 3\nFoo: 1\n");
66
67 eval { $h->init_header(A => 1, B => 2, C => 3) };
68 ok($@);
69 is($h->as_string, "Bar: 2\nBaz: 2\nBaz: 3\nFoo: 1\n");
70
71 is($h->clone->remove_header("Foo"), 1);
72 is($h->clone->remove_header("Bar"), 1);
73 is($h->clone->remove_header("Baz"), 2);
74 is($h->clone->remove_header(qw(Foo Bar Baz Not-There)), 4);
75 is($h->clone->remove_header("Not-There"), 0);
76 is(j($h->clone->remove_header("Foo")), 1);
77 is(j($h->clone->remove_header("Bar")), 2);
78 is(j($h->clone->remove_header("Baz")), "2|3");
79 is(j($h->clone->remove_header(qw(Foo Bar Baz Not-There))), "1|2|2|3");
80 is(j($h->clone->remove_header("Not-There")), "");
81
82 $h = HTTP::Headers->new(
83 allow => "GET",
84 content => "none",
85 content_type => "text/html",
86 content_md5 => "dummy",
87 content_encoding => "gzip",
88 content_foo => "bar",
89 last_modified => "yesterday",
90 expires => "tomorrow",
91 etag => "abc",
92 date => "today",
93 user_agent => "libwww-perl",
94 zoo => "foo",
95 );
96 is($h->as_string, <<EOT);
97 Date: today
98 User-Agent: libwww-perl
99 ETag: abc
100 Allow: GET
101 Content-Encoding: gzip
102 Content-MD5: dummy
103 Content-Type: text/html
104 Expires: tomorrow
105 Last-Modified: yesterday
106 Content: none
107 Content-Foo: bar
108 Zoo: foo
109 EOT
110
111 $h2 = $h->clone;
112 is($h->as_string, $h2->as_string);
113
114 is($h->remove_content_headers->as_string, <<EOT);
115 Allow: GET
116 Content-Encoding: gzip
117 Content-MD5: dummy
118 Content-Type: text/html
119 Expires: tomorrow
120 Last-Modified: yesterday
121 Content-Foo: bar
122 EOT
123
124 is($h->as_string, <<EOT);
125 Date: today
126 User-Agent: libwww-perl
127 ETag: abc
128 Content: none
129 Zoo: foo
130 EOT
131
132 # separate code path for the void context case, so test it as well
133 $h2->remove_content_headers;
134 is($h->as_string, $h2->as_string);
135
136 $h->clear;
137 is($h->as_string, "");
138 undef($h2);
139
140 $h = HTTP::Headers->new;
141 is($h->header_field_names, 0);
142 is(j($h->header_field_names), "");
143
144 $h = HTTP::Headers->new( etag => 1, foo => [2,3],
145 content_type => "text/plain");
146 is($h->header_field_names, 3);
147 is(j($h->header_field_names), "ETag|Content-Type|Foo");
148
149 {
150 my @tmp;
151 $h->scan(sub { push(@tmp, @_) });
152 is(j(@tmp), "ETag|1|Content-Type|text/plain|Foo|2|Foo|3");
153
154 @tmp = ();
155 eval { $h->scan(sub { push(@tmp, @_); die if $_[0] eq "Content-Type" }) };
156 ok($@);
157 is(j(@tmp), "ETag|1|Content-Type|text/plain");
158
159 @tmp = ();
160 $h->scan(sub { push(@tmp, @_) });
161 is(j(@tmp), "ETag|1|Content-Type|text/plain|Foo|2|Foo|3");
162 }
163
164 # CONVENIENCE METHODS
165
166 $h = HTTP::Headers->new;
167 is($h->date, undef);
168 is($h->date(time), undef);
169 is(j($h->header_field_names), "Date");
170 like($h->header("Date"), qr/^[A-Z][a-z][a-z], \d\d .* GMT$/);
171 {
172 my $off = time - $h->date;
173 ok($off == 0 || $off == 1);
174 }
175
176 if ($] < 5.006) {
177 Test::skip("Can't call variable method", 1) for 1..13;
178 }
179 else {
180 # other date fields
181 for my $field (qw(expires if_modified_since if_unmodified_since
182 last_modified))
183 {
184 eval <<'EOT'; die $@ if $@;
185 is($h->$field, undef);
186 is($h->$field(time), undef);
187 like((time - $h->$field), qr/^[01]$/);
188 EOT
189 }
190 is(j($h->header_field_names), "Date|If-Modified-Since|If-Unmodified-Since|Expires|Last-Modified");
191 }
192
193 $h->clear;
194 is($h->content_type, "");
195 is($h->content_type(""), "");
196 is($h->content_type("text/html"), "");
197 is($h->content_type, "text/html");
198 is($h->content_type(" TEXT / HTML ") , "text/html");
199 is($h->content_type, "text/html");
200 is(j($h->content_type), "text/html");
201 is($h->content_type("text/html;\n charSet = \"ISO-8859-1\"; Foo=1 "), "text/html");
202 is($h->content_type, "text/html");
203 is(j($h->content_type), "text/html|charSet = \"ISO-8859-1\"; Foo=1 ");
204 is($h->header("content_type"), "text/html;\n charSet = \"ISO-8859-1\"; Foo=1 ");
205 ok($h->content_is_html);
206 ok(!$h->content_is_xhtml);
207 ok(!$h->content_is_xml);
208 $h->content_type("application/vnd.wap.xhtml+xml");
209 ok($h->content_is_html);
210 ok($h->content_is_xhtml);
211 ok($h->content_is_xml);
212 $h->content_type("text/xml");
213 ok(!$h->content_is_html);
214 ok(!$h->content_is_xhtml);
215 ok($h->content_is_xml);
216 $h->content_type("application/xhtml+xml");
217 ok($h->content_is_html);
218 ok($h->content_is_xhtml);
219 ok($h->content_is_xml);
220 is($h->content_type("text/html;\n charSet = \"ISO-8859-1\"; Foo=1 "), "application/xhtml+xml");
221
222 is($h->content_encoding, undef);
223 is($h->content_encoding("gzip"), undef);
224 is($h->content_encoding, "gzip");
225 is(j($h->header_field_names), "Content-Encoding|Content-Type");
226
227 is($h->content_language, undef);
228 is($h->content_language("no"), undef);
229 is($h->content_language, "no");
230
231 is($h->title, undef);
232 is($h->title("This is a test"), undef);
233 is($h->title, "This is a test");
234
235 is($h->user_agent, undef);
236 is($h->user_agent("Mozilla/1.2"), undef);
237 is($h->user_agent, "Mozilla/1.2");
238
239 is($h->server, undef);
240 is($h->server("Apache/2.1"), undef);
241 is($h->server, "Apache/2.1");
242
243 is($h->from("Gisle\@ActiveState.com"), undef);
244 ok($h->header("from", "Gisle\@ActiveState.com"));
245
246 is($h->referer("http://www.example.com"), undef);
247 is($h->referer, "http://www.example.com");
248 is($h->referrer, "http://www.example.com");
249 is($h->referer("http://www.example.com/#bar"), "http://www.example.com");
250 is($h->referer, "http://www.example.com/");
251 {
252 require URI;
253 my $u = URI->new("http://www.example.com#bar");
254 $h->referer($u);
255 is($u->as_string, "http://www.example.com#bar");
256 is($h->referer->fragment, undef);
257 is($h->referrer->as_string, "http://www.example.com");
258 }
259
260 is($h->as_string, <<EOT);
261 From: Gisle\@ActiveState.com
262 Referer: http://www.example.com
263 User-Agent: Mozilla/1.2
264 Server: Apache/2.1
265 Content-Encoding: gzip
266 Content-Language: no
267 Content-Type: text/html;
268 charSet = "ISO-8859-1"; Foo=1
269 Title: This is a test
270 EOT
271
272 $h->clear;
273 is($h->www_authenticate("foo"), undef);
274 is($h->www_authenticate("bar"), "foo");
275 is($h->www_authenticate, "bar");
276 is($h->proxy_authenticate("foo"), undef);
277 is($h->proxy_authenticate("bar"), "foo");
278 is($h->proxy_authenticate, "bar");
279
280 is($h->authorization_basic, undef);
281 is($h->authorization_basic("u"), undef);
282 is($h->authorization_basic("u", "p"), "u:");
283 is($h->authorization_basic, "u:p");
284 is(j($h->authorization_basic), "u|p");
285 is($h->authorization, "Basic dTpw");
286
287 is(eval { $h->authorization_basic("u2:p") }, undef);
288 ok($@);
289 is(j($h->authorization_basic), "u|p");
290
291 is($h->proxy_authorization_basic("u2", "p2"), undef);
292 is(j($h->proxy_authorization_basic), "u2|p2");
293 is($h->proxy_authorization, "Basic dTI6cDI=");
294
295 is($h->as_string, <<EOT);
296 Authorization: Basic dTpw
297 Proxy-Authorization: Basic dTI6cDI=
298 Proxy-Authenticate: bar
299 WWW-Authenticate: bar
300 EOT
301
302 # Try some bad field names
303 my $file = __FILE__;
304 my $line;
305 $h = HTTP::Headers->new;
306 eval {
307 $line = __LINE__; $h->header('foo:', 1);
308 };
309 like($@, qr/^Illegal field name 'foo:' at \Q$file\E line $line/);
310 eval {
311 $line = __LINE__; $h->header('', 2);
312 };
313 like($@, qr/^Illegal field name '' at \Q$file\E line $line/);
314
315
316
317 #---- old tests below -----
318
319 $h = HTTP::Headers->new(
320 mime_version => "1.0",
321 content_type => "text/html"
322 );
323 $h->header(URI => "http://www.oslonett.no/");
324
325 is($h->header("MIME-Version"), "1.0");
326 is($h->header('Uri'), "http://www.oslonett.no/");
327
328 $h->header("MY-header" => "foo",
329 "Date" => "somedate",
330 "Accept" => ["text/plain", "image/*"],
331 );
332 $h->push_header("accept" => "audio/basic");
333
334 is($h->header("date"), "somedate");
335
336 my @accept = $h->header("accept");
337 is(@accept, 3);
338
339 $h->remove_header("uri", "date");
340
341 my $str = $h->as_string;
342 my $lines = ($str =~ tr/\n/\n/);
343 is($lines, 6);
344
345 $h2 = $h->clone;
346
347 $h->header("accept", "*/*");
348 $h->remove_header("my-header");
349
350 @accept = $h2->header("accept");
351 is(@accept, 3);
352
353 @accept = $h->header("accept");
354 is(@accept, 1);
355
356 # Check order of headers, but first remove this one
357 $h2->remove_header('mime_version');
358
359 # and add this general header
360 $h2->header(Connection => 'close');
361
362 my @x = ();
363 $h2->scan(sub {push(@x, shift);});
364 is(join(";", @x), "Connection;Accept;Accept;Accept;Content-Type;MY-Header");
365
366 # Check headers with embedded newlines:
367 $h = HTTP::Headers->new(
368 a => "foo\n\n",
369 b => "foo\nbar",
370 c => "foo\n\nbar\n\n",
371 d => "foo\n\tbar",
372 e => "foo\n bar ",
373 f => "foo\n bar\n baz\nbaz",
374 );
375 is($h->as_string("<<\n"), <<EOT);
376 A: foo<<
377 B: foo<<
378 bar<<
379 C: foo<<
380 bar<<
381 D: foo<<
382 \tbar<<
383 E: foo<<
384 bar<<
385 F: foo<<
386 bar<<
387 baz<<
388 baz<<
389 EOT
390
391 # Check for attempt to send a body
392 $h = HTTP::Headers->new(
393 a => "foo\r\n\r\nevil body" ,
394 b => "foo\015\012\015\012evil body" ,
395 c => "foo\x0d\x0a\x0d\x0aevil body" ,
396 );
397 is (
398 $h->as_string(),
399 "A: foo\r\n evil body\n".
400 "B: foo\015\012 evil body\n" .
401 "C: foo\x0d\x0a evil body\n" ,
402 "embedded CRLF are stripped out");
403
404 # Check with FALSE $HTML::Headers::TRANSLATE_UNDERSCORE
405 {
406 local($HTTP::Headers::TRANSLATE_UNDERSCORE);
407 $HTTP::Headers::TRANSLATE_UNDERSCORE = undef; # avoid -w warning
408
409 $h = HTTP::Headers->new;
410 $h->header(abc_abc => "foo");
411 $h->header("abc-abc" => "bar");
412
413 is($h->header("ABC_ABC"), "foo");
414 is($h->header("ABC-ABC"),"bar");
415 ok($h->remove_header("Abc_Abc"));
416 ok(!defined($h->header("abc_abc")));
417 is($h->header("ABC-ABC"), "bar");
418 }
419
420 # Check if objects as header values works
421 require URI;
422 $h->header(URI => URI->new("http://www.perl.org"));
423
424 is($h->header("URI")->scheme, "http");
425
426 $h->clear;
427 is($h->as_string, "");
428
429 $h->content_type("text/plain");
430 $h->header(content_md5 => "dummy");
431 $h->header("Content-Foo" => "foo");
432 $h->header(Location => "http:", xyzzy => "plugh!");
433
434 is($h->as_string, <<EOT);
435 Location: http:
436 Content-MD5: dummy
437 Content-Type: text/plain
438 Content-Foo: foo
439 Xyzzy: plugh!
440 EOT
441
442 my $c = $h->remove_content_headers;
443 is($h->as_string, <<EOT);
444 Location: http:
445 Xyzzy: plugh!
446 EOT
447
448 is($c->as_string, <<EOT);
449 Content-MD5: dummy
450 Content-Type: text/plain
451 Content-Foo: foo
452 EOT
453
454 $h = HTTP::Headers->new;
455 $h->content_type("text/plain");
456 $h->header(":foo_bar", 1);
457 $h->push_header(":content_type", "text/html");
458 is(j($h->header_field_names), "Content-Type|:content_type|:foo_bar");
459 is($h->header('Content-Type'), "text/plain");
460 is($h->header(':Content_Type'), undef);
461 is($h->header(':content_type'), "text/html");
462 is($h->as_string, <<EOT);
463 Content-Type: text/plain
464 content_type: text/html
465 foo_bar: 1
466 EOT
467
468 $h = HTTP::Headers->new;
469 ok(!defined $h->warning('foo', 'INIT'));
470 is($h->warning('bar'), 'foo');
471 is($h->warning('baz', 'GET'), 'bar');
472 is($h->as_string, <<EOT);
473 Warning: bar
474 EOT
475
476 $h = HTTP::Headers->new;
477 ok(!defined $h->header(':foo', 'bar'));
478 ok(!defined $h->header(':zap', 'bang'));
479 $h->push_header(':zap', ['kapow', 'shazam']);
480 is(j($h->header_field_names), ':foo|:zap');
481 is(j($h->header_field_names), ':foo|:zap');
482 $h->scan(sub { $_[1] .= '!' });
483 is(j($h->header(':zap')), 'bang!|kapow!|shazam!');
484 is(j($h->header(':foo')), 'bar');
485 is($h->as_string, <<EOT);
486 foo: bar
487 zap: bang!
488 zap: kapow!
489 zap: shazam!
490 EOT
491 is(j($h->remove_header(':zap')), 'bang!|kapow!|shazam!');
492 $h->push_header(':zap', 'whomp', ':foo', 'quux');
493 is(j($h->header(':foo')), 'bar|quux');
494
495 # [RT#30579] IE6 appends "; length = NNNN" on If-Modified-Since (can we handle it)
496 $h = HTTP::Headers->new(
497 if_modified_since => "Sat, 29 Oct 1994 19:43:31 GMT; length=34343"
498 );
499 is(gmtime($h->if_modified_since), "Sat Oct 29 19:43:31 1994");
500
501 $h = HTTP::Headers->new();
502 $h->content_type('text/plain');
503 $h->content_length(4);
504 $h->push_header('x-foo' => 'bar');
505 $h->push_header('x-foo' => 'baz');
506 is(0+$h->flatten, 8);
507 is_deeply(
508 [ $h->flatten ],
509 [
510 'Content-Length',
511 4,
512 'Content-Type',
513 'text/plain',
514 'X-Foo',
515 'bar',
516 'X-Foo',
517 'baz',
518 ],
519 );
520
521 subtest 'object that stringifies is a valid value' => sub {
522 my $h = HTTP::Headers->new;
523 $h->header('X-Password' => Secret->new('hunter2'));
524 my $h2 = $h->clone;
525 is($h2->as_string, "X-Password: hunter2\n", 'correct headers');
526 };