comparison userid.t @ 605:a77f19282f63

Tests: switched to using headers to test variables in userid.t. Tests might behave incorrectly due to the delay in writing to log. Made tests stricter for different endianness. While here, improved style.
author Andrey Zelenkov <zelenkov@nginx.com>
date Thu, 11 Jun 2015 20:56:19 +0300
parents b96c4739ca85
children 0597ca82c26a
comparison
equal deleted inserted replaced
604:b96c4739ca85 605:a77f19282f63
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use Config;
15 use MIME::Base64; 16 use MIME::Base64;
16 use Time::Local; 17 use Time::Local;
17 18
18 BEGIN { use FindBin; chdir($FindBin::Bin); } 19 BEGIN { use FindBin; chdir($FindBin::Bin); }
19 20
36 events { 37 events {
37 } 38 }
38 39
39 http { 40 http {
40 %%TEST_GLOBALS_HTTP%% 41 %%TEST_GLOBALS_HTTP%%
41
42 log_format uid '$uid_got:$uid_set';
43 access_log %%TESTDIR%%/userid.log uid;
44 42
45 map $args $uid_reset { 43 map $args $uid_reset {
46 default 0; 44 default 0;
47 value 1; 45 value 1;
48 log log; 46 log log;
50 48
51 server { 49 server {
52 listen 127.0.0.1:8080; 50 listen 127.0.0.1:8080;
53 server_name localhost; 51 server_name localhost;
54 52
53 add_header X-Got $uid_got;
54 add_header X-Reset $uid_reset;
55 add_header X-Set $uid_set;
55 userid on; 56 userid on;
56 57
57 location / { 58 location / {
58 add_header X-Reset $uid_reset; 59 error_log %%TESTDIR%%/error_reset.log info;
59 } 60 }
60 61
61 location /name { 62 location /name {
62 userid_name test; 63 userid_name test;
63 } 64 }
92 userid_expires 100; 93 userid_expires 100;
93 } 94 }
94 location /expires_max { 95 location /expires_max {
95 userid_expires max; 96 userid_expires max;
96 97
97 location /expires_max/r { 98 location /expires_max/off {
98 userid_expires off; 99 userid_expires off;
99 } 100 }
100 } 101 }
101 location /expires_off { 102 location /expires_off {
102 userid_expires off; 103 userid_expires off;
127 128
128 EOF 129 EOF
129 130
130 $t->write_file('index.html', ''); 131 $t->write_file('index.html', '');
131 $t->write_file('expires_time', ''); 132 $t->write_file('expires_time', '');
133 $t->write_file('service', '');
134 $t->write_file('cv1', '');
135 $t->write_file('clog', '');
136 $t->write_file('coff', '');
132 $t->run(); 137 $t->run();
133 138
134 ############################################################################### 139 ###############################################################################
135 140
136 # userid 141 # userid
137 142
138 like(http_get('/'), qr/Set-Cookie:/, 'on cookie'); 143 like(http_get('/'), qr/Set-Cookie:/, 'cookie on');
139 like(http_get('/cv1'), qr/Set-Cookie:/, 'v1 cookie'); 144 like(http_get('/cv1'), qr/Set-Cookie:/, 'cookie v1');
140 unlike(http_get('/clog'), qr/Set-Cookie:/, 'log no cookie'); 145 unlike(http_get('/clog'), qr/Set-Cookie:/, 'cookie log');
141 unlike(http_get('/coff'), qr/Set-Cookie:/, 'off no cookie'); 146 unlike(http_get('/coff'), qr/Set-Cookie:/, 'cookie off');
142 147
143 # default 148 # default
144 149
145 my %cookie = get_cookie('/'); 150 my %cookie = get_cookie('/');
146 isnt($cookie{'uid'}, undef, 'name default'); 151 isnt($cookie{'uid'}, undef, 'name default');
147 is($cookie{'path'}, '/', 'path default'); 152 is($cookie{'path'}, '/', 'path default');
148 is($cookie{'domain'}, undef, 'domain default'); 153 is($cookie{'domain'}, undef, 'domain default');
149 is($cookie{'expires'}, undef, 'expires default'); 154 is($cookie{'expires'}, undef, 'expires default');
150 like($cookie{'uid'}, '/\w+={0,2}$/', 'mark default'); 155 like($cookie{'uid'}, '/\w+={0,2}$/', 'mark default');
151 unlike(http_get('/'), qr/P3P/, 'p3p default'); 156 unlike(http_get('/'), qr/P3P/, 'p3p default');
152 like(http_get('/'), qr/X-Reset: 0/, 'reset var default'); 157 like(http_get('/'), qr/X-Reset: 0/, 'uid reset variable default');
153 158
154 # name, path, domain and p3p 159 # name, path, domain and p3p
155 160
156 isnt(get_cookie('/name', 'test'), undef, 'name'); 161 isnt(get_cookie('/name', 'test'), undef, 'name');
157 is(get_cookie('/path', 'path'), '/0123456789', 'path'); 162 is(get_cookie('/path', 'path'), '/0123456789', 'path');
174 'expires max'); 179 'expires max');
175 is(get_cookie('/expires_off', 'expires'), undef, 'expires off'); 180 is(get_cookie('/expires_off', 'expires'), undef, 'expires off');
176 181
177 # redefinition 182 # redefinition
178 183
179 unlike(http_get('/expires_max/r'), qr/expires/, 'redefine expires'); 184 unlike(http_get('/expires_max/off'), qr/expires/, 'redefine expires');
180 like(http_get('/path/r'), qr!/9876543210!, 'redefine path'); 185 like(http_get('/path/r'), qr!/9876543210!, 'redefine path');
181 186
182 # log and requests 187 # requests
183 188
184 my $uidc = get_cookie('/', 'uid'); 189 $r = http_get('/');
185 my $uidl = last_set($t); 190 my ($uid) = uid_set($r);
186 isnt($uidl, undef, 'log uid_set'); 191 isnt($uid, undef, 'uid set variable');
187 192
188 $r = send_uid('/', $uidc); 193 $r = send_uid('/', cookie($r, 'uid'));
189 is(last_got($t), $uidl, 'log uid_got'); 194 is(uid_got($r), $uid, 'uid got variable');
190 unlike($r, qr/Set-Cookie:/, 'same path request'); 195 unlike($r, qr/Set-Cookie:/, 'same path request');
191 196
192 $r = send_uid('/coff', $uidc); 197 $r = send_uid('/coff', $uid);
193 unlike($r, qr/Set-Cookie:/, 'other path request'); 198 unlike($r, qr/Set-Cookie:/, 'other path request');
194 199
195 $r = send_uid('/?value', $uidc); 200 $r = send_uid('/?value', $uid);
196 like($r, qr/Set-Cookie:/, 'reset request'); 201 like($r, qr/Set-Cookie:/, 'uid reset variable value');
197
198 $uidc = cookie($r, 'uid');
199 $r = send_uid('/?log', $uidc);
200 isnt(last_got($t), $uidc, 'log reset request');
201 202
202 # service 203 # service
203 204
204 http_get('/cv1'); 205 is(substr(uid_set(http_get('/cv1')), 0, 8), '00000000', 'service default v1');
205 is(substr(last_set($t), 0, 8), '00000000', 'service default v1'); 206
206 207 my $bigendian = $Config{byteorder} eq '12345678' ? 0 : 1;
207 http_get('/'); 208 my $addr = $bigendian ? "7F000001" : "0100007F";
208 like(substr(last_set($t), 0, 8), '/[0100007F|F7000010|00000000]/', 209 is(substr(uid_set(http_get('/')), 0, 8), $addr, 'service default v2');
209 'service default v2'); 210
210 211 $addr = $bigendian ? "0000FFFE" : "FEFF0000";
211 http_get('/service'); 212 is(substr(uid_set(http_get('/service')), 0, 8), $addr, 'service custom');
212 like(substr(last_set($t), 0, 8), '/[0000FEFF|FEFF0000]/', 'service custom'); 213
214 # reset log
215
216 send_uid('/?log', cookie($r, 'uid'));
217
218 $t->stop();
219
220 like($t->read_file('error_reset.log'),
221 '/userid cookie "uid=\w+" was reset/m', 'uid reset variable log');
213 222
214 ############################################################################### 223 ###############################################################################
215 224
216 sub cookie { 225 sub cookie {
217 my ($r, $key) = @_; 226 my ($r, $key) = @_;
241 my ($hour, $min, $sec) = split(":", $time); 250 my ($hour, $min, $sec) = split(":", $time);
242 251
243 return timegm($sec, $min, $hour, $day, $months{$month}, $year); 252 return timegm($sec, $min, $hour, $day, $months{$month}, $year);
244 } 253 }
245 254
246 sub last_got { 255 sub uid_set {
247 my ($t) = @_; 256 my ($r) = @_;
248 my $log = $t->read_file('userid.log'); 257 my ($uid) = $r =~ /X-Set: uid=(.*)\n/m;
249 my ($uid) = $log =~ /uid=(.*):.*\n$/m;
250 return $uid; 258 return $uid;
251 } 259 }
252 260
253 sub last_set { 261 sub uid_got {
254 my ($t) = @_; 262 my ($r) = @_;
255 my $log = $t->read_file('userid.log'); 263 my ($uid) = $r =~ /X-Got: uid=(.*)\n/m;
256 my ($uid) = $log =~ /:uid=(.*)\n$/m;
257 return $uid; 264 return $uid;
258 } 265 }
259 266
260 sub send_uid { 267 sub send_uid {
261 my ($url, $uid) = @_; 268 my ($url, $uid) = @_;