Mercurial > hg > nginx-tests
comparison proxy_upgrade.t @ 584:7d6db4ac6ab0
Tests: proxy_upgrade.t tests for bytes sent to a client.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 20 May 2015 14:43:03 +0300 |
parents | 5276aceb32a6 |
children | a9569f57da98 |
comparison
equal
deleted
inserted
replaced
583:5276aceb32a6 | 584:7d6db4ac6ab0 |
---|---|
26 | 26 |
27 select STDERR; $| = 1; | 27 select STDERR; $| = 1; |
28 select STDOUT; $| = 1; | 28 select STDOUT; $| = 1; |
29 | 29 |
30 my $t = Test::Nginx->new()->has(qw/http proxy/) | 30 my $t = Test::Nginx->new()->has(qw/http proxy/) |
31 ->write_file_expand('nginx.conf', <<'EOF')->plan(27); | 31 ->write_file_expand('nginx.conf', <<'EOF')->plan(30); |
32 | 32 |
33 %%TEST_GLOBALS%% | 33 %%TEST_GLOBALS%% |
34 | 34 |
35 daemon off; | 35 daemon off; |
36 | 36 |
37 events { | 37 events { |
38 } | 38 } |
39 | 39 |
40 http { | 40 http { |
41 %%TEST_GLOBALS_HTTP%% | 41 %%TEST_GLOBALS_HTTP%% |
42 | |
43 log_format test "$bytes_sent $body_bytes_sent"; | |
44 access_log %%TESTDIR%%/cc.log test; | |
42 | 45 |
43 server { | 46 server { |
44 listen 127.0.0.1:8080; | 47 listen 127.0.0.1:8080; |
45 server_name localhost; | 48 server_name localhost; |
46 | 49 |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 EOF | 61 EOF |
59 | 62 |
63 my $d = $t->testdir(); | |
64 | |
60 $t->run_daemon(\&upgrade_fake_daemon); | 65 $t->run_daemon(\&upgrade_fake_daemon); |
61 $t->run(); | 66 $t->run(); |
62 | 67 |
63 $t->waitforsocket('127.0.0.1:8081') | 68 $t->waitforsocket('127.0.0.1:8081') |
64 or die "Can't start test backend"; | 69 or die "Can't start test backend"; |
65 | 70 |
66 ############################################################################### | 71 ############################################################################### |
67 | 72 |
68 # establish connection | 73 # establish connection |
69 | 74 |
75 my @r; | |
70 my $s = upgrade_connect(); | 76 my $s = upgrade_connect(); |
71 ok($s, "handshake"); | 77 ok($s, "handshake"); |
72 | 78 |
73 SKIP: { | 79 SKIP: { |
74 skip "handshake failed", 22 unless $s; | 80 skip "handshake failed", 22 unless $s; |
94 like(upgrade_read($s), qr/^(bar){16384}\d+$/, "upgrade $i"); | 100 like(upgrade_read($s), qr/^(bar){16384}\d+$/, "upgrade $i"); |
95 is(upgrade_read($s), 'bazz' . $i, "upgrade small $i"); | 101 is(upgrade_read($s), 'bazz' . $i, "upgrade small $i"); |
96 } | 102 } |
97 } | 103 } |
98 | 104 |
105 push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed'; | |
99 undef $s; | 106 undef $s; |
100 | 107 |
101 # establish connection with some pipelined data | 108 # establish connection with some pipelined data |
102 # and make sure they are correctly passed upstream | 109 # and make sure they are correctly passed upstream |
103 | 110 |
111 | 118 |
112 upgrade_write($s, "foo"); | 119 upgrade_write($s, "foo"); |
113 is(upgrade_read($s), "bar", "next to pipelined"); | 120 is(upgrade_read($s), "bar", "next to pipelined"); |
114 } | 121 } |
115 | 122 |
123 push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed'; | |
116 undef $s; | 124 undef $s; |
117 | 125 |
118 # connection should not be upgraded unless upgrade was actually | 126 # connection should not be upgraded unless upgrade was actually |
119 # requested and allowed by configuration | 127 # requested and allowed by configuration |
120 | 128 |
121 $s = upgrade_connect(noheader => 1); | 129 $s = upgrade_connect(noheader => 1); |
122 ok(!$s, "handshake noupgrade"); | 130 ok(!$s, "handshake noupgrade"); |
131 | |
132 # bytes sent on upgraded connection | |
133 # verify with 1) data actually read by client, 2) expected data from backend | |
134 | |
135 $t->stop(); | |
136 | |
137 open my $f, '<', "$d/cc.log" or die "Can't open cc.log: $!"; | |
138 | |
139 is($f->getline(), shift (@r) . " 540793\n", 'log - bytes'); | |
140 is($f->getline(), shift (@r) . " 22\n", 'log - bytes pipelined'); | |
141 is($f->getline(), "0 0\n", 'log - bytes noupgrade'); | |
123 | 142 |
124 ############################################################################### | 143 ############################################################################### |
125 | 144 |
126 sub upgrade_connect { | 145 sub upgrade_connect { |
127 my (%opts) = @_; | 146 my (%opts) = @_; |
173 | 192 |
174 sub upgrade_getline { | 193 sub upgrade_getline { |
175 my ($s) = @_; | 194 my ($s) = @_; |
176 my ($h, $buf, $line); | 195 my ($h, $buf, $line); |
177 | 196 |
178 ${*$s}->{_upgrade_private} ||= { b => ''}; | 197 ${*$s}->{_upgrade_private} ||= { b => '', r => 0 }; |
179 $h = ${*$s}->{_upgrade_private}; | 198 $h = ${*$s}->{_upgrade_private}; |
180 | 199 |
181 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) { | 200 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) { |
182 $h->{b} = $2; | 201 $h->{b} = $2; |
183 return $1; | 202 return $1; |
187 while (IO::Select->new($s)->can_read(1.5)) { | 206 while (IO::Select->new($s)->can_read(1.5)) { |
188 my $n = $s->sysread($buf, 1024); | 207 my $n = $s->sysread($buf, 1024); |
189 last unless $n; | 208 last unless $n; |
190 | 209 |
191 $h->{b} .= $buf; | 210 $h->{b} .= $buf; |
211 $h->{r} += $n; | |
192 | 212 |
193 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) { | 213 if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) { |
194 $h->{b} = $2; | 214 $h->{b} = $2; |
195 return $1; | 215 return $1; |
196 } | 216 } |