comparison proxy_upgrade.t @ 277:8a41f7d38cc3

Tests: fix proxy_upgrade.t. Not it doesn't produce spurious TODO test passes on stable, and can correctly timeout connections even if nginx doesn't respond.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 03 Apr 2013 19:41:29 +0400
parents ccebd3168b5b
children c754b1c79efe
comparison
equal deleted inserted replaced
276:94f8ee2f6688 277:8a41f7d38cc3
27 27
28 select STDERR; $| = 1; 28 select STDERR; $| = 1;
29 select STDOUT; $| = 1; 29 select STDOUT; $| = 1;
30 30
31 my $t = Test::Nginx->new()->has(qw/http proxy/) 31 my $t = Test::Nginx->new()->has(qw/http proxy/)
32 ->write_file_expand('nginx.conf', <<'EOF')->plan(28); 32 ->write_file_expand('nginx.conf', <<'EOF')->plan(27);
33 33
34 %%TEST_GLOBALS%% 34 %%TEST_GLOBALS%%
35 35
36 daemon off; 36 daemon off;
37 37
114 114
115 upgrade_write($s, "foo"); 115 upgrade_write($s, "foo");
116 is(upgrade_read($s), "bar", "next to pipelined"); 116 is(upgrade_read($s), "bar", "next to pipelined");
117 } 117 }
118 118
119 }
120
119 # connection should not be upgraded unless upgrade was actually 121 # connection should not be upgraded unless upgrade was actually
120 # requested and allowed by configuration 122 # requested and allowed by configuration
121 123
122 undef $s; 124 my $s = upgrade_connect(noheader => 1);
123 $s = upgrade_connect(noheader => 1); 125 ok(!$s, "handshake noupgrade");
124 ok($s, "handshake noupgrade");
125
126 SKIP: {
127 skip "handshake failed", 1 unless $s;
128
129 upgrade_write($s, "foo");
130 isnt(upgrade_read($s), "bar", "after handshake noupgrade");
131 }
132
133 }
134 126
135 ############################################################################### 127 ###############################################################################
136 128
137 sub upgrade_connect { 129 sub upgrade_connect {
138 my (%opts) = @_; 130 my (%opts) = @_;
161 153
162 my $got = ''; 154 my $got = '';
163 $buf = ''; 155 $buf = '';
164 156
165 while (1) { 157 while (1) {
166 $buf = $s->getline(); 158 $buf = upgrade_getline($s);
167 last unless length $buf; 159 last unless length $buf;
168 log_in($buf); 160 log_in($buf);
169 $got .= $buf; 161 $got .= $buf;
170 last if $got =~ /\x0d?\x0a\x0d?\x0a$/; 162 last if $got =~ /\x0d?\x0a\x0d?\x0a$/;
171 } 163 }
172 164
173 # parse server response 165 # parse server response
174 166
175 return $s if $got =~ m!HTTP/1.1 101!; 167 return if $got !~ m!HTTP/1.1 101!;
168
169 # make sure next line is "handshaked"
170
171 return if upgrade_read($s) ne 'handshaked';
172 return $s;
173 }
174
175 sub upgrade_getline {
176 my ($s) = @_;
177 my $buf;
178
179 eval {
180 local $SIG{ALRM} = sub { die "timeout\n"; };
181 alarm(2);
182 $buf = $s->getline();
183 alarm(0);
184 };
185 alarm(0);
186
187 if ($@) {
188 log_in("died: $@");
189 return undef;
190 }
191
192 return $buf;
176 } 193 }
177 194
178 sub upgrade_write { 195 sub upgrade_write {
179 my ($s, $message) = @_; 196 my ($s, $message) = @_;
180 $s->print($message . CRLF); 197 $s->print($message . CRLF);
181 } 198 }
182 199
183 sub upgrade_read { 200 sub upgrade_read {
184 my ($s) = @_; 201 my ($s) = @_;
185 my $m = $s->getline(); 202 my $m = upgrade_getline($s);
186 $m =~ s/\x0d?\x0a// if defined $m; 203 $m =~ s/\x0d?\x0a// if defined $m;
204 log_in($m);
187 return $m; 205 return $m;
188 } 206 }
189 207
190 ############################################################################### 208 ###############################################################################
191 209
236 log2c("(handshake done)"); 254 log2c("(handshake done)");
237 255
238 $handshake = 0; 256 $handshake = 0;
239 $buffer = 'HTTP/1.1 101 Switching' . CRLF 257 $buffer = 'HTTP/1.1 101 Switching' . CRLF
240 . 'Upgrade: foo' . CRLF 258 . 'Upgrade: foo' . CRLF
241 . 'Connection: Upgrade' . CRLF . CRLF; 259 . 'Connection: Upgrade' . CRLF . CRLF
260 . 'handshaked' . CRLF;
261
262 log2o($buffer);
242 263
243 next; 264 next;
244 } 265 }
245 266
246 $unfinished .= $chunk; 267 $unfinished .= $chunk;
247 268
248 if ($unfinished =~ m/\x0d?\x0a\z/) { 269 if ($unfinished =~ m/\x0d?\x0a\z/) {
249 $unfinished =~ s/foo/bar/g; 270 $unfinished =~ s/foo/bar/g;
271 log2o($unfinished);
250 $buffer .= $unfinished; 272 $buffer .= $unfinished;
251 $unfinished = ''; 273 $unfinished = '';
252 } 274 }
253 } 275 }
254 276