comparison h2_request_body_preread.t @ 954:b9692ec5a08b

Tests: HTTP/2 request body preread tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 21 Jun 2016 20:46:08 +0300
parents
children e214983c605b
comparison
equal deleted inserted replaced
953:2bbab9bd73e5 954:b9692ec5a08b
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for HTTP/2 protocol with preread request body.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::HTTP2;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http http_v2 proxy limit_req shmem/);
27
28 $t->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 limit_req_zone $binary_remote_addr zone=req:1m rate=1r/s;
41
42 server {
43 listen 127.0.0.1:%%PORT_0%% http2;
44 listen 127.0.0.1:%%PORT_1%%;
45 server_name localhost;
46
47 http2_body_preread_size 10;
48
49 location /t { }
50 location / {
51 add_header X-Body $request_body;
52 proxy_pass http://127.0.0.1:%%PORT_1%%/t;
53
54 location /req {
55 limit_req zone=req burst=2;
56 proxy_pass http://127.0.0.1:%%PORT_1%%/t;
57 }
58 }
59 }
60
61 server {
62 listen 127.0.0.1:%%PORT_2%% http2;
63 server_name localhost;
64
65 http2_body_preread_size 0;
66
67 location / {
68 add_header X-Body $request_body;
69 proxy_pass http://127.0.0.1:%%PORT_1%%/t;
70
71 location /req {
72 limit_req zone=req burst=2;
73 proxy_pass http://127.0.0.1:%%PORT_1%%/t;
74 }
75 }
76 }
77
78 server {
79 listen 127.0.0.1:%%PORT_3%% http2;
80 server_name localhost;
81
82 location / {
83 add_header X-Body $request_body;
84 proxy_pass http://127.0.0.1:%%PORT_1%%/t;
85 }
86 }
87 }
88
89 EOF
90
91 $t->write_file('t', '');
92 $t->try_run('no http2_body_preread_size')->plan(8);
93
94 ###############################################################################
95
96 # request body within preread size (that is, stream window)
97
98 my $s = Test::Nginx::HTTP2->new();
99 my $sid = $s->new_stream({ body => 'TEST' });
100 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
101
102 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
103 is($frame->{headers}->{'x-body'}, 'TEST', 'within preread');
104
105 # request body beyond preread size
106 # RST_STREAM expected due stream window violation
107
108 TODO: {
109 local $TODO = 'not yet';
110
111 $s = Test::Nginx::HTTP2->new();
112 $sid = $s->new_stream({ body => 'TEST' x 10 });
113 $frames = $s->read(all => [{ type => 'RST_STREAM' }], wait => 0.5);
114
115 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
116 is($frame->{code}, 3, 'beyond preread - FLOW_CONTROL_ERROR');
117
118 }
119
120 # within preread size - limited
121
122 $s = Test::Nginx::HTTP2->new();
123 $sid = $s->new_stream({ path => '/req' });
124 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
125
126 $sid = $s->new_stream({ path => '/req', body => 'TEST' });
127 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
128
129 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
130 is($frame->{headers}->{'x-body'}, 'TEST', 'within preread limited');
131
132 # beyond preread size - limited
133
134 $s = Test::Nginx::HTTP2->new();
135 $sid = $s->new_stream({ path => '/req', body => 'TEST' x 10 });
136 $frames = $s->read(all => [{ type => 'RST_STREAM' }]);
137
138 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
139 is($frame->{code}, 3, 'beyond preread limited - FLOW_CONTROL_ERROR');
140
141
142 # zero preread size
143
144 TODO: {
145 local $TODO = 'not yet';
146
147 $s = Test::Nginx::HTTP2->new(port(2));
148 $sid = $s->new_stream({ body => 'TEST' });
149 $frames = $s->read(all => [{ type => 'RST_STREAM' }], wait => 0.5);
150
151 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
152 is($frame->{code}, 3, 'zero preread - FLOW_CONTROL_ERROR');
153
154 }
155
156 # zero preread size - limited
157
158 $s = Test::Nginx::HTTP2->new(port(2));
159 $sid = $s->new_stream({ path => '/req', body => 'TEST' });
160 $frames = $s->read(all => [{ type => 'RST_STREAM' }]);
161
162 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
163 is($frame->{code}, 3, 'zero preread limited - FLOW_CONTROL_ERROR');
164
165
166 # REFUSED_STREAM on request body prior SETTINGS acknowledgement
167
168 $s = Test::Nginx::HTTP2->new(port(0), pure => 1);
169 $sid = $s->new_stream({ body => 'TEST' });
170 $frames = $s->read(all => [{ type => 'RST_STREAM' }]);
171
172 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
173 is($frame->{code}, 7, 'no SETTINGS ack - REFUSED_STREAM');
174
175 # default preread size - no REFUSED_STREAM expected
176
177 $s = Test::Nginx::HTTP2->new(port(3), pure => 1);
178 $sid = $s->new_stream({ body => 'TEST' });
179 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
180
181 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
182 is($frame->{headers}->{'x-body'}, 'TEST', 'no SETTINGS ack - default preread');
183
184 ###############################################################################