comparison js_subrequests.t @ 1338:adb306c10169

Tests: style.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 07 Jun 2018 22:11:47 +0300
parents bab9dd2654c3
children c582fa4c4a95
comparison
equal deleted inserted replaced
1337:bab9dd2654c3 1338:adb306c10169
233 subrequest_fn(req, ['/p/sub2'], ['uri', 'status']) 233 subrequest_fn(req, ['/p/sub2'], ['uri', 'status'])
234 } 234 }
235 235
236 function sr_args(req, res) { 236 function sr_args(req, res) {
237 req.subrequest('/p/sub1', 'h=xxx', function(reply) { 237 req.subrequest('/p/sub1', 'h=xxx', function(reply) {
238 res.status = 200; 238 res.status = 200;
239 res.sendHeader(); 239 res.sendHeader();
240 res.send(JSON.stringify({h:reply.headers.h})) 240 res.send(JSON.stringify({h:reply.headers.h}))
241 res.finish(); 241 res.finish();
242 }); 242 });
243 } 243 }
244 244
245 function sr_options_args(req, res) { 245 function sr_options_args(req, res) {
246 req.subrequest('/p/sub1', {args:'h=xxx'}, function(reply) { 246 req.subrequest('/p/sub1', {args:'h=xxx'}, function(reply) {
247 res.status = 200; 247 res.status = 200;
248 res.sendHeader(); 248 res.sendHeader();
249 res.send(JSON.stringify({h:reply.headers.h})) 249 res.send(JSON.stringify({h:reply.headers.h}))
250 res.finish(); 250 res.finish();
251 }); 251 });
252 } 252 }
253 253
254 function sr_options_method(req, res) { 254 function sr_options_method(req, res) {
255 req.subrequest('/p/method', {method:'POST'}, body_fwd_cb); 255 req.subrequest('/p/method', {method:'POST'}, body_fwd_cb);
256 } 256 }
259 req.subrequest('/p/body', {method:'POST', body:'["REQ-BODY"]'}, 259 req.subrequest('/p/body', {method:'POST', body:'["REQ-BODY"]'},
260 body_fwd_cb); 260 body_fwd_cb);
261 } 261 }
262 262
263 function sr_options_method_head(req, res) { 263 function sr_options_method_head(req, res) {
264 req.subrequest('/p/method', {method:'HEAD'}, function(reply){ 264 req.subrequest('/p/method', {method:'HEAD'}, function(reply) {
265 res.status = 200; 265 res.status = 200;
266 res.sendHeader(); 266 res.sendHeader();
267 res.send(JSON.stringify({c:reply.status, s:reply.body.length})) 267 res.send(JSON.stringify({c:reply.status, s:reply.body.length}))
268 res.finish(); 268 res.finish();
269 }); 269 });
270 } 270 }
271 271
272 function sr_body(req, res) { 272 function sr_body(req, res) {
273 req.subrequest('/p/sub1', body_fwd_cb); 273 req.subrequest('/p/sub1', body_fwd_cb);
274 } 274 }
314 function sr_in_variable_handler(req, res) { 314 function sr_in_variable_handler(req, res) {
315 } 315 }
316 316
317 function async_var(req, res) { 317 function async_var(req, res) {
318 req.subrequest('/p/delayed', function(reply) { 318 req.subrequest('/p/delayed', function(reply) {
319 res.status = 200; 319 res.status = 200;
320 res.sendHeader(); 320 res.sendHeader();
321 res.send(JSON.stringify(["CB-VAR"])) 321 res.send(JSON.stringify(["CB-VAR"]))
322 res.finish(); 322 res.finish();
323 }) 323 })
324 324
325 return ""; 325 return "";
326 } 326 }
327 327
328 function sr_file(req, res) { 328 function sr_file(req, res) {
329 req.subrequest('/file/t', body_fwd_cb); 329 req.subrequest('/file/t', body_fwd_cb);
330 } 330 }
359 req.subrequest('/js_sub', body_fwd_cb); 359 req.subrequest('/js_sub', body_fwd_cb);
360 } 360 }
361 361
362 function sr_out_of_order(req) { 362 function sr_out_of_order(req) {
363 subrequest_fn(req, ['/p/delayed', '/p/sub1', '/unknown'], 363 subrequest_fn(req, ['/p/delayed', '/p/sub1', '/unknown'],
364 ['uri', 'status']) 364 ['uri', 'status']);
365 } 365 }
366 366
367 function subrequest_fn(req, subs, props) { 367 function subrequest_fn(req, subs, props) {
368 var r, replies = []; 368 var r, replies = [];
369 369
370 subs.forEach(function(sr) { 370 subs.forEach(function(sr) {
371 req.subrequest(sr, function(reply) { 371 req.subrequest(sr, function(reply) {
372 req.log("subrequest handler: " + reply.uri 372 req.log("subrequest handler: " + reply.uri
373 + " status: " + reply.status) 373 + " status: " + reply.status)
374 374
375 r = {}; 375 r = {};
376 props.forEach(function (p) {r[p] = reply[p]}); 376 props.forEach(function (p) {r[p] = reply[p]});
377 377
378 replies.push(r); 378 replies.push(r);
379 379
380 if (replies.length == subs.length) { 380 if (replies.length == subs.length) {
381 var res = req.response; 381 var res = req.response;
382 res.status = 200; 382 res.status = 200;
383 res.sendHeader(); 383 res.sendHeader();
384 res.send(JSON.stringify(replies)); 384 res.send(JSON.stringify(replies));
385 res.finish(); 385 res.finish();
386 } 386 }
387 }); 387 });
388 }) 388 });
389 } 389 }
390 390
391 function sr_except_not_a_func(req, res) { 391 function sr_except_not_a_func(req, res) {
392 req.subrequest('/sub1', 'a=1', 'b') 392 req.subrequest('/sub1', 'a=1', 'b');
393 } 393 }
394 394
395 function sr_except_failed_to_convert_arg(req, res) { 395 function sr_except_failed_to_convert_arg(req, res) {
396 req.subrequest('/sub1', req.args, function(){}) 396 req.subrequest('/sub1', req.args, function(){});
397 } 397 }
398 398
399 function sr_except_failed_to_convert_options_arg(req, res) { 399 function sr_except_failed_to_convert_options_arg(req, res) {
400 req.subrequest('/sub1', {args:req.args}, function(){}) 400 req.subrequest('/sub1', {args:req.args}, function(){});
401 } 401 }
402 402
403 function sr_except_invalid_options_method(req, res) { 403 function sr_except_invalid_options_method(req, res) {
404 req.subrequest('/sub1', {method:'UNKNOWN_METHOD'}, function(){}) 404 req.subrequest('/sub1', {method:'UNKNOWN_METHOD'}, function(){});
405 } 405 }
406 406
407 function sr_uri_except(req, res) { 407 function sr_uri_except(req, res) {
408 req.subrequest(req, 'a=1', 'b') 408 req.subrequest(req, 'a=1', 'b');
409 } 409 }
410 410
411 function body_fwd_cb(reply) { 411 function body_fwd_cb(reply) {
412 var res = reply.parent.response; 412 var res = reply.parent.response;
413 res.status = 200; 413 res.status = 200;
536 "HTTP/1.1 200 OK" . CRLF . 536 "HTTP/1.1 200 OK" . CRLF .
537 "Transfer-Encoding: chunked" . CRLF . 537 "Transfer-Encoding: chunked" . CRLF .
538 "Content-Length: 100" . CRLF . 538 "Content-Length: 100" . CRLF .
539 CRLF . 539 CRLF .
540 "unfinished" . CRLF; 540 "unfinished" . CRLF;
541 close($client); 541 close($client);
542 } 542 }
543 } 543 }
544 } 544 }
545
546 ###############################################################################