comparison stream_js.t @ 1751:18ac4d9e5a2a

Tests: rewriting njs tests without deprecated "js_include".
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 09 Dec 2021 17:36:29 +0000
parents 9e0347f4df11
children 520fb74cce4c
comparison
equal deleted inserted replaced
1750:b28f88e352dd 1751:18ac4d9e5a2a
35 } 35 }
36 36
37 http { 37 http {
38 %%TEST_GLOBALS_HTTP%% 38 %%TEST_GLOBALS_HTTP%%
39 39
40 js_include test.js; 40 js_import test.js;
41 41
42 server { 42 server {
43 listen 127.0.0.1:8079; 43 listen 127.0.0.1:8079;
44 server_name localhost; 44 server_name localhost;
45 45
46 location /njs { 46 location /njs {
47 js_content test_njs; 47 js_content test.njs;
48 } 48 }
49 49
50 location /p/ { 50 location /p/ {
51 proxy_pass http://127.0.0.1:8095/; 51 proxy_pass http://127.0.0.1:8095/;
52 52
59 } 59 }
60 60
61 stream { 61 stream {
62 %%TEST_GLOBALS_STREAM%% 62 %%TEST_GLOBALS_STREAM%%
63 63
64 js_set $js_addr js_addr; 64 js_set $js_addr test.addr;
65 js_set $js_var js_var; 65 js_set $js_var test.variable;
66 js_set $js_log js_log; 66 js_set $js_log test.log;
67 js_set $js_unk js_unk; 67 js_set $js_unk test.unk;
68 js_set $js_req_line js_req_line; 68 js_set $js_req_line test.req_line;
69 js_set $js_sess_unk js_sess_unk; 69 js_set $js_sess_unk test.sess_unk;
70 js_set $js_async js_async; 70 js_set $js_async test.asyncf;
71 71
72 js_include test.js; 72 js_import test.js;
73 73
74 log_format status $server_port:$status; 74 log_format status $server_port:$status;
75 75
76 server { 76 server {
77 listen 127.0.0.1:8080; 77 listen 127.0.0.1:8080;
103 return $js_addr; 103 return $js_addr;
104 } 104 }
105 105
106 server { 106 server {
107 listen 127.0.0.1:8086; 107 listen 127.0.0.1:8086;
108 js_access js_access_step; 108 js_access test.access_step;
109 js_preread js_preread_step; 109 js_preread test.preread_step;
110 js_filter js_filter_step; 110 js_filter test.filter_step;
111 proxy_pass 127.0.0.1:8090; 111 proxy_pass 127.0.0.1:8090;
112 } 112 }
113 113
114 server { 114 server {
115 listen 127.0.0.1:8087; 115 listen 127.0.0.1:8087;
116 js_access js_access_undecided; 116 js_access test.access_undecided;
117 return OK; 117 return OK;
118 access_log %%TESTDIR%%/status.log status; 118 access_log %%TESTDIR%%/status.log status;
119 } 119 }
120 120
121 server { 121 server {
122 listen 127.0.0.1:8088; 122 listen 127.0.0.1:8088;
123 js_access js_access_allow; 123 js_access test.access_allow;
124 return OK; 124 return OK;
125 access_log %%TESTDIR%%/status.log status; 125 access_log %%TESTDIR%%/status.log status;
126 } 126 }
127 127
128 server { 128 server {
129 listen 127.0.0.1:8089; 129 listen 127.0.0.1:8089;
130 js_access js_access_deny; 130 js_access test.access_deny;
131 return OK; 131 return OK;
132 access_log %%TESTDIR%%/status.log status; 132 access_log %%TESTDIR%%/status.log status;
133 } 133 }
134 134
135 server { 135 server {
136 listen 127.0.0.1:8091; 136 listen 127.0.0.1:8091;
137 js_preread js_preread_async; 137 js_preread test.preread_async;
138 proxy_pass 127.0.0.1:8090; 138 proxy_pass 127.0.0.1:8090;
139 } 139 }
140 140
141 server { 141 server {
142 listen 127.0.0.1:8092; 142 listen 127.0.0.1:8092;
143 js_preread js_preread_data; 143 js_preread test.preread_data;
144 proxy_pass 127.0.0.1:8090; 144 proxy_pass 127.0.0.1:8090;
145 } 145 }
146 146
147 server { 147 server {
148 listen 127.0.0.1:8093; 148 listen 127.0.0.1:8093;
149 js_preread js_preread_req_line; 149 js_preread test.preread_req_line;
150 return $js_req_line; 150 return $js_req_line;
151 } 151 }
152 152
153 server { 153 server {
154 listen 127.0.0.1:8094; 154 listen 127.0.0.1:8094;
155 js_filter js_filter_empty; 155 js_filter test.filter_empty;
156 proxy_pass 127.0.0.1:8090; 156 proxy_pass 127.0.0.1:8090;
157 } 157 }
158 158
159 server { 159 server {
160 listen 127.0.0.1:8095; 160 listen 127.0.0.1:8095;
161 js_filter js_filter_header_inject; 161 js_filter test.filter_header_inject;
162 proxy_pass 127.0.0.1:8079; 162 proxy_pass 127.0.0.1:8079;
163 } 163 }
164 164
165 server { 165 server {
166 listen 127.0.0.1:8096; 166 listen 127.0.0.1:8096;
167 js_filter js_filter_search; 167 js_filter test.filter_search;
168 proxy_pass 127.0.0.1:8090; 168 proxy_pass 127.0.0.1:8090;
169 } 169 }
170 170
171 server { 171 server {
172 listen 127.0.0.1:8097; 172 listen 127.0.0.1:8097;
173 js_access js_access_except; 173 js_access test.access_except;
174 proxy_pass 127.0.0.1:8090; 174 proxy_pass 127.0.0.1:8090;
175 } 175 }
176 176
177 server { 177 server {
178 listen 127.0.0.1:8098; 178 listen 127.0.0.1:8098;
179 js_preread js_preread_except; 179 js_preread test.preread_except;
180 proxy_pass 127.0.0.1:8090; 180 proxy_pass 127.0.0.1:8090;
181 } 181 }
182 182
183 server { 183 server {
184 listen 127.0.0.1:8099; 184 listen 127.0.0.1:8099;
185 js_filter js_filter_except; 185 js_filter test.filter_except;
186 proxy_pass 127.0.0.1:8090; 186 proxy_pass 127.0.0.1:8090;
187 } 187 }
188 188
189 server { 189 server {
190 listen 127.0.0.1:8100; 190 listen 127.0.0.1:8100;
197 $t->write_file('test.js', <<EOF); 197 $t->write_file('test.js', <<EOF);
198 function test_njs(r) { 198 function test_njs(r) {
199 r.return(200, njs.version); 199 r.return(200, njs.version);
200 } 200 }
201 201
202 function js_addr(s) { 202 function addr(s) {
203 return 'addr=' + s.remoteAddress; 203 return 'addr=' + s.remoteAddress;
204 } 204 }
205 205
206 function js_var(s) { 206 function variable(s) {
207 return 'variable=' + s.variables.remote_addr; 207 return 'variable=' + s.variables.remote_addr;
208 } 208 }
209 209
210 function js_sess_unk(s) { 210 function sess_unk(s) {
211 return 'sess_unk=' + s.unk; 211 return 'sess_unk=' + s.unk;
212 } 212 }
213 213
214 function js_log(s) { 214 function log(s) {
215 s.log("SEE-THIS"); 215 s.log("SEE-THIS");
216 } 216 }
217 217
218 var res = ''; 218 var res = '';
219 219
220 function js_access_step(s) { 220 function access_step(s) {
221 res += '1'; 221 res += '1';
222 222
223 setTimeout(function() { 223 setTimeout(function() {
224 if (s.remoteAddress.match('127.0.0.1')) { 224 if (s.remoteAddress.match('127.0.0.1')) {
225 s.allow(); 225 s.allow();
226 } 226 }
227 }, 1); 227 }, 1);
228 } 228 }
229 229
230 function js_preread_step(s) { 230 function preread_step(s) {
231 s.on('upload', function (data) { 231 s.on('upload', function (data) {
232 res += '2'; 232 res += '2';
233 if (res.length >= 3) { 233 if (res.length >= 3) {
234 s.done(); 234 s.done();
235 } 235 }
236 }); 236 });
237 } 237 }
238 238
239 function js_filter_step(s) { 239 function filter_step(s) {
240 s.on('upload', function(data, flags) { 240 s.on('upload', function(data, flags) {
241 s.send(data); 241 s.send(data);
242 res += '3'; 242 res += '3';
243 }); 243 });
244 244
254 s.off('download'); 254 s.off('download');
255 } 255 }
256 }); 256 });
257 } 257 }
258 258
259 function js_access_undecided(s) { 259 function access_undecided(s) {
260 s.decline(); 260 s.decline();
261 } 261 }
262 262
263 function js_access_allow(s) { 263 function access_allow(s) {
264 if (s.remoteAddress.match('127.0.0.1')) { 264 if (s.remoteAddress.match('127.0.0.1')) {
265 s.done(); 265 s.done();
266 return; 266 return;
267 } 267 }
268 268
269 s.deny(); 269 s.deny();
270 } 270 }
271 271
272 function js_access_deny(s) { 272 function access_deny(s) {
273 if (s.remoteAddress.match('127.0.0.1')) { 273 if (s.remoteAddress.match('127.0.0.1')) {
274 s.deny(); 274 s.deny();
275 return; 275 return;
276 } 276 }
277 277
278 s.allow(); 278 s.allow();
279 } 279 }
280 280
281 281
282 function js_preread_async(s) { 282 function preread_async(s) {
283 setTimeout(function() { 283 setTimeout(function() {
284 s.done(); 284 s.done();
285 }, 1); 285 }, 1);
286 } 286 }
287 287
288 function js_preread_data(s) { 288 function preread_data(s) {
289 s.on('upload', function (data, flags) { 289 s.on('upload', function (data, flags) {
290 if (data.indexOf('z') != -1) { 290 if (data.indexOf('z') != -1) {
291 s.done(); 291 s.done();
292 } 292 }
293 }); 293 });
294 } 294 }
295 295
296 var line = ''; 296 var line = '';
297 297
298 function js_preread_req_line(s) { 298 function preread_req_line(s) {
299 s.on('upload', function (data, flags) { 299 s.on('upload', function (data, flags) {
300 var n = data.indexOf('\\n'); 300 var n = data.indexOf('\\n');
301 if (n != -1) { 301 if (n != -1) {
302 line = data.substr(0, n); 302 line = data.substr(0, n);
303 s.done(); 303 s.done();
304 } 304 }
305 }); 305 });
306 } 306 }
307 307
308 function js_req_line(s) { 308 function req_line(s) {
309 return line; 309 return line;
310 } 310 }
311 311
312 function js_filter_empty(s) { 312 function filter_empty(s) {
313 } 313 }
314 314
315 function js_filter_header_inject(s) { 315 function filter_header_inject(s) {
316 var req = ''; 316 var req = '';
317 317
318 s.on('upload', function(data, flags) { 318 s.on('upload', function(data, flags) {
319 req += data; 319 req += data;
320 320
328 s.off('upload'); 328 s.off('upload');
329 } 329 }
330 }); 330 });
331 } 331 }
332 332
333 function js_filter_search(s) { 333 function filter_search(s) {
334 s.on('download', function(data, flags) { 334 s.on('download', function(data, flags) {
335 var n = data.search('y'); 335 var n = data.search('y');
336 if (n != -1) { 336 if (n != -1) {
337 s.send('z'); 337 s.send('z');
338 } 338 }
344 s.send('y'); 344 s.send('y');
345 } 345 }
346 }); 346 });
347 } 347 }
348 348
349 function js_access_except(s) { 349 function access_except(s) {
350 function done() {return s.a.a}; 350 function done() {return s.a.a};
351 351
352 setTimeout(done, 1); 352 setTimeout(done, 1);
353 setTimeout(done, 2); 353 setTimeout(done, 2);
354 } 354 }
355 355
356 function js_preread_except(s) { 356 function preread_except(s) {
357 var fs = require('fs'); 357 var fs = require('fs');
358 fs.readFileSync(); 358 fs.readFileSync();
359 } 359 }
360 360
361 function js_filter_except(s) { 361 function filter_except(s) {
362 s.on('unknown', function() {}); 362 s.on('unknown', function() {});
363 } 363 }
364 364
365 function pr(x) { 365 function pr(x) {
366 return new Promise(resolve => {resolve(x)}).then(v => v).then(v => v); 366 return new Promise(resolve => {resolve(x)}).then(v => v).then(v => v);
367 } 367 }
368 368
369 async function js_async(s) { 369 async function asyncf(s) {
370 const a1 = await pr(10); 370 const a1 = await pr(10);
371 const a2 = await pr(20); 371 const a2 = await pr(20);
372 372
373 s.setReturnValue(`retval: \${a1 + a2}`); 373 s.setReturnValue(`retval: \${a1 + a2}`);
374 } 374 }
375
376 export default {njs:test_njs, addr, variable, sess_unk, log, access_step,
377 preread_step, filter_step, access_undecided, access_allow,
378 access_deny, preread_async, preread_data, preread_req_line,
379 req_line, filter_empty, filter_header_inject, filter_search,
380 access_except, preread_except, filter_except, asyncf};
375 381
376 EOF 382 EOF
377 383
378 $t->run_daemon(\&stream_daemon, port(8090)); 384 $t->run_daemon(\&stream_daemon, port(8090));
379 $t->try_run('no stream njs available')->plan(23); 385 $t->try_run('no stream njs available')->plan(23);
391 is(stream('127.0.0.1:' . port(8083))->read(), '', 'stream js unknown function'); 397 is(stream('127.0.0.1:' . port(8083))->read(), '', 'stream js unknown function');
392 is(stream('127.0.0.1:' . port(8084))->read(), 'sess_unk=undefined', 's.unk'); 398 is(stream('127.0.0.1:' . port(8084))->read(), 'sess_unk=undefined', 's.unk');
393 399
394 is(stream('127.0.0.1:' . port(8086))->io('0'), '0122345', 400 is(stream('127.0.0.1:' . port(8086))->io('0'), '0122345',
395 'async handlers order'); 401 'async handlers order');
396 is(stream('127.0.0.1:' . port(8087))->io('#'), 'OK', 'js_access_undecided'); 402 is(stream('127.0.0.1:' . port(8087))->io('#'), 'OK', 'access_undecided');
397 is(stream('127.0.0.1:' . port(8088))->io('#'), 'OK', 'js_access_allow'); 403 is(stream('127.0.0.1:' . port(8088))->io('#'), 'OK', 'access_allow');
398 is(stream('127.0.0.1:' . port(8089))->io('#'), '', 'js_access_deny'); 404 is(stream('127.0.0.1:' . port(8089))->io('#'), '', 'access_deny');
399 405
400 is(stream('127.0.0.1:' . port(8091))->io('#'), '#', 'js_preread_async'); 406 is(stream('127.0.0.1:' . port(8091))->io('#'), '#', 'preread_async');
401 is(stream('127.0.0.1:' . port(8092))->io('#z'), '#z', 'js_preread_async_data'); 407 is(stream('127.0.0.1:' . port(8092))->io('#z'), '#z', 'preread_async_data');
402 is(stream('127.0.0.1:' . port(8093))->io("xy\na"), 'xy', 'js_preread_req_line'); 408 is(stream('127.0.0.1:' . port(8093))->io("xy\na"), 'xy', 'preread_req_line');
403 409
404 is(stream('127.0.0.1:' . port(8094))->io('x'), 'x', 'js_filter_empty'); 410 is(stream('127.0.0.1:' . port(8094))->io('x'), 'x', 'filter_empty');
405 like(get('/p/return'), qr/foo/, 'js_filter_injected_header'); 411 like(get('/p/return'), qr/foo/, 'filter_injected_header');
406 is(stream('127.0.0.1:' . port(8096))->io('x'), 'z', 'js_filter_search'); 412 is(stream('127.0.0.1:' . port(8096))->io('x'), 'z', 'filter_search');
407 413
408 stream('127.0.0.1:' . port(8097))->io('x'); 414 stream('127.0.0.1:' . port(8097))->io('x');
409 stream('127.0.0.1:' . port(8098))->io('x'); 415 stream('127.0.0.1:' . port(8098))->io('x');
410 stream('127.0.0.1:' . port(8099))->io('x'); 416 stream('127.0.0.1:' . port(8099))->io('x');
411 417
412 TODO: { 418 TODO: {
413 local $TODO = 'not yet' 419 local $TODO = 'not yet'
414 unless get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.0'; 420 unless get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.0';
415 421
416 is(stream('127.0.0.1:' . port(8100))->read(), 'retval: 30', 'js_async'); 422 is(stream('127.0.0.1:' . port(8100))->read(), 'retval: 30', 'asyncf');
417 423
418 } 424 }
419 425
420 $t->stop(); 426 $t->stop();
421 427
422 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'stream js log'); 428 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'stream js log');
423 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0, 429 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0,
424 'stream js_preread backtrace'); 430 'stream js_preread backtrace');
425 ok(index($t->read_file('error.log'), 'at js_filter_except') > 0, 431 ok(index($t->read_file('error.log'), 'at filter_except') > 0,
426 'stream js_filter backtrace'); 432 'stream js_filter backtrace');
427 433
428 my @p = (port(8087), port(8088), port(8089)); 434 my @p = (port(8087), port(8088), port(8089));
429 like($t->read_file('status.log'), qr/$p[0]:200/, 'status undecided'); 435 like($t->read_file('status.log'), qr/$p[0]:200/, 'status undecided');
430 like($t->read_file('status.log'), qr/$p[1]:200/, 'status allow'); 436 like($t->read_file('status.log'), qr/$p[1]:200/, 'status allow');