comparison js_subrequests.t @ 1545:cde406e61e48

Tests: adapted js_subrequests.t to njs changes after 0.3.8. Since 0.3.8 if callback is not provided for r.subrequest() a promise is returned.
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 16 Jan 2020 19:15:26 +0300
parents 53ed2231403b
children 01867708af95
comparison
equal deleted inserted replaced
1544:53ed2231403b 1545:cde406e61e48
57 57
58 location /sr { 58 location /sr {
59 js_content sr; 59 js_content sr;
60 } 60 }
61 61
62 location /sr_pr {
63 js_content sr_pr;
64 }
65
62 location /sr_args { 66 location /sr_args {
63 js_content sr_args; 67 js_content sr_args;
64 } 68 }
65 69
66 location /sr_options_args { 70 location /sr_options_args {
67 js_content sr_options_args; 71 js_content sr_options_args;
68 } 72 }
69 73
74 location /sr_options_args_pr {
75 js_content sr_options_args_pr;
76 }
77
70 location /sr_options_method { 78 location /sr_options_method {
71 js_content sr_options_method; 79 js_content sr_options_method;
72 } 80 }
73 81
82 location /sr_options_method_pr {
83 js_content sr_options_method_pr;
84 }
85
74 location /sr_options_body { 86 location /sr_options_body {
75 js_content sr_options_body; 87 js_content sr_options_body;
76 } 88 }
77 89
78 location /sr_options_method_head { 90 location /sr_options_method_head {
81 93
82 location /sr_body { 94 location /sr_body {
83 js_content sr_body; 95 js_content sr_body;
84 } 96 }
85 97
98 location /sr_body_pr {
99 js_content sr_body_pr;
100 }
101
86 location /sr_body_special { 102 location /sr_body_special {
87 js_content sr_body_special; 103 js_content sr_body_special;
88 }
89
90 location /sr_background {
91 js_content sr_background;
92 } 104 }
93 105
94 location /sr_in_variable_handler { 106 location /sr_in_variable_handler {
95 set $_ $async_var; 107 set $_ $async_var;
96 js_content sr_in_variable_handler; 108 js_content sr_in_variable_handler;
104 116
105 location /sr_js_in_subrequest { 117 location /sr_js_in_subrequest {
106 js_content sr_js_in_subrequest; 118 js_content sr_js_in_subrequest;
107 } 119 }
108 120
121 location /sr_js_in_subrequest_pr {
122 js_content sr_js_in_subrequest_pr;
123 }
124
109 location /sr_file { 125 location /sr_file {
110 js_content sr_file; 126 js_content sr_file;
111 } 127 }
112 128
113 location /sr_cache { 129 location /sr_cache {
115 } 131 }
116 132
117 133
118 location /sr_unavail { 134 location /sr_unavail {
119 js_content sr_unavail; 135 js_content sr_unavail;
136 }
137
138 location /sr_unavail_pr {
139 js_content sr_unavail_pr;
120 } 140 }
121 141
122 location /sr_broken { 142 location /sr_broken {
123 js_content sr_broken; 143 js_content sr_broken;
124 } 144 }
214 234
215 location /body { 235 location /body {
216 js_content body; 236 js_content body;
217 } 237 }
218 238
219 location /background {
220 js_content background;
221 }
222
223 location /delayed { 239 location /delayed {
224 js_content delayed; 240 js_content delayed;
225 } 241 }
226 } 242 }
227 243
240 r.return(200, njs.version); 256 r.return(200, njs.version);
241 } 257 }
242 258
243 function sr(r) { 259 function sr(r) {
244 subrequest_fn(r, ['/p/sub2'], ['uri', 'status']) 260 subrequest_fn(r, ['/p/sub2'], ['uri', 'status'])
261 }
262
263 function sr_pr(r) {
264 r.subrequest('/p/sub1', 'h=xxx')
265 .then(reply => r.return(200, JSON.stringify({h:reply.headersOut.h})))
245 } 266 }
246 267
247 function sr_args(r) { 268 function sr_args(r) {
248 r.subrequest('/p/sub1', 'h=xxx', reply => { 269 r.subrequest('/p/sub1', 'h=xxx', reply => {
249 r.return(200, JSON.stringify({h:reply.headersOut.h})); 270 r.return(200, JSON.stringify({h:reply.headersOut.h}));
254 r.subrequest('/p/sub1', {args:'h=xxx'}, reply => { 275 r.subrequest('/p/sub1', {args:'h=xxx'}, reply => {
255 r.return(200, JSON.stringify({h:reply.headersOut.h})); 276 r.return(200, JSON.stringify({h:reply.headersOut.h}));
256 }); 277 });
257 } 278 }
258 279
280 function sr_options_args_pr(r) {
281 r.subrequest('/p/sub1', {args:'h=xxx'})
282 .then(reply => r.return(200, JSON.stringify({h:reply.headersOut.h})))
283 }
284
259 function sr_options_method(r) { 285 function sr_options_method(r) {
260 r.subrequest('/p/method', {method:r.args.m}, body_fwd_cb); 286 r.subrequest('/p/method', {method:r.args.m}, body_fwd_cb);
287 }
288
289 function sr_options_method_pr(r) {
290 r.subrequest('/p/method', {method:r.args.m})
291 .then(body_fwd_cb);
261 } 292 }
262 293
263 function sr_options_body(r) { 294 function sr_options_body(r) {
264 r.subrequest('/p/body', {method:'POST', body:'["REQ-BODY"]'}, 295 r.subrequest('/p/body', {method:'POST', body:'["REQ-BODY"]'},
265 body_fwd_cb); 296 body_fwd_cb);
274 305
275 function sr_body(r) { 306 function sr_body(r) {
276 r.subrequest('/p/sub1', body_fwd_cb); 307 r.subrequest('/p/sub1', body_fwd_cb);
277 } 308 }
278 309
310 function sr_body_pr(r) {
311 r.subrequest('/p/sub1')
312 .then(body_fwd_cb);
313 }
314
279 function sr_body_special(r) { 315 function sr_body_special(r) {
280 r.subrequest('/p/sub2', body_fwd_cb); 316 r.subrequest('/p/sub2', body_fwd_cb);
281 }
282
283 function sr_background(r) {
284 r.subrequest('/p/background');
285 r.subrequest('/p/background', 'a=xxx');
286 r.subrequest('/p/background', {args: 'a=yyy', method:'POST'});
287
288 r.return(200);
289 } 317 }
290 318
291 function body(r) { 319 function body(r) {
292 r.return(200, r.variables.request_body); 320 r.return(200, r.variables.request_body);
293 } 321 }
294 322
295 function delayed(r) { 323 function delayed(r) {
296 setTimeout(r => r.return(200), 100, r); 324 setTimeout(r => r.return(200), 100, r);
297 } 325 }
298
299 function background(r) {
300 r.log("BACKGROUND: " + r.variables.request_method
301 + " args: " + r.variables.args);
302
303 r.return(200);
304 }
305 326
306 function sr_in_variable_handler(r) { 327 function sr_in_variable_handler(r) {
307 } 328 }
308 329
309 function async_var(r) { 330 function async_var(r) {
324 345
325 function sr_unavail(req) { 346 function sr_unavail(req) {
326 subrequest_fn(req, ['/unavail'], ['uri', 'status']); 347 subrequest_fn(req, ['/unavail'], ['uri', 'status']);
327 } 348 }
328 349
350 function sr_unavail_pr(req) {
351 subrequest_fn_pr(req, ['/unavail'], ['uri', 'status']);
352 }
353
329 function sr_broken(r) { 354 function sr_broken(r) {
330 r.subrequest('/daemon/unfinished', reply => { 355 r.subrequest('/daemon/unfinished', reply => {
331 r.return(200, JSON.stringify({code:reply.status})); 356 r.return(200, JSON.stringify({code:reply.status}));
332 }); 357 });
333 } 358 }
342 367
343 function sr_js_in_subrequest(r) { 368 function sr_js_in_subrequest(r) {
344 r.subrequest('/js_sub', body_fwd_cb); 369 r.subrequest('/js_sub', body_fwd_cb);
345 } 370 }
346 371
372 function sr_js_in_subrequest_pr(r) {
373 r.subrequest('/js_sub')
374 .then(body_fwd_cb);
375 }
376
347 function sr_in_sr_callback(r) { 377 function sr_in_sr_callback(r) {
348 r.subrequest('/return', function (reply) { 378 r.subrequest('/return', function (reply) {
349 try { 379 try {
350 reply.subrequest('/return'); 380 reply.subrequest('/return');
351 381
373 function sr_out_of_order(r) { 403 function sr_out_of_order(r) {
374 subrequest_fn(r, ['/p/delayed', '/p/sub1', '/unknown'], 404 subrequest_fn(r, ['/p/delayed', '/p/sub1', '/unknown'],
375 ['uri', 'status']); 405 ['uri', 'status']);
376 } 406 }
377 407
408 function collect(replies, props, total, reply) {
409 reply.log(`subrequest handler: \${reply.uri} status: \${reply.status}`)
410
411 var rep = {};
412 props.forEach(p => {rep[p] = reply[p]});
413
414 replies.push(rep);
415
416 if (replies.length == total) {
417 reply.parent.return(200, JSON.stringify(replies));
418 }
419 }
420
378 function subrequest_fn(r, subs, props) { 421 function subrequest_fn(r, subs, props) {
379 var rep, replies = []; 422 var replies = [];
380 423
381 subs.forEach(sr => { 424 subs.forEach(sr =>
382 r.subrequest(sr, reply => { 425 r.subrequest(sr, collect.bind(null, replies,
383 r.log("subrequest handler: " + reply.uri 426 props, subs.length)));
384 + " status: " + reply.status) 427 }
385 428
386 rep = {}; 429 function subrequest_fn_pr(r, subs, props) {
387 props.forEach(p => {rep[p] = reply[p]}); 430 var replies = [];
388 431
389 replies.push(rep); 432 subs.forEach(sr => r.subrequest(sr)
390 433 .then(collect.bind(null, replies, props, subs.length)));
391 if (replies.length == subs.length) {
392 r.return(200, JSON.stringify(replies));
393 }
394 });
395 });
396 } 434 }
397 435
398 function sr_except_not_a_func(r) { 436 function sr_except_not_a_func(r) {
399 r.subrequest('/sub1', 'a=1', 'b'); 437 r.subrequest('/sub1', 'a=1', 'b');
400 } 438 }
421 459
422 EOF 460 EOF
423 461
424 $t->write_file('t', '["SEE-THIS"]'); 462 $t->write_file('t', '["SEE-THIS"]');
425 463
426 $t->try_run('no njs available')->plan(24); 464 $t->try_run('no njs available')->plan(29);
427 $t->run_daemon(\&http_daemon); 465 $t->run_daemon(\&http_daemon);
428 466
429 ############################################################################### 467 ###############################################################################
430 468
431 is(get_json('/sr'), '[{"status":404,"uri":"/p/sub2"}]', 'sr'); 469 is(get_json('/sr'), '[{"status":404,"uri":"/p/sub2"}]', 'sr');
459 '[{"status":404,"uri":"/unknown"},' . 497 '[{"status":404,"uri":"/unknown"},' .
460 '{"status":206,"uri":"/p/sub1"},' . 498 '{"status":206,"uri":"/p/sub1"},' .
461 '{"status":200,"uri":"/p/delayed"}]', 499 '{"status":200,"uri":"/p/delayed"}]',
462 'sr_multi'); 500 'sr_multi');
463 501
464 http_get('/sr_background'); 502 TODO: {
503 local $TODO = 'not yet'
504 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.3.8';
505
506 is(get_json('/sr_pr'), '{"h":"xxx"}', 'sr_promise');
507 is(get_json('/sr_options_args_pr'), '{"h":"xxx"}', 'sr_options_args_pr');
508 is(get_json('/sr_options_method_pr?m=PUT'), '["PUT"]', 'sr method PUT');
509 is(get_json('/sr_body_pr'), '{"a":{"b":1}}', 'sr_body_pr');
510 is(get_json('/sr_js_in_subrequest_pr'), '["JS-SUB"]', 'sr_js_in_subrequest_pr');
511 is(get_json('/sr_unavail_pr'), '[{"status":502,"uri":"/unavail"}]',
512 'sr_unavail_pr');
513
514 }
465 515
466 http_get('/sr_broken'); 516 http_get('/sr_broken');
467 http_get('/sr_in_sr'); 517 http_get('/sr_in_sr');
468 http_get('/sr_in_variable_handler'); 518 http_get('/sr_in_variable_handler');
469 http_get('/sr_error_page'); 519 http_get('/sr_error_page');
483 'subrequest cb exception'); 533 'subrequest cb exception');
484 ok(index($t->read_file('error.log'), 'failed to convert uri arg') > 0, 534 ok(index($t->read_file('error.log'), 'failed to convert uri arg') > 0,
485 'subrequest uri exception'); 535 'subrequest uri exception');
486 ok(index($t->read_file('error.log'), 'failed to convert args') > 0, 536 ok(index($t->read_file('error.log'), 'failed to convert args') > 0,
487 'subrequest invalid args exception'); 537 'subrequest invalid args exception');
488 ok(index($t->read_file('error.log'), 'BACKGROUND') > 0,
489 'background subrequest');
490 ok(index($t->read_file('error.log'), 'too big subrequest response') > 0, 538 ok(index($t->read_file('error.log'), 'too big subrequest response') > 0,
491 'subrequest too large body'); 539 'subrequest too large body');
492 ok(index($t->read_file('error.log'), 'subrequest creation failed') > 0, 540 ok(index($t->read_file('error.log'), 'subrequest creation failed') > 0,
493 'subrequest creation failed'); 541 'subrequest creation failed');
494 ok(index($t->read_file('error.log'), 542 ok(index($t->read_file('error.log'),