~ubuntu-branches/ubuntu/vivid/libhtml-form-perl/vivid

« back to all changes in this revision

Viewing changes to t/form.t

  • Committer: Bazaar Package Importer
  • Author(s): Nicholas Bamber
  • Date: 2011-03-22 23:42:22 UTC
  • Revision ID: james.westby@ubuntu.com-20110322234222-dmpiumpv6bgz3ssa
Tags: upstream-6.00
ImportĀ upstreamĀ versionĀ 6.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!perl -w
 
2
 
 
3
use strict;
 
4
use Test qw(plan ok);
 
5
 
 
6
plan tests => 127;
 
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', base => "http://localhost/", verbose => 1);
 
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: 69
 
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=&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="/etc/passwd">
 
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
$input->value(22);
 
261
ok($f->click->uri->query, "x=1&y=22&z=3");
 
262
 
 
263
$input->strict(1);
 
264
eval {
 
265
    $input->value(23);
 
266
};
 
267
ok($@ =~ /^Input 'y' is readonly/);
 
268
 
 
269
ok($input->readonly(0));
 
270
ok(!$input->readonly);
 
271
 
 
272
$input->value(222);
 
273
ok(@warn, 0);
 
274
ok($f->click->uri->query, "x=1&y=222&z=3");
 
275
 
 
276
$input = $f->find_input("z");
 
277
ok($input->type, "hidden");
 
278
ok($input->readonly);
 
279
ok(!$input->disabled);
 
280
 
 
281
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
282
<form>
 
283
<textarea name="t" type="hidden">
 
284
<foo>
 
285
</textarea>
 
286
<select name=s value=s>
 
287
 <option name=y>Foo
 
288
 <option name=x value=bar type=x>Bar
 
289
</form>
 
290
EOT
 
291
 
 
292
ok($f->value("t"), "\n<foo>\n");
 
293
ok($f->value("s"), "Foo");
 
294
ok(join(":", $f->find_input("s")->possible_values), "Foo:bar");
 
295
ok(join(":", $f->find_input("s")->other_possible_values), "bar");
 
296
ok($f->value("s", "bar"), "Foo");
 
297
ok($f->value("s"), "bar");
 
298
ok(join(":", $f->find_input("s")->other_possible_values), "");
 
299
 
 
300
 
 
301
$f = HTML::Form->parse(<<EOT, base => "http://www.example.com", strict => 1);
 
302
<form>
 
303
 
 
304
<input type=radio name=r0 value=1 disabled>one
 
305
 
 
306
<input type=radio name=r1 value=1 disabled>one
 
307
<input type=radio name=r1 value=2>two
 
308
<input type=radio name=r1 value=3>three
 
309
 
 
310
<input type=radio name=r2 value=1>one
 
311
<input type=radio name=r2 value=2 disabled>two
 
312
<input type=radio name=r2 value=3>three
 
313
 
 
314
<select name=s0>
 
315
 <option disabled>1
 
316
</select>
 
317
 
 
318
<select name=s1>
 
319
 <option disabled>1
 
320
 <option>2
 
321
 <option>3
 
322
</select>
 
323
 
 
324
<select name=s2>
 
325
 <option>1
 
326
 <option disabled>2
 
327
 <option>3
 
328
</select>
 
329
 
 
330
<select name=s3 disabled>
 
331
 <option>1
 
332
 <option disabled>2
 
333
 <option>3
 
334
</select>
 
335
 
 
336
<select name=m0 multiple>
 
337
 <option disabled>1
 
338
</select>
 
339
 
 
340
<select name=m1 multiple="">
 
341
 <option disabled>1
 
342
 <option>2
 
343
 <option>3
 
344
</select>
 
345
 
 
346
<select name=m2 multiple>
 
347
 <option>1
 
348
 <option disabled>2
 
349
 <option>3
 
350
</select>
 
351
 
 
352
<select name=m3 disabled multiple>
 
353
 <option>1
 
354
 <option disabled>2
 
355
 <option>3
 
356
</select>
 
357
 
 
358
</form>
 
359
 
 
360
EOT
 
361
#print $f->dump;
 
362
ok($f->find_input("r0")->disabled);
 
363
ok(!eval {$f->value("r0", 1);});
 
364
ok($@ && $@ =~ /^The value '1' has been disabled for field 'r0'/);
 
365
ok($f->find_input("r0")->disabled(0));
 
366
ok(!$f->find_input("r0")->disabled);
 
367
ok($f->value("r0", 1), undef);
 
368
ok($f->value("r0"), 1);
 
369
 
 
370
ok(!$f->find_input("r1")->disabled);
 
371
ok($f->value("r1", 2), undef);
 
372
ok($f->value("r1"), 2);
 
373
ok(!eval {$f->value("r1", 1);});
 
