source: main/trunk/greenstone2/build-src/packages/w3mir/libwww-perl-5.36/t/base/common-req.t@ 26670

Last change on this file since 26670 was 719, checked in by davidb, 25 years ago

added w3mir package

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1print "1..19\n";
2
3use HTTP::Request::Common;
4
5$r = GET 'http://www.sn.no/';
6print $r->as_string;
7
8print "not " unless $r->method eq "GET" and $r->url eq "http://www.sn.no/";
9print "ok 1\n";
10
11$r = HEAD "http://www.sn.no/",
12 If_Match => 'abc',
13 From => '[email protected]';
14print $r->as_string;
15
16print "not " unless $r->method eq "HEAD" and $r->url->eq("http://www.sn.no");
17print "ok 2\n";
18
19print "not " unless $r->header('If-Match') eq "abc" and $r->header("from") eq "aas\@sn.no";
20print "ok 3\n";
21
22$r = PUT "http://www.sn.no",
23 Content => 'foo';
24print $r->as_string;
25
26print "not " unless $r->method eq "PUT" and $r->url->netloc eq "www.sn.no";
27print "ok 4\n";
28
29print "not " if defined($r->header("Content"));
30print "ok 5\n";
31
32print "not " unless ${$r->content_ref} eq "foo" and
33 $r->content eq "foo";
34print "ok 6\n";
35
36#--- Test POST requests ---
37
38$r = POST "http://www.sn.no", [foo => 'bar',
39 baz => [qw(a b c)],
40 foo => 'zoo=&',
41 "space " => " + ",
42 ],
43 bar => 'foo';
44print $r->as_string;
45
46print "not " unless $r->method eq "POST" and
47 $r->content_type eq "application/x-www-form-urlencoded" and
48 $r->content_length == 58 and
49 $r->header("bar") eq "foo";
50print "ok 7\n";
51
52print "not " unless $r->content eq "foo=bar&baz=a&baz=b&baz=c&foo=zoo%3D%26&space%20=%20%2B%20";
53print "ok 8\n";
54
55$r = POST "mailto:gisle\@aas.no",
56 Subject => "Heisan",
57 Content_Type => "text/plain",
58 Content => "Howdy\n";
59print $r->as_string;
60
61print "not " unless $r->method eq "POST" and
62 $r->header("Subject") eq "Heisan" and
63 $r->content eq "Howdy\n" and
64 $r->content_type eq "text/plain";
65print "ok 9\n";
66
67#
68# POST for File upload
69#
70$file = "test-$$";
71open(FILE, ">$file") or die "Can't create $file: $!";
72print FILE "foo\nbar\nbaz\n";
73close(FILE);
74
75$r = POST 'http://www.perl.org/survey.cgi',
76 Content_Type => 'form-data',
77 Content => [ name => 'Gisle Aas',
78 email => '[email protected]',
79 gender => 'm',
80 born => '1964',
81 file => [$file],
82 ];
83print $r->as_string;
84
85unlink($file) or warn "Can't unlink $file: $!";
86
87print "not " unless $r->method eq "POST" and
88 $r->url->path eq "/survey.cgi" and
89 $r->content_type eq "multipart/form-data" and
90 $r->header(Content_type) =~ /boundary="?([^"]+)"?/;
91print "ok 10\n";
92$boundary = $1;
93
94$c = $r->content;
95$c =~ s/\r//g;
96@c = split(/--\Q$boundary/, $c);
97print "$c[5]\n";
98
99print "not " unless @c == 7 and $c[6] =~ /^--\n/; # 5 parts + header & trailer
100print "ok 11\n";
101
102print "not " unless $c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m and
103 $c[2] =~ /^gisle\@aas.no$/m;
104print "ok 12\n";
105
106print "not " unless $c[5] =~ /^Content-Disposition:\s*form-data;\s*name="file";\s*filename="$file"/m and
107 $c[5] =~ /^Content-Type:\s*text\/plain$/m and
108 $c[5] =~ /^foo\nbar\nbaz/m;
109print "ok 13\n";
110
111$r = POST 'http://www.perl.org/survey.cgi',
112 [ file => [ undef, "xxx", Content_type => "text/html", Content => "<h1>Hello, world!</h1>" ]],
113 Content_type => 'multipart/form-data';
114print $r->as_string;
115
116print "not " unless $r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxx"/m and
117 $r->content =~ /^Content-Type: text\/html/m and
118 $r->content =~ /^<h1>Hello, world/m;
119print "ok 14\n";
120
121
122$r = POST 'http://www.perl.org/survey.cgi',
123 Content_type => 'multipart/form-data',
124 Content => [ file => [ undef, undef, Content => "foo"]];
125print $r->as_string;
126
127print "not " if $r->content =~ /filename=/;
128print "ok 15\n";
129
130
131# The POST routine can now also take a hash reference.
132my %hash = (foo => 42, bar => 24);
133$r = POST 'http://www.perl.org/survey.cgi', \%hash;
134print $r->as_string;
135print "not " unless $r->content =~ /foo=42/ &&
136 $r->content =~ /bar=24/ &&
137 $r->content_type eq "application/x-www-form-urlencoded" &&
138 $r->content_length == 13;
139print "ok 16\n";
140
141
142#
143# POST for File upload
144#
145use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
146
147$file = "test-$$";
148open(FILE, ">$file") or die "Can't create $file: $!";
149for (1..1000) {
150 print FILE "a" .. "z";
151}
152close(FILE);
153
154$DYNAMIC_FILE_UPLOAD++;
155$r = POST 'http://www.perl.org/survey.cgi',
156 Content_Type => 'form-data',
157 Content => [ name => 'Gisle Aas',
158 email => '[email protected]',
159 gender => 'm',
160 born => '1964',
161 file => [$file],
162 ];
163print $r->as_string;
164
165print "not " unless $r->method eq "POST" and
166 $r->url->path eq "/survey.cgi" and
167 $r->content_type eq "multipart/form-data" and
168 $r->header(Content_type) =~ /boundary="?([^"]+)"?/ and
169 ref($r->content) eq "CODE";
170print "ok 17\n";
171$boundary = $1;
172
173print "not " unless length($boundary) > 10;
174print "ok 18\n";
175
176$code = $r->content;
177my $chunk;
178my @chunks;
179while (defined($chunk = &$code) && length $chunk) {
180 push(@chunks, $chunk);
181}
182
183unlink($file) or warn "Can't unlink $file: $!";
184
185$_ = join("", @chunks);
186
187print int(@chunks), " chunks, total size is ", length($_), " bytes\n";
188
189# should be close to expected size and number of chunks
190print "not " unless abs(@chunks - 15 < 3) and
191 abs(length($_) - 26589) < 20;
192print "ok 19\n";
193
Note: See TracBrowser for help on using the repository browser.