1 | print "1..19\n";
|
---|
2 |
|
---|
3 | use HTTP::Request::Common;
|
---|
4 |
|
---|
5 | $r = GET 'http://www.sn.no/';
|
---|
6 | print $r->as_string;
|
---|
7 |
|
---|
8 | print "not " unless $r->method eq "GET" and $r->url eq "http://www.sn.no/";
|
---|
9 | print "ok 1\n";
|
---|
10 |
|
---|
11 | $r = HEAD "http://www.sn.no/",
|
---|
12 | If_Match => 'abc',
|
---|
13 | From => '[email protected]';
|
---|
14 | print $r->as_string;
|
---|
15 |
|
---|
16 | print "not " unless $r->method eq "HEAD" and $r->url->eq("http://www.sn.no");
|
---|
17 | print "ok 2\n";
|
---|
18 |
|
---|
19 | print "not " unless $r->header('If-Match') eq "abc" and $r->header("from") eq "aas\@sn.no";
|
---|
20 | print "ok 3\n";
|
---|
21 |
|
---|
22 | $r = PUT "http://www.sn.no",
|
---|
23 | Content => 'foo';
|
---|
24 | print $r->as_string;
|
---|
25 |
|
---|
26 | print "not " unless $r->method eq "PUT" and $r->url->netloc eq "www.sn.no";
|
---|
27 | print "ok 4\n";
|
---|
28 |
|
---|
29 | print "not " if defined($r->header("Content"));
|
---|
30 | print "ok 5\n";
|
---|
31 |
|
---|
32 | print "not " unless ${$r->content_ref} eq "foo" and
|
---|
33 | $r->content eq "foo";
|
---|
34 | print "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';
|
---|
44 | print $r->as_string;
|
---|
45 |
|
---|
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";
|
---|
50 | print "ok 7\n";
|
---|
51 |
|
---|
52 | print "not " unless $r->content eq "foo=bar&baz=a&baz=b&baz=c&foo=zoo%3D%26&space%20=%20%2B%20";
|
---|
53 | print "ok 8\n";
|
---|
54 |
|
---|
55 | $r = POST "mailto:gisle\@aas.no",
|
---|
56 | Subject => "Heisan",
|
---|
57 | Content_Type => "text/plain",
|
---|
58 | Content => "Howdy\n";
|
---|
59 | print $r->as_string;
|
---|
60 |
|
---|
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";
|
---|
65 | print "ok 9\n";
|
---|
66 |
|
---|
67 | #
|
---|
68 | # POST for File upload
|
---|
69 | #
|
---|
70 | $file = "test-$$";
|
---|
71 | open(FILE, ">$file") or die "Can't create $file: $!";
|
---|
72 | print FILE "foo\nbar\nbaz\n";
|
---|
73 | close(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 | ];
|
---|
83 | print $r->as_string;
|
---|
84 |
|
---|
85 | unlink($file) or warn "Can't unlink $file: $!";
|
---|
86 |
|
---|
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="?([^"]+)"?/;
|
---|
91 | print "ok 10\n";
|
---|
92 | $boundary = $1;
|
---|
93 |
|
---|
94 | $c = $r->content;
|
---|
95 | $c =~ s/\r//g;
|
---|
96 | @c = split(/--\Q$boundary/, $c);
|
---|
97 | print "$c[5]\n";
|
---|
98 |
|
---|
99 | print "not " unless @c == 7 and $c[6] =~ /^--\n/; # 5 parts + header & trailer
|
---|
100 | print "ok 11\n";
|
---|
101 |
|
---|
102 | print "not " unless $c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m and
|
---|
103 | $c[2] =~ /^gisle\@aas.no$/m;
|
---|
104 | print "ok 12\n";
|
---|
105 |
|
---|
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;
|
---|
109 | print "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';
|
---|
114 | print $r->as_string;
|
---|
115 |
|
---|
116 | print "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;
|
---|
119 | print "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"]];
|
---|
125 | print $r->as_string;
|
---|
126 |
|
---|
127 | print "not " if $r->content =~ /filename=/;
|
---|
128 | print "ok 15\n";
|
---|
129 |
|
---|
130 |
|
---|
131 | # The POST routine can now also take a hash reference.
|
---|
132 | my %hash = (foo => 42, bar => 24);
|
---|
133 | $r = POST 'http://www.perl.org/survey.cgi', \%hash;
|
---|
134 | print $r->as_string;
|
---|
135 | print "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;
|
---|
139 | print "ok 16\n";
|
---|
140 |
|
---|
141 |
|
---|
142 | #
|
---|
143 | # POST for File upload
|
---|
144 | #
|
---|
145 | use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
|
---|
146 |
|
---|
147 | $file = "test-$$";
|
---|
148 | open(FILE, ">$file") or die "Can't create $file: $!";
|
---|
149 | for (1..1000) {
|
---|
150 | print FILE "a" .. "z";
|
---|
151 | }
|
---|
152 | close(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 | ];
|
---|
163 | print $r->as_string;
|
---|
164 |
|
---|
165 | print "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";
|
---|
170 | print "ok 17\n";
|
---|
171 | $boundary = $1;
|
---|
172 |
|
---|
173 | print "not " unless length($boundary) > 10;
|
---|
174 | print "ok 18\n";
|
---|
175 |
|
---|
176 | $code = $r->content;
|
---|
177 | my $chunk;
|
---|
178 | my @chunks;
|
---|
179 | while (defined($chunk = &$code) && length $chunk) {
|
---|
180 | push(@chunks, $chunk);
|
---|
181 | }
|
---|
182 |
|
---|
183 | unlink($file) or warn "Can't unlink $file: $!";
|
---|
184 |
|
---|
185 | $_ = join("", @chunks);
|
---|
186 |
|
---|
187 | print int(@chunks), " chunks, total size is ", length($_), " bytes\n";
|
---|
188 |
|
---|
189 | # should be close to expected size and number of chunks
|
---|
190 | print "not " unless abs(@chunks - 15 < 3) and
|
---|
191 | abs(length($_) - 26589) < 20;
|
---|
192 | print "ok 19\n";
|
---|
193 |
|
---|