374
ok($@ && $@ =~ /^The value '1' has been disabled for field 'r1'/);
 
375
 
 
376
ok($f->value("r2", 1), undef);
 
377
ok(!eval {$f->value("r2", 2);});
 
378
ok($@ && $@ =~ /^The value '2' has been disabled for field 'r2'/);
 
379
ok(!eval {$f->value("r2", "two");});
 
380
ok($@ && $@ =~ /^The value 'two' has been disabled for field 'r2'/);
 
381
ok(!$f->find_input("r2")->disabled(1));
 
382
ok(!eval {$f->value("r2", 1);});
 
383
ok($@ && $@ =~ /^The value '1' has been disabled for field 'r2'/);
 
384
ok($f->find_input("r2")->disabled(0));
 
385
ok(!$f->find_input("r2")->disabled);
 
386
ok($f->value("r2", 2), 1);
 
387
 
 
388
ok($f->find_input("s0")->disabled);
 
389
ok(!$f->find_input("s1")->disabled);
 
390
ok(!$f->find_input("s2")->disabled);
 
391
ok($f->find_input("s3")->disabled);
 
392
 
 
393
ok(!eval {$f->value("s1", 1);});
 
394
ok($@ && $@ =~ /^The value '1' has been disabled for field 's1'/);
 
395
 
 
396
ok($f->find_input("m0")->disabled);
 
397
ok($f->find_input("m1", undef, 1)->disabled);
 
398
ok(!$f->find_input("m1", undef, 2)->disabled);
 
399
ok(!$f->find_input("m1", undef, 3)->disabled);
 
400
 
 
401
ok(!$f->find_input("m2", undef, 1)->disabled);
 
402
ok($f->find_input("m2", undef, 2)->disabled);
 
403
ok(!$f->find_input("m2", undef, 3)->disabled);
 
404
 
 
405
ok($f->find_input("m3", undef, 1)->disabled);
 
406
ok($f->find_input("m3", undef, 2)->disabled);
 
407
ok($f->find_input("m3", undef, 3)->disabled);
 
408
 
 
409
$f->find_input("m3", undef, 2)->disabled(0);
 
410
ok(!$f->find_input("m3", undef, 2)->disabled);
 
411
ok($f->find_input("m3", undef, 2)->value(2), undef);
 
412
ok($f->find_input("m3", undef, 2)->value(undef), 2);
 
413
 
 
414
$f->find_input("m3", undef, 2)->disabled(1);
 
415
ok($f->find_input("m3", undef, 2)->disabled);
 
416
ok(eval{$f->find_input("m3", undef, 2)->value(2)}, undef);
 
417
ok($@ && $@ =~ /^The value '2' has been disabled/);
 
418
ok(eval{$f->find_input("m3", undef, 2)->value(undef)}, undef);
 
419
ok($@ && $@ =~ /^The 'm3' field can't be unchecked/);
 
420
 
 
421
# multiple select with the same name [RT#18993]
 
422
$f = HTML::Form->parse(<<EOT, "http://localhost/");
 
423
<form action="target.html" method="get">
 
424
<select name="bug">
 
425
<option selected value=hi>hi
 
426
<option value=mom>mom
 
427
</select>
 
428
<select name="bug">
 
429
<option value=hi>hi
 
430
<option selected value=mom>mom
 
431
</select>
 
432
<select name="nobug">
 
433
<option value=hi>hi
 
434
<option selected value=mom>mom
 
435
</select>
 
436
EOT
 
437
ok(join("|", $f->form), "bug|hi|bug|mom|nobug|mom");
 
438
 
 
439
# Try a disabled radiobutton:
 
440
$f = HTML::Form->parse(<<EOT, "http://localhost/");
 
441
<form>
 
442
 <input disabled checked type=radio name=f value=a>
 
443
 <input type=hidden name=f value=b>
 
444
</form>
 
445
 
 
446
EOT
 
447
 
 
448
ok($f->click->as_string, <<'EOT');
 
449
GET http://localhost/?f=b
 
450
 
 
451
EOT
 
452
 
 
453
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
454
<!-- from http://www.blooberry.com/indexdot/html/tagpages/k/keygen.htm -->
 
455
<form  METHOD="post" ACTION="http://example.com/secure/keygen/test.cgi" ENCTYPE="application/x-www-form-urlencoded">
 
456
   <keygen NAME="randomkey" CHALLENGE="1234567890">
 
457
   <input TYPE="text" NAME="Field1" VALUE="Default Text">
 
458
</form>
 
459
EOT
 
460
 
 
461
ok($f->find_input("randomkey"));
 
462
ok($f->find_input("randomkey")->challenge, "1234567890");
 
463
ok($f->find_input("randomkey")->keytype, "rsa");
 
464
ok($f->click->as_string, <<EOT);
 
465
POST http://example.com/secure/keygen/test.cgi
 
466
Content-Length: 19
 
467
Content-Type: application/x-www-form-urlencoded
 
468
 
 
469
Field1=Default+Text
 
470
EOT
 
471
 
 
472
$f->value(randomkey => "foo");
 
473
ok($f->click->as_string, <<EOT);
 
474
POST http://example.com/secure/keygen/test.cgi
 
475
Content-Length: 33
 
476
Content-Type: application/x-www-form-urlencoded
 
477
 
 
478
randomkey=foo&Field1=Default+Text
 
479
EOT
 
480
 
 
481
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
482
<form  ACTION="http://example.com/">
 
483
   <select name=s>
 
484
     <option>1
 
485
     <option>2
 
486
   <input name=t>
 
487
</form>
 
488
EOT
 
489
 
 
490
ok($f);
 
491
ok($f->find_input("t"));
 
492
 
 
493
 
 
494
@f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
495
<form  ACTION="http://example.com/">
 
496
   <select name=s>
 
497
     <option>1
 
498
     <option>2
 
499
</form>
 
500
<form  ACTION="http://example.com/">
 
501
     <input name=t>
 
502
</form>
 
503
EOT
 
504
 
 
505
ok(@f, 2);
 
506
ok($f[0]->find_input("s"));
 
507
ok($f[1]->find_input("t"));
 
508
 
 
509
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
510
<form  ACTION="http://example.com/">
 
511
  <fieldset>
 
512
    <legend>Radio Buttons with Labels</legend>
 
513
    <label>
 
514
      <input type=radio name=r0 value=0 />zero
 
515
    </label>
 
516
    <label>one
 
517
      <input type=radio name=r1 value=1>
 
518
    </label>
 
519
    <label for="r2">two</label>
 
520
    <input type=radio name=r2 id=r2 value=2>
 
521
    <label>
 
522
      <span>nested</span>
 
523
      <input type=radio name=r3 value=3>
 
524
    </label>
 
525
    <label>
 
526
      before
 
527
      and <input type=radio name=r4 value=4>
 
528
      after
 
529
    </label>
 
530
  </fieldset>
 
531
</form>
 
532
EOT
 
533
 
 
534
ok(join(":", $f->find_input("r0")->value_names), "zero");
 
535
ok(join(":", $f->find_input("r1")->value_names), "one");
 
536
ok(join(":", $f->find_input("r2")->value_names), "two");
 
537
ok(join(":", $f->find_input("r3")->value_names), "nested");
 
538
ok(join(":", $f->find_input("r4")->value_names), "before and after");
 
539
 
 
540
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
541
<form>
 
542
  <table>
 
543
    <TR>
 
544
      <TD align="left" colspan="2">
 
545
        &nbsp;&nbsp;&nbsp;&nbsp;Keep me informed on the progress of this election
 
546
        <INPUT type="checkbox" id="keep_informed" name="keep_informed" value="yes" checked>
 
547
      </TD>
 
548
    </TR>
 
549
    <TR>
 
550
      <TD align=left colspan=2>
 
551
        <BR><B>The place you are registered to vote:</B>
 
552
      </TD>
 
553
    </TR>
 
554
    <TR>
 
555
      <TD valign="middle" height="2" align="right">
 
556
        <A name="Note1back">County or Parish</A>
 
557
      </TD>
 
558
      <TD align="left">
 
559
        <INPUT type="text" id="reg_county" size="40" name="reg_county" value="">
 
560
      </TD>
 
561
      <TD align="left" width="10">
 
562
        <A href="#Note2" class="c2" tabindex="-1">Note&nbsp;2</A>
 
563
      </TD>
 
564
    </TR>
 
565
  </table>
 
566
</form>
 
567
EOT
 
568
ok(join(":", $f->find_input("keep_informed")->value_names), "off:");
 
569
 
 
570
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
571
<form action="test" method="post">
 
572
<select name="test">
 
573
<option value="1">One</option>
 
574
<option value="2">Two</option>
 
575
<option disabled="disabled" value="3">Three</option>
 
576
</select>
 
577
<input type="submit" name="submit" value="Go">
 
578
</form>
 
579
</body>
 
580
</html>
 
581
EOT
 
582
ok(join(":", $f->find_input("test")->possible_values), "1:2");
 
583
ok(join(":", $f->find_input("test")->other_possible_values), "2");
 
584
 
 
585
@warn = ();
 
586
$f = HTML::Form->parse(<<EOT, "http://www.example.com");
 
587
<form>
 
588
<select id="myselect">
 
589
<option>one</option>
 
590
<option>two</option>
 
591
<option>three</option>
 
592
</select>
 
593
</form>
 
594
EOT
 
595
ok(@warn, 0);