Mercurial > hg > nginx-tests
annotate userid.t @ 1427:eb1d883305ea
Tests: avoid edge cases in upstream random two test.
Unavailable servers contribute to the number of attempts, if selected,
before the balancer would fall back to the default round-robin method.
This means that it's quite possible to get server with more connections.
To facilitate with selecting two alive servers, down server was removed
from the upstream configuration at the cost of slightly worse coverage.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Thu, 10 Jan 2019 17:42:34 +0300 |
parents | a8d18c2852ab |
children | 144c6ce732e4 |
rev | line source |
---|---|
604 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for userid filter module. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
15 use Config; |
604 | 16 use MIME::Base64; |
17 use Time::Local; | |
18 | |
19 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
20 | |
21 use lib 'lib'; | |
22 use Test::Nginx; | |
23 | |
24 ############################################################################### | |
25 | |
26 select STDERR; $| = 1; | |
27 select STDOUT; $| = 1; | |
28 | |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
29 my $t = Test::Nginx->new()->has(qw/http userid map unix/); |
604 | 30 |
31 $t->write_file_expand('nginx.conf', <<'EOF'); | |
32 | |
33 %%TEST_GLOBALS%% | |
34 | |
35 daemon off; | |
36 | |
37 events { | |
38 } | |
39 | |
40 http { | |
41 %%TEST_GLOBALS_HTTP%% | |
42 | |
43 map $args $uid_reset { | |
44 default 0; | |
45 value 1; | |
46 log log; | |
47 } | |
48 | |
49 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
50 listen 127.0.0.1:8080; |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
51 listen [::1]:%%PORT_8080%%; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
52 listen unix:%%TESTDIR%%/unix.sock; |
604 | 53 server_name localhost; |
54 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
55 add_header X-Got $uid_got; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
56 add_header X-Reset $uid_reset; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
57 add_header X-Set $uid_set; |
604 | 58 userid on; |
59 | |
60 location / { | |
1177
382011b269f1
Tests: do not inadvertently disable debug logging in userid.t.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
61 error_log %%TESTDIR%%/error.log debug; |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
62 error_log %%TESTDIR%%/error_reset.log info; |
604 | 63 } |
64 | |
65 location /name { | |
66 userid_name test; | |
67 } | |
68 | |
69 location /path { | |
70 userid_path /0123456789; | |
71 | |
72 location /path/r { | |
73 userid_path /9876543210; | |
74 } | |
75 } | |
76 | |
77 location /domain { | |
78 userid_domain test.domain; | |
79 } | |
80 | |
81 location /mark_off { | |
82 userid_mark off; | |
83 } | |
84 location /mark_eq { | |
85 userid_mark =; | |
86 } | |
87 location /mark_let { | |
88 userid_mark t; | |
89 } | |
90 location /mark_num { | |
91 userid_mark 9; | |
92 } | |
93 | |
94 location /expires_time { | |
95 add_header X-Msec $msec; | |
96 userid_expires 100; | |
97 } | |
98 location /expires_max { | |
99 userid_expires max; | |
100 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
101 location /expires_max/off { |
604 | 102 userid_expires off; |
103 } | |
104 } | |
105 location /expires_off { | |
106 userid_expires off; | |
107 } | |
108 | |
109 location /p3p { | |
110 userid_p3p policyref="/w3c/p3p.xml"; | |
111 } | |
112 | |
113 location /service { | |
114 userid_service 65534; | |
115 } | |
116 | |
117 location /cv1 { | |
118 userid v1; | |
119 userid_mark t; | |
120 } | |
121 | |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
122 location /ip6 { |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
123 userid off; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
124 proxy_pass http://[::1]:%%PORT_8080%%/; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
125 } |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
126 |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
127 location /unix { |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
128 userid off; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
129 proxy_pass http://unix:%%TESTDIR%%/unix.sock:/; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
130 } |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
131 |
604 | 132 location /clog { |
133 userid log; | |
134 } | |
135 | |
136 location /coff { | |
137 userid off; | |
138 } | |
139 } | |
140 } | |
141 | |
142 EOF | |
143 | |
144 $t->write_file('index.html', ''); | |
145 $t->write_file('expires_time', ''); | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
146 $t->write_file('service', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
147 $t->write_file('cv1', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
148 $t->write_file('clog', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
149 $t->write_file('coff', ''); |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
150 $t->try_run('no inet6 support')->plan(35); |
604 | 151 |
152 ############################################################################### | |
153 | |
154 # userid | |
155 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
156 like(http_get('/'), qr/Set-Cookie:/, 'cookie on'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
157 like(http_get('/cv1'), qr/Set-Cookie:/, 'cookie v1'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
158 unlike(http_get('/clog'), qr/Set-Cookie:/, 'cookie log'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
159 unlike(http_get('/coff'), qr/Set-Cookie:/, 'cookie off'); |
604 | 160 |
161 # default | |
162 | |
163 my %cookie = get_cookie('/'); | |
164 isnt($cookie{'uid'}, undef, 'name default'); | |
165 is($cookie{'path'}, '/', 'path default'); | |
166 is($cookie{'domain'}, undef, 'domain default'); | |
167 is($cookie{'expires'}, undef, 'expires default'); | |
168 like($cookie{'uid'}, '/\w+={0,2}$/', 'mark default'); | |
817
ada7d1ad985b
Tests: fixed occasional negative match failure in userid.t.
Sergey Kandaurov <pluknet@nginx.com>
parents:
614
diff
changeset
|
169 unlike(http_get('/'), qr/^P3P/m, 'p3p default'); |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
170 like(http_get('/'), qr/X-Reset: 0/, 'uid reset variable default'); |
604 | 171 |
172 # name, path, domain and p3p | |
173 | |
174 isnt(get_cookie('/name', 'test'), undef, 'name'); | |
175 is(get_cookie('/path', 'path'), '/0123456789', 'path'); | |
176 is(get_cookie('/domain', 'domain'), 'test.domain', 'domain'); | |
177 like(http_get('/p3p'), qr!P3P: policyref="/w3c/p3p.xml"!, 'p3p'); | |
178 | |
179 # mark | |
180 | |
181 like(get_cookie('/mark_off', 'uid'), '/\w+={0,2}$/', 'mark off'); | |
182 like(get_cookie('/mark_eq', 'uid'), '/==$/', 'mark equal'); | |
183 like(get_cookie('/mark_let', 'uid'), '/t=$/', 'mark letter'); | |
184 like(get_cookie('/mark_num', 'uid'), '/9=$/', 'mark number'); | |
185 | |
186 # expires | |
187 | |
188 my $r = http_get('/expires_time'); | |
189 my ($t1) = $r =~ /X-Msec: (\d+)/; | |
190 is(expires2timegm(cookie($r, 'expires')), $t1 + 100, 'expires time'); | |
191 is(get_cookie('/expires_max', 'expires'), 'Thu, 31-Dec-37 23:55:55 GMT', | |
192 'expires max'); | |
193 is(get_cookie('/expires_off', 'expires'), undef, 'expires off'); | |
194 | |
195 # redefinition | |
196 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
197 unlike(http_get('/expires_max/off'), qr/expires/, 'redefine expires'); |
604 | 198 like(http_get('/path/r'), qr!/9876543210!, 'redefine path'); |
199 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
200 # requests |
604 | 201 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
202 $r = http_get('/'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
203 my ($uid) = uid_set($r); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
204 isnt($uid, undef, 'uid set variable'); |
604 | 205 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
206 $r = send_uid('/', cookie($r, 'uid')); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
207 is(uid_got($r), $uid, 'uid got variable'); |
604 | 208 unlike($r, qr/Set-Cookie:/, 'same path request'); |
209 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
210 $r = send_uid('/coff', $uid); |
604 | 211 unlike($r, qr/Set-Cookie:/, 'other path request'); |
212 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
213 $r = send_uid('/?value', $uid); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
214 like($r, qr/Set-Cookie:/, 'uid reset variable value'); |
604 | 215 |
216 # service | |
217 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
218 is(substr(uid_set(http_get('/cv1')), 0, 8), '00000000', 'service default v1'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
219 |
614
0597ca82c26a
Tests: respected byteorder value on longsize=4 but USE_64_BIT_INT.
Sergey Kandaurov <pluknet@nginx.com>
parents:
605
diff
changeset
|
220 my $bigendian = $Config{byteorder} =~ '1234' ? 0 : 1; |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
221 my $addr = $bigendian ? "7F000001" : "0100007F"; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
222 is(substr(uid_set(http_get('/')), 0, 8), $addr, 'service default v2'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
223 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
224 $addr = $bigendian ? "0000FFFE" : "FEFF0000"; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
225 is(substr(uid_set(http_get('/service')), 0, 8), $addr, 'service custom'); |
604 | 226 |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
227 $addr = $bigendian ? "00000001" : "01000000"; |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
228 is(substr(uid_set(http_get('/ip6')), 0, 8), $addr, 'service ipv6'); |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
229 |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
230 TODO: { |
1426
a8d18c2852ab
Tests: userid TODO adjusted, fix committed.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1422
diff
changeset
|
231 local $TODO = 'not yet' unless $t->has_version('1.15.8'); |
1422
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
232 |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
233 is(substr(uid_set(http_get('/unix')), 0, 8), "00000000", 'service unix'); |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
234 |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
235 } |
2d7fcd713b66
Tests: added userid tests with IPv6 and unix sockets.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1177
diff
changeset
|
236 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
237 # reset log |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
238 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
239 send_uid('/?log', cookie($r, 'uid')); |
604 | 240 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
241 $t->stop(); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
242 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
243 like($t->read_file('error_reset.log'), |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
244 '/userid cookie "uid=\w+" was reset/m', 'uid reset variable log'); |
604 | 245 |
246 ############################################################################### | |
247 | |
248 sub cookie { | |
249 my ($r, $key) = @_; | |
250 my %cookie; | |
251 | |
252 $r =~ /(Set-Cookie:[^\x0d]*).*\x0d\x0a?\x0d/ms; | |
253 if ($1) { | |
254 %cookie = $1 =~ /(\w+)=([^;]+)/g; | |
255 } | |
256 | |
257 return $cookie{$key} if defined $key; | |
258 return %cookie; | |
259 } | |
260 | |
261 sub get_cookie { | |
262 my ($url, $key) = @_; | |
263 return cookie(http_get($url), $key); | |
264 } | |
265 | |
266 sub expires2timegm { | |
267 my ($e) = @_; | |
268 my %months = (Jan => 0, Feb => 1, Mar => 2, Apr => 3, May =>4, Jun => 5, | |
269 Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11); | |
270 | |
271 my ($w, $date, $time) = split(" ", $e); | |
272 my ($day, $month, $year) = split("-", $date); | |
273 my ($hour, $min, $sec) = split(":", $time); | |
274 | |
275 return timegm($sec, $min, $hour, $day, $months{$month}, $year); | |
276 } | |
277 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
278 sub uid_set { |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
279 my ($r) = @_; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
280 my ($uid) = $r =~ /X-Set: uid=(.*)\n/m; |
604 | 281 return $uid; |
282 } | |
283 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
284 sub uid_got { |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
285 my ($r) = @_; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
286 my ($uid) = $r =~ /X-Got: uid=(.*)\n/m; |
604 | 287 return $uid; |
288 } | |
289 | |
290 sub send_uid { | |
291 my ($url, $uid) = @_; | |
292 return http(<<EOF); | |
293 GET $url HTTP/1.0 | |
294 Host: localhost | |
295 Cookie: uid=$uid | |
296 | |
297 EOF | |
298 } | |
299 | |
300 ############################################################################### |