comparison h2_request_body.t @ 955:b8948aecba12

Tests: HTTP/2 request body Content-Length tests in proxied request.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 21 Jun 2016 20:46:10 +0300
parents e9064d691790
children 882267679006
comparison
equal deleted inserted replaced
954:b9692ec5a08b 955:b8948aecba12
21 ############################################################################### 21 ###############################################################################
22 22
23 select STDERR; $| = 1; 23 select STDERR; $| = 1;
24 select STDOUT; $| = 1; 24 select STDOUT; $| = 1;
25 25
26 my $t = Test::Nginx->new()->has(qw/http http_v2 proxy/)->plan(35); 26 my $t = Test::Nginx->new()->has(qw/http http_v2 proxy/)->plan(42);
27 27
28 $t->todo_alerts() unless $t->has_version('1.9.14'); 28 $t->todo_alerts() unless $t->has_version('1.9.14');
29 29
30 $t->write_file_expand('nginx.conf', <<'EOF'); 30 $t->write_file_expand('nginx.conf', <<'EOF');
31 31
42 server { 42 server {
43 listen 127.0.0.1:%%PORT_0%% http2; 43 listen 127.0.0.1:%%PORT_0%% http2;
44 listen 127.0.0.1:%%PORT_1%%; 44 listen 127.0.0.1:%%PORT_1%%;
45 server_name localhost; 45 server_name localhost;
46 46
47 location / { } 47 location / {
48 add_header X-Length $http_content_length;
49 }
48 location /proxy2/ { 50 location /proxy2/ {
49 add_header X-Body $request_body; 51 add_header X-Body $request_body;
50 add_header X-Body-File $request_body_file; 52 add_header X-Body-File $request_body_file;
51 client_body_in_file_only on; 53 client_body_in_file_only on;
52 proxy_pass http://127.0.0.1:%%PORT_1%%/; 54 proxy_pass http://127.0.0.1:%%PORT_1%%/;
77 $s->h2_body('TEST'); 79 $s->h2_body('TEST');
78 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 80 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
79 81
80 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 82 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
81 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST', 'request body'); 83 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST', 'request body');
84 is($frame->{headers}->{'x-length'}, 4, 'request body - content length');
82 85
83 # request body with padding (uses proxied response) 86 # request body with padding (uses proxied response)
84 87
85 $s = Test::Nginx::HTTP2->new(); 88 $s = Test::Nginx::HTTP2->new();
86 $sid = $s->new_stream({ path => '/proxy2/t.html', body_more => 1 }); 89 $sid = $s->new_stream({ path => '/proxy2/t.html', body_more => 1 });
88 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 91 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
89 92
90 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 93 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
91 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST', 94 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST',
92 'request body with padding'); 95 'request body with padding');
96 is($frame->{headers}->{'x-length'}, 4,
97 'request body with padding - content length');
93 98
94 $sid = $s->new_stream(); 99 $sid = $s->new_stream();
95 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 100 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
96 101
97 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 102 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
105 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 110 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
106 111
107 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 112 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
108 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST', 113 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST',
109 'request body in multiple frames'); 114 'request body in multiple frames');
115 is($frame->{headers}->{'x-length'}, 4,
116 'request body in multiple frames - content length');
110 117
111 # request body sent in multiple DATA frames, each in its own packet 118 # request body sent in multiple DATA frames, each in its own packet
112 119
113 $s = Test::Nginx::HTTP2->new(); 120 $s = Test::Nginx::HTTP2->new();
114 $sid = $s->new_stream({ path => '/proxy2/t.html', body_more => 1 }); 121 $sid = $s->new_stream({ path => '/proxy2/t.html', body_more => 1 });
118 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 125 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
119 126
120 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 127 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
121 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TESTMOREDATA', 128 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TESTMOREDATA',
122 'request body in multiple frames separately'); 129 'request body in multiple frames separately');
130 is($frame->{headers}->{'x-length'}, 12,
131 'request body in multiple frames separately - content length');
123 132
124 # request body with an empty DATA frame 133 # request body with an empty DATA frame
125 # "zero size buf in output" alerts seen 134 # "zero size buf in output" alerts seen
126 135
127 $s = Test::Nginx::HTTP2->new(); 136 $s = Test::Nginx::HTTP2->new();
129 $s->h2_body(''); 138 $s->h2_body('');
130 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); 139 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
131 140
132 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 141 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
133 is($frame->{headers}->{':status'}, 200, 'request body - empty'); 142 is($frame->{headers}->{':status'}, 200, 'request body - empty');
143 is($frame->{headers}->{'x-length'}, 0, 'request body - empty size');
134 144
135 TODO: { 145 TODO: {
136 local $TODO = 'not yet' unless $t->has_version('1.9.14'); 146 local $TODO = 'not yet' unless $t->has_version('1.9.14');
137 147
138 ok($frame->{headers}{'x-body-file'}, 'request body - empty body file'); 148 ok($frame->{headers}{'x-body-file'}, 'request body - empty body file');
142 TODO: { 152 TODO: {
143 todo_skip 'empty body file', 1 unless $frame->{headers}{'x-body-file'}; 153 todo_skip 'empty body file', 1 unless $frame->{headers}{'x-body-file'};
144 154
145 is(read_body_file($frame->{headers}{'x-body-file'}), '', 155 is(read_body_file($frame->{headers}{'x-body-file'}), '',
146 'request body - empty content'); 156 'request body - empty content');
157
158 }
159
160 # it is expected to avoid adding Content-Length for requests without body
161
162 $s = Test::Nginx::HTTP2->new();
163 $sid = $s->new_stream({ path => '/proxy2/' });
164 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
165
166 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
167 is($frame->{headers}->{':status'}, 200, 'request without body');
168
169 TODO: {
170 local $TODO = 'not yet' unless $t->has_version('1.11.2');
171
172 is($frame->{headers}->{'x-length'}, undef,
173 'request without body - content length');
147 174
148 } 175 }
149 176
150 # request body discarded 177 # request body discarded
151 # RST_STREAM with zero code received 178 # RST_STREAM with zero code received