3 use HTTP::Request::Common;
5 $r = GET 'http://www.sn.no/';
8 print "not " unless $r->method eq "GET" and $r->url eq "http://www.sn.no/";
11 $r = HEAD "http://www.sn.no/",
16 print "not " unless $r->method eq "HEAD" and $r->url->eq("http://www.sn.no");
19 print "not " unless $r->header('If-Match') eq "abc" and $r->header("from") eq "aas\@sn.no";
22 $r = PUT "http://www.sn.no",
24 print $r->as_string, "\n";
26 print "not " unless $r->method eq "PUT" and $r->uri->host eq "www.sn.no";
29 print "not " if defined($r->header("Content"));
32 print "not " unless ${$r->content_ref} eq "foo" and
33 $r->content eq "foo" and $r->content_length == 3;
36 #--- Test POST requests ---
38 $r = POST "http://www.sn.no", [foo => 'bar;baz',
44 print $r->as_string, "\n";
46 print "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";
52 print "not " unless $r->content eq "foo=bar%3Bbaz&baz=a&baz=b&baz=c&foo=zoo%3D%26&space+=+%2B+";
55 $r = POST "mailto:gisle\@aas.no",
57 Content_Type => "text/plain",
61 print "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";
68 # POST for File upload
71 open(FILE, ">$file") or die "Can't create $file: $!";
72 print FILE "foo\nbar\nbaz\n";
75 $r = POST 'http://www.perl.org/survey.cgi',
76 Content_Type => 'form-data',
77 Content => [ name => 'Gisle Aas',
78 email => 'gisle@aas.no',
85 unlink($file) or warn "Can't unlink $file: $!";
87 print "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="?([^"]+)"?/;
96 @c = split(/--\Q$boundary/, $c);
99 print "not " unless @c == 7 and $c[6] =~ /^--\n/; # 5 parts + header & trailer
102 print "not " unless $c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m and
103 $c[2] =~ /^gisle\@aas.no$/m;
106 print "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;
111 $r = POST 'http://www.perl.org/survey.cgi',
112 [ file => [ undef, "xxy\"", Content_type => "text/html", Content => "<h1>Hello, world!</h1>" ]],
113 Content_type => 'multipart/form-data';
117 print "not " unless $r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxy\\"/m and
118 $r->content =~ /^\012Content-Type: text\/html/m and
119 $r->content =~ /^\012<h1>Hello, world/m;
122 print "not " unless $r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxy\\"/m and
123 $r->content =~ /^Content-Type: text\/html/m and
124 $r->content =~ /^<h1>Hello, world/m;
129 $r = POST 'http://www.perl.org/survey.cgi',
130 Content_type => 'multipart/form-data',
131 Content => [ file => [ undef, undef, Content => "foo"]];
134 print "not " if $r->content =~ /filename=/;
138 # The POST routine can now also take a hash reference.
139 my %hash = (foo => 42, bar => 24);
140 $r = POST 'http://www.perl.org/survey.cgi', \%hash;
141 print $r->as_string, "\n";
142 print "not " unless $r->content =~ /foo=42/ &&
143 $r->content =~ /bar=24/ &&
144 $r->content_type eq "application/x-www-form-urlencoded" &&
145 $r->content_length == 13;
150 # POST for File upload
152 use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
155 open(FILE, ">$file") or die "Can't create $file: $!";
157 print FILE "a" .. "z";
161 $DYNAMIC_FILE_UPLOAD++;
162 $r = POST 'http://www.perl.org/survey.cgi',
163 Content_Type => 'form-data',
164 Content => [ name => 'Gisle Aas',
165 email => 'gisle@aas.no',
170 print $r->as_string, "\n";
172 print "not " unless $r->method eq "POST" and
173 $r->url->path eq "/survey.cgi" and
174 $r->content_type eq "multipart/form-data" and
175 $r->header(Content_type) =~ /boundary="?([^"]+)"?/ and
176 ref($r->content) eq "CODE";
180 print "not " unless length($boundary) > 10;
186 while (defined($chunk = &$code) && length $chunk) {
187 push(@chunks, $chunk);
190 unlink($file) or warn "Can't unlink $file: $!";
192 $_ = join("", @chunks);
194 print int(@chunks), " chunks, total size is ", length($_), " bytes\n";
196 # should be close to expected size and number of chunks
197 print "not " unless abs(@chunks - 15 < 3) and
198 abs(length($_) - 26589) < 20;
201 $r = POST 'http://www.example.com';
202 print "not " unless $r->as_string eq <<EOT; print "ok 20\n";
203 POST http://www.example.com
205 Content-Type: application/x-www-form-urlencoded
209 $r = POST 'http://www.example.com', Content_Type => 'form-data', Content => [];
210 print "not " unless $r->as_string eq <<EOT; print "ok 21\n";
211 POST http://www.example.com
213 Content-Type: multipart/form-data; boundary=none
217 $r = POST 'http://www.example.com', Content_Type => 'form-data';
218 #print $r->as_string;
219 print "not " unless $r->as_string eq <<EOT; print "ok 22\n";
220 POST http://www.example.com
222 Content-Type: multipart/form-data