Add ARM files
[dh-make-perl] / dev / arm / libwww-perl / libwww-perl-5.813 / t / html / form.t
1 #!perl -w
2
3 use strict;
4 use Test qw(plan ok);
5
6 plan tests => 124;
7
8 use HTML::Form;
9
10 my @warn;
11 $SIG{__WARN__} = sub { push(@warn, $_[0]) };
12
13 my @f = HTML::Form->parse("", "http://localhost/");
14 ok(@f, 0);
15
16 @f = HTML::Form->parse(<<'EOT', "http://localhost/");
17 <form action="abc" name="foo">
18 <input name="name">
19 </form>
20 <form></form>
21 EOT
22
23 ok(@f, 2);
24
25 my $f = shift @f;
26 ok($f->value("name"), "");
27 ok($f->dump, "GET http://localhost/abc [foo]\n  name=                          (text)\n");
28
29 my $req = $f->click;
30 ok($req->method, "GET");
31 ok($req->uri, "http://localhost/abc?name=");
32
33 $f->value(name => "Gisle Aas");
34 $req = $f->click;
35 ok($req->method, "GET");
36 ok($req->uri, "http://localhost/abc?name=Gisle+Aas");
37
38 ok($f->attr("name"), "foo");
39 ok($f->attr("method"), undef);
40
41 $f = shift @f;
42 ok($f->method, "GET");
43 ok($f->action, "http://localhost/");
44 ok($f->enctype, "application/x-www-form-urlencoded");
45 ok($f->dump, "GET http://localhost/\n");
46
47 # try some more advanced inputs
48 $f = HTML::Form->parse(<<'EOT', "http://localhost/");
49 <form method=post>
50    <input name=i type="image" src="foo.gif">
51    <input name=c type="checkbox" checked>
52    <input name=r type="radio" value="a">
53    <input name=r type="radio" value="b" checked>
54    <input name=t type="text">
55    <input name=p type="PASSWORD">
56    <input name=h type="hidden" value=xyzzy>
57    <input name=s type="submit" value="Doit!">
58    <input name=r type="reset">
59    <input name=b type="button">
60    <input name=f type="file" value="foo.txt">
61    <input name=x type="xyzzy">
62
63    <textarea name=a>
64 abc
65    </textarea>
66
67    <select name=s>
68       <option>Foo
69       <option value="bar" selected>Bar
70    </select>
71
72    <select name=m multiple>
73       <option selected value="a">Foo
74       <option selected value="b">Bar
75    </select>
76 </form>
77 EOT
78
79 #print $f->dump;
80 #print $f->click->as_string;
81
82 ok($f->click->as_string, <<'EOT');
83 POST http://localhost/
84 Content-Length: 76
85 Content-Type: application/x-www-form-urlencoded
86
87 i.x=1&i.y=1&c=on&r=b&t=&p=&h=xyzzy&f=foo.txt&x=&a=%0Aabc%0A+++&s=bar&m=a&m=b
88 EOT
89
90 ok(@warn, 1);
91 ok($warn[0] =~ /^Unknown input type 'xyzzy'/);
92 @warn = ();
93
94 $f = HTML::Form->parse(<<'EOT', "http://localhost/");
95 <form>
96    <input type=submit value="Upload it!" name=n disabled>
97    <input type=image alt="Foo">
98    <input type=text name=t value="1">
99 </form>
100 EOT
101
102 #$f->dump;
103 ok($f->click->as_string, <<'EOT');
104 GET http://localhost/?x=1&y=1&t=1
105
106 EOT
107
108 # test file upload
109 $f = HTML::Form->parse(<<'EOT', "http://localhost/");
110 <form method=post enctype="MULTIPART/FORM-DATA">
111    <input name=f type=file value=>
112    <input type=submit value="Upload it!">
113 </form>
114 EOT
115
116 #print $f->dump;
117 #print $f->click->as_string;
118
119 ok($f->click->as_string, <<'EOT');
120 POST http://localhost/
121 Content-Length: 0
122 Content-Type: multipart/form-data; boundary=none
123
124 EOT
125
126 my $filename = sprintf "foo-%08d.txt", $$;
127 die if -e $filename;
128
129 open(FILE, ">$filename") || die;
130 binmode(FILE);
131 print FILE "This is some text\n";
132 close(FILE) || die;
133
134 $f->value(f => $filename);
135
136 #print $f->click->as_string;
137
138 ok($f->click->as_string, <<"EOT");
139 POST http://localhost/
140 Content-Length: 139
141 Content-Type: multipart/form-data; boundary=xYzZY
142
143 --xYzZY\r
144 Content-Disposition: form-data; name="f"; filename="$filename"\r
145 Content-Type: text/plain\r
146 \r
147 This is some text
148 \r
149 --xYzZY--\r
150 EOT
151
152 unlink($filename) || warn "Can't unlink '$filename': $!";
153
154 ok(@warn, 0);
155
156 # Try to parse form HTTP::Response directly
157 {
158     package MyResponse;
159     use vars qw(@ISA);
160     require HTTP::Response;
161     @ISA = ('HTTP::Response');
162
163     sub base { "http://www.example.com" }
164 }
165 my $response = MyResponse->new(200, 'OK');
166 $response->content("<form><input type=text value=42 name=x></form>");
167
168 $f = HTML::Form->parse($response);
169
170 ok($f->click->as_string, <<"EOT");
171 GET http://www.example.com?x=42
172
173 EOT
174
175 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
176 <form>
177    <input type=checkbox name=x> I like it!
178 </form>
179 EOT
180
181 $f->find_input("x")->check;
182
183 ok($f->click->as_string, <<"EOT");
184 GET http://www.example.com?x=on
185
186 EOT
187
188 $f->value("x", "off");
189 ok($f->click->as_string, <<"EOT");
190 GET http://www.example.com
191
192 EOT
193
194 $f->value("x", "I like it!");
195 ok($f->click->as_string, <<"EOT");
196 GET http://www.example.com?x=on
197
198 EOT
199
200 $f->value("x", "I LIKE IT!");
201 ok($f->click->as_string, <<"EOT");
202 GET http://www.example.com?x=on
203
204 EOT
205
206 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
207 <form>
208 <select name=x>
209    <option value=1>one
210    <option value=2>two
211    <option>3
212 </select>
213 <select name=y multiple>
214    <option value=1>
215 </select>
216 </form>
217 EOT
218
219 $f->value("x", "one");
220
221 ok($f->click->as_string, <<"EOT");
222 GET http://www.example.com?x=1
223
224 EOT
225
226 $f->value("x", "TWO");
227 ok($f->click->as_string, <<"EOT");
228 GET http://www.example.com?x=2
229
230 EOT
231
232 ok(join(":", $f->find_input("x")->value_names), "one:two:3");
233 ok(join(":", map $_->name, $f->find_input(undef, "option")), "x:y");
234
235 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
236 <form>
237 <input name=x value=1 disabled>
238 <input name=y value=2 READONLY type=TEXT>
239 <input name=z value=3 type=hidden>
240 </form>
241 EOT
242
243 ok($f->value("x"), 1);
244 ok($f->value("y"), 2);
245 ok($f->value("z"), 3);
246 ok($f->click->uri->query, "y=2&z=3");
247
248 my $input = $f->find_input("x");
249 ok($input->type, "text");
250 ok(!$input->readonly);
251 ok($input->disabled);
252 ok($input->disabled(0));
253 ok(!$input->disabled);
254 ok($f->click->uri->query, "x=1&y=2&z=3");
255
256 $input = $f->find_input("y");
257 ok($input->type, "text");
258 ok($input->readonly);
259 ok(!$input->disabled);
260
261 $input->value(22);
262 ok($f->click->uri->query, "x=1&y=22&z=3");
263 ok(@warn, 1);
264 ok($warn[0] =~ /^Input 'y' is readonly/);
265 @warn = ();
266
267 ok($input->readonly(0));
268 ok(!$input->readonly);
269
270 $input->value(222);
271 ok(@warn, 0);
272 print @warn;
273 ok($f->click->uri->query, "x=1&y=222&z=3");
274
275 $input = $f->find_input("z");
276 ok($input->type, "hidden");
277 ok($input->readonly);
278 ok(!$input->disabled);
279
280 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
281 <form>
282 <textarea name="t" type="hidden">
283 <foo>
284 </textarea>
285 <select name=s value=s>
286  <option name=y>Foo
287  <option name=x value=bar type=x>Bar
288 </form>
289 EOT
290
291 ok($f->value("t"), "\n<foo>\n");
292 ok($f->value("s"), "Foo");
293 ok(join(":", $f->find_input("s")->possible_values), "Foo:bar");
294 ok(join(":", $f->find_input("s")->other_possible_values), "bar");
295 ok($f->value("s", "bar"), "Foo");
296 ok($f->value("s"), "bar");
297 ok(join(":", $f->find_input("s")->other_possible_values), "");
298
299
300 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
301 <form>
302
303 <input type=radio name=r0 value=1 disabled>one
304
305 <input type=radio name=r1 value=1 disabled>one
306 <input type=radio name=r1 value=2>two
307 <input type=radio name=r1 value=3>three
308
309 <input type=radio name=r2 value=1>one
310 <input type=radio name=r2 value=2 disabled>two
311 <input type=radio name=r2 value=3>three
312
313 <select name=s0>
314  <option disabled>1
315 </select>
316
317 <select name=s1>
318  <option disabled>1
319  <option>2
320  <option>3
321 </select>
322
323 <select name=s2>
324  <option>1
325  <option disabled>2
326  <option>3
327 </select>
328
329 <select name=s3 disabled>
330  <option>1
331  <option disabled>2
332  <option>3
333 </select>
334
335 <select name=m0 multiple>
336  <option disabled>1
337 </select>
338
339 <select name=m1 multiple="">
340  <option disabled>1
341  <option>2
342  <option>3
343 </select>
344
345 <select name=m2 multiple>
346  <option>1
347  <option disabled>2
348  <option>3
349 </select>
350
351 <select name=m3 disabled multiple>
352  <option>1
353  <option disabled>2
354  <option>3
355 </select>
356
357 </form>
358
359 EOT
360 #print $f->dump;
361 ok($f->find_input("r0")->disabled);
362 ok(!eval {$f->value("r0", 1);});
363 ok($@ && $@ =~ /^The value '1' has been disabled for field 'r0'/);
364 ok($f->find_input("r0")->disabled(0));
365 ok(!$f->find_input("r0")->disabled);
366 ok($f->value("r0", 1), undef);
367 ok($f->value("r0"), 1);
368
369 ok(!$f->find_input("r1")->disabled);
370 ok($f->value("r1", 2), undef);
371 ok($f->value("r1"), 2);
372 ok(!eval {$f->value("r1", 1);});
373 ok($@ && $@ =~ /^The value '1' has been disabled for field 'r1'/);
374
375 ok($f->value("r2", 1), undef);
376 ok(!eval {$f->value("r2", 2);});
377 ok($@ && $@ =~ /^The value '2' has been disabled for field 'r2'/);
378 ok(!eval {$f->value("r2", "two");});
379 ok($@ && $@ =~ /^The value 'two' has been disabled for field 'r2'/);
380 ok(!$f->find_input("r2")->disabled(1));
381 ok(!eval {$f->value("r2", 1);});
382 ok($@ && $@ =~ /^The value '1' has been disabled for field 'r2'/);
383 ok($f->find_input("r2")->disabled(0));
384 ok(!$f->find_input("r2")->disabled);
385 ok($f->value("r2", 2), 1);
386
387 ok($f->find_input("s0")->disabled);
388 ok(!$f->find_input("s1")->disabled);
389 ok(!$f->find_input("s2")->disabled);
390 ok($f->find_input("s3")->disabled);
391
392 ok(!eval {$f->value("s1", 1);});
393 ok($@ && $@ =~ /^The value '1' has been disabled for field 's1'/);
394
395 ok($f->find_input("m0")->disabled);
396 ok($f->find_input("m1", undef, 1)->disabled);
397 ok(!$f->find_input("m1", undef, 2)->disabled);
398 ok(!$f->find_input("m1", undef, 3)->disabled);
399
400 ok(!$f->find_input("m2", undef, 1)->disabled);
401 ok($f->find_input("m2", undef, 2)->disabled);
402 ok(!$f->find_input("m2", undef, 3)->disabled);
403
404 ok($f->find_input("m3", undef, 1)->disabled);
405 ok($f->find_input("m3", undef, 2)->disabled);
406 ok($f->find_input("m3", undef, 3)->disabled);
407
408 $f->find_input("m3", undef, 2)->disabled(0);
409 ok(!$f->find_input("m3", undef, 2)->disabled);
410 ok($f->find_input("m3", undef, 2)->value(2), undef);
411 ok($f->find_input("m3", undef, 2)->value(undef), 2);
412
413 $f->find_input("m3", undef, 2)->disabled(1);
414 ok($f->find_input("m3", undef, 2)->disabled);
415 ok(eval{$f->find_input("m3", undef, 2)->value(2)}, undef);
416 ok($@ && $@ =~ /^The value '2' has been disabled/);
417 ok(eval{$f->find_input("m3", undef, 2)->value(undef)}, undef);
418 ok($@ && $@ =~ /^The 'm3' field can't be unchecked/);
419
420 # multiple select with the same name [RT#18993]
421 $f = HTML::Form->parse(<<EOT, "http://localhost/");
422 <form action="target.html" method="get">
423 <select name="bug">
424 <option selected value=hi>hi
425 <option value=mom>mom
426 </select>
427 <select name="bug">
428 <option value=hi>hi
429 <option selected value=mom>mom
430 </select>
431 <select name="nobug">
432 <option value=hi>hi
433 <option selected value=mom>mom
434 </select>
435 EOT
436 ok(join("|", $f->form), "bug|hi|bug|mom|nobug|mom");
437
438 # Try a disabled radiobutton:
439 $f = HTML::Form->parse(<<EOT, "http://localhost/");
440 <form>
441  <input disabled checked type=radio name=f value=a>
442  <input type=hidden name=f value=b>
443 </form>
444
445 EOT
446
447 ok($f->click->as_string, <<'EOT');
448 GET http://localhost/?f=b
449
450 EOT
451
452 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
453 <!-- from http://www.blooberry.com/indexdot/html/tagpages/k/keygen.htm -->
454 <form  METHOD="post" ACTION="http://example.com/secure/keygen/test.cgi" ENCTYPE="application/x-www-form-urlencoded">
455    <keygen NAME="randomkey" CHALLENGE="1234567890">
456    <input TYPE="text" NAME="Field1" VALUE="Default Text">
457 </form>
458 EOT
459
460 ok($f->find_input("randomkey"));
461 ok($f->find_input("randomkey")->challenge, "1234567890");
462 ok($f->find_input("randomkey")->keytype, "rsa");
463 ok($f->click->as_string, <<EOT);
464 POST http://example.com/secure/keygen/test.cgi
465 Content-Length: 19
466 Content-Type: application/x-www-form-urlencoded
467
468 Field1=Default+Text
469 EOT
470
471 $f->value(randomkey => "foo");
472 ok($f->click->as_string, <<EOT);
473 POST http://example.com/secure/keygen/test.cgi
474 Content-Length: 33
475 Content-Type: application/x-www-form-urlencoded
476
477 randomkey=foo&Field1=Default+Text
478 EOT
479
480 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
481 <form  ACTION="http://example.com/">
482    <select name=s>
483      <option>1
484      <option>2
485    <input name=t>
486 </form>
487 EOT
488
489 ok($f);
490 ok($f->find_input("t"));
491
492
493 @f = HTML::Form->parse(<<EOT, "http://www.example.com");
494 <form  ACTION="http://example.com/">
495    <select name=s>
496      <option>1
497      <option>2
498 </form>
499 <form  ACTION="http://example.com/">
500      <input name=t>
501 </form>
502 EOT
503
504 ok(@f, 2);
505 ok($f[0]->find_input("s"));
506 ok($f[1]->find_input("t"));
507
508 $f = HTML::Form->parse(<<EOT, "http://www.example.com");
509 <form  ACTION="http://example.com/">
510   <fieldset>
511     <legend>Radio Buttons with Labels</legend>
512     <label>
513       <input type=radio name=r0 value=0 />zero
514     </label>
515     <label>one
516       <input type=radio name=r1 value=1>
517     </label>
518     <label for="r2">two</label>
519     <input type=radio name=r2 id=r2 value=2>
520     <label>
521       <span>nested</span>
522       <input type=radio name=r3 value=3>
523     </label>
524     <label>
525       before
526       and <input type=radio name=r4 value=4>
527       after
528     </label>
529   </fieldset>
530 </form>
531 EOT
532
533 ok(join(":", $f->find_input("r0")->value_names), "zero");
534 ok(join(":", $f->find_input("r1")->value_names), "one");
535 ok(join(":", $f->find_input("r2")->value_names), "two");
536 ok(join(":", $f->find_input("r3")->value_names), "nested");
537 ok(join(":", $f->find_input("r4")->value_names), "before and after");