Mercurial > hg > nginx-tests
comparison h2_proxy_request_buffering.t @ 948:4dc302d8e04f
Tests: changed HTTP2 package to act as a class.
Stopped exporting any subroutines. A subset of them now act as class methods.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 17 Jun 2016 11:36:33 +0300 |
parents | d73bef563aea |
children | e9064d691790 |
comparison
equal
deleted
inserted
replaced
947:b9e42c554ba7 | 948:4dc302d8e04f |
---|---|
16 | 16 |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | 17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
18 | 18 |
19 use lib 'lib'; | 19 use lib 'lib'; |
20 use Test::Nginx; | 20 use Test::Nginx; |
21 use Test::Nginx::HTTP2 qw/ :DEFAULT :frame :io /; | 21 use Test::Nginx::HTTP2; |
22 | 22 |
23 ############################################################################### | 23 ############################################################################### |
24 | 24 |
25 select STDERR; $| = 1; | 25 select STDERR; $| = 1; |
26 select STDOUT; $| = 1; | 26 select STDOUT; $| = 1; |
174 Timeout => 3, | 174 Timeout => 3, |
175 Reuse => 1 | 175 Reuse => 1 |
176 ) | 176 ) |
177 or die "Can't create listening socket: $!\n"; | 177 or die "Can't create listening socket: $!\n"; |
178 | 178 |
179 my $sess = new_session(8080); | 179 my $s = Test::Nginx::HTTP2->new(); |
180 my $sid = exists $extra{'content-length'} | 180 my $sid = exists $extra{'content-length'} |
181 ? new_stream($sess, { headers => [ | 181 ? $s->new_stream({ headers => [ |
182 { name => ':method', value => 'GET' }, | 182 { name => ':method', value => 'GET' }, |
183 { name => ':scheme', value => 'http' }, | 183 { name => ':scheme', value => 'http' }, |
184 { name => ':path', value => $url, }, | 184 { name => ':path', value => $url, }, |
185 { name => ':authority', value => 'localhost' }, | 185 { name => ':authority', value => 'localhost' }, |
186 { name => 'content-length', | 186 { name => 'content-length', |
187 value => $extra{'content-length'} }], | 187 value => $extra{'content-length'} }], |
188 body_more => 1 }) | 188 body_more => 1 }) |
189 : new_stream($sess, { path => $url, body_more => 1 }); | 189 : $s->new_stream({ path => $url, body_more => 1 }); |
190 | 190 |
191 $client = $server->accept() or return; | 191 $client = $server->accept() or return; |
192 | 192 |
193 log2c("(new connection $client)"); | 193 log2c("(new connection $client)"); |
194 | 194 |
195 $f->{headers} = raw_read($client, '', 1, \&log2i); | 195 $f->{headers} = backend_read($client); |
196 | 196 |
197 my $chunked = $f->{headers} =~ /chunked/; | 197 my $chunked = $f->{headers} =~ /chunked/; |
198 | 198 |
199 my $body_read = sub { | 199 $f->{upload} = sub { |
200 my ($s, $buf, $len, $wait) = @_; | 200 my ($body, %extra) = @_; |
201 my $len = length($body); | |
202 my $wait = $extra{wait}; | |
203 | |
204 $s->h2_body($body, { %extra }); | |
205 | |
206 $body = ''; | |
201 | 207 |
202 for (1 .. 10) { | 208 for (1 .. 10) { |
203 $buf = raw_read($s, $buf, length($buf) + 1, \&log2i, | 209 my $buf = backend_read($client, $wait) or return ''; |
204 $wait) or return ''; | 210 $body .= $buf; |
205 | 211 |
206 my $got = 0; | 212 my $got = 0; |
207 $got += $chunked ? hex $_ : $_ for $chunked | 213 $got += $chunked ? hex $_ : $_ for $chunked |
208 ? $buf =~ /(\w+)\x0d\x0a?\w+\x0d\x0a?/g | 214 ? $body =~ /(\w+)\x0d\x0a?\w+\x0d\x0a?/g |
209 : length($buf); | 215 : length($body); |
210 last if $got >= $len; | 216 last if $got >= $len; |
211 } | 217 } |
212 | 218 |
213 return $buf; | 219 return $body; |
214 }; | |
215 | |
216 $f->{upload} = sub { | |
217 my ($body, %extra) = @_; | |
218 | |
219 h2_body($sess, $body, { %extra }); | |
220 | |
221 return $body_read->($client, '', length($body), $extra{wait}); | |
222 }; | 220 }; |
223 $f->{http_end} = sub { | 221 $f->{http_end} = sub { |
224 $client->write(<<EOF); | 222 $client->write(<<EOF); |
225 HTTP/1.1 200 OK | 223 HTTP/1.1 200 OK |
226 Connection: close | 224 Connection: close |
227 | 225 |
228 EOF | 226 EOF |
229 | 227 |
230 $client->close; | 228 $client->close; |
231 | 229 |
232 my $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | 230 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); |
233 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | 231 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
234 return $frame->{headers}->{':status'}; | 232 return $frame->{headers}->{':status'}; |
235 }; | 233 }; |
236 return $f; | 234 return $f; |
237 } | 235 } |
238 | 236 |
237 sub backend_read { | |
238 my ($s, $timo) = @_; | |
239 my $buf = ''; | |
240 | |
241 if (IO::Select->new($s)->can_read($timo || 3)) { | |
242 $s->sysread($buf, 16384) or return; | |
243 log2i($buf); | |
244 } | |
245 return $buf; | |
246 } | |
247 | |
239 sub log2i { Test::Nginx::log_core('|| <<', @_); } | 248 sub log2i { Test::Nginx::log_core('|| <<', @_); } |
240 sub log2o { Test::Nginx::log_core('|| >>', @_); } | 249 sub log2o { Test::Nginx::log_core('|| >>', @_); } |
241 sub log2c { Test::Nginx::log_core('||', @_); } | 250 sub log2c { Test::Nginx::log_core('||', @_); } |
242 | 251 |
243 ############################################################################### | 252 ############################################################################### |