comparison js_fetch.t @ 1780:bd21a4a7c9f5

Tests: added js fetch tests for responses without Content-Length.
author Dmitry Volyntsev <xeioex@nginx.com>
date Wed, 20 Jul 2022 17:47:22 -0700
parents 7c727869aeda
children 95ba1e704b7b
comparison
equal deleted inserted replaced
1779:4b80f1ab7fdc 1780:bd21a4a7c9f5
58 js_content test.broken_response; 58 js_content test.broken_response;
59 } 59 }
60 60
61 location /body { 61 location /body {
62 js_content test.body; 62 js_content test.body;
63 }
64
65 location /body_special {
66 js_content test.body_special;
63 } 67 }
64 68
65 location /chain { 69 location /chain {
66 js_content test.chain; 70 js_content test.chain;
67 } 71 }
274 p.then(reply => { 278 p.then(reply => {
275 var h = reply.headers[method](r.args.h); 279 var h = reply.headers[method](r.args.h);
276 r.return(200, njs.dump(h)); 280 r.return(200, njs.dump(h));
277 }) 281 })
278 .catch(e => r.return(501, e.message)) 282 .catch(e => r.return(501, e.message))
283 }
284
285 async function body_special(r) {
286 let reply = await ngx.fetch(`http://127.0.0.1:$p2/\${r.args.loc}`);
287 let body = await reply.text();
288
289 r.return(200, body);
279 } 290 }
280 291
281 async function header_iter(r) { 292 async function header_iter(r) {
282 let url = `http://127.0.0.1:$p2/\${r.args.loc}`; 293 let url = `http://127.0.0.1:$p2/\${r.args.loc}`;
283 294
352 var bar = str(r.headersIn.bar); 363 var bar = str(r.headersIn.bar);
353 var c = r.headersIn.code ? Number(r.headersIn.code) : 200; 364 var c = r.headersIn.code ? Number(r.headersIn.code) : 200;
354 r.return(c, `\${v.host}:\${v.request_method}:\${foo}:\${bar}:\${body}`); 365 r.return(c, `\${v.host}:\${v.request_method}:\${foo}:\${bar}:\${body}`);
355 } 366 }
356 367
357 export default {njs: test_njs, body, broken, broken_response, 368 export default {njs: test_njs, body, broken, broken_response, body_special,
358 chain, chunked, header, header_iter, multi, loc, property}; 369 chain, chunked, header, header_iter, multi, loc, property};
359 EOF 370 EOF
360 371
361 $t->try_run('no njs.fetch')->plan(28); 372 $t->try_run('no njs.fetch')->plan(31);
362 373
363 $t->run_daemon(\&http_daemon, port(8082)); 374 $t->run_daemon(\&http_daemon, port(8082));
364 $t->waitforsocket('127.0.0.1:' . port(8082)); 375 $t->waitforsocket('127.0.0.1:' . port(8082));
365 376
366 ############################################################################### 377 ###############################################################################
371 'fetch body text'); 382 'fetch body text');
372 like(http_get('/body?getter=json&loc=json&path=b.c'), 383 like(http_get('/body?getter=json&loc=json&path=b.c'),
373 qr/200 OK.*"FIELD"$/s, 'fetch body json'); 384 qr/200 OK.*"FIELD"$/s, 'fetch body json');
374 like(http_get('/body?getter=json&loc=loc'), qr/501/s, 385 like(http_get('/body?getter=json&loc=loc'), qr/501/s,
375 'fetch body json invalid'); 386 'fetch body json invalid');
387 like(http_get('/body_special?loc=parted'), qr/200 OK.*X{32000}$/s,
388 'fetch body parted');
376 like(http_get('/property?pr=bodyUsed'), qr/false$/s, 389 like(http_get('/property?pr=bodyUsed'), qr/false$/s,
377 'fetch bodyUsed false'); 390 'fetch bodyUsed false');
378 like(http_get('/property?pr=bodyUsed&readBody=1'), qr/true$/s, 391 like(http_get('/property?pr=bodyUsed&readBody=1'), qr/true$/s,
379 'fetch bodyUsed true'); 392 'fetch bodyUsed true');
380 like(http_get('/property?pr=ok'), qr/200 OK.*true$/s, 393 like(http_get('/property?pr=ok'), qr/200 OK.*true$/s,
439 qr/\['A:a','B:a','C:a','D:a','E:a','F:a','G:a','H:a','Foo:a,b']$/s, 452 qr/\['A:a','B:a','C:a','D:a','E:a','F:a','G:a','H:a','Foo:a,b']$/s,
440 'fetch header duplicate large'); 453 'fetch header duplicate large');
441 454
442 } 455 }
443 456
457 TODO: {
458 local $TODO = 'not yet'
459 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.6';
460
461 like(http_get('/body_special?loc=no_content_length'),
462 qr/200 OK.*CONTENT-BODY$/s, 'fetch body without content-length');
463 like(http_get('/body_special?loc=no_content_length/parted'),
464 qr/200 OK.*X{32000}$/s, 'fetch body without content-length parted');
465
466 }
467
444 ############################################################################### 468 ###############################################################################
445 469
446 sub recode { 470 sub recode {
447 my $json; 471 my $json;
448 eval { $json = JSON::PP::decode_json(shift) }; 472 eval { $json = JSON::PP::decode_json(shift) };
540 "Content-Length: 100" . CRLF . 564 "Content-Length: 100" . CRLF .
541 "Connection: close" . CRLF . 565 "Connection: close" . CRLF .
542 CRLF . 566 CRLF .
543 "unfinished" . CRLF; 567 "unfinished" . CRLF;
544 568
569 } elsif ($uri eq '/parted') {
570 print $client
571 "HTTP/1.1 200 OK" . CRLF .
572 "Content-Length: 32000" . CRLF .
573 "Connection: close" . CRLF .
574 CRLF;
575
576 for (1 .. 4) {
577 select undef, undef, undef, 0.01;
578 print $client "X" x 8000;
579 }
580
581 } elsif ($uri eq '/no_content_length') {
582 print $client
583 "HTTP/1.1 200 OK" . CRLF .
584 "Connection: close" . CRLF .
585 CRLF .
586 "CONTENT-BODY";
587
588 } elsif ($uri eq '/no_content_length/parted') {
589 print $client
590 "HTTP/1.1 200 OK" . CRLF .
591 "Connection: close" . CRLF .
592 CRLF;
593
594 for (1 .. 4) {
595 select undef, undef, undef, 0.01;
596 print $client "X" x 8000;
597 }
598
545 } elsif ($uri eq '/big') { 599 } elsif ($uri eq '/big') {
546 print $client 600 print $client
547 "HTTP/1.1 200 OK" . CRLF . 601 "HTTP/1.1 200 OK" . CRLF .
548 "Content-Length: 100100" . CRLF . 602 "Content-Length: 100100" . CRLF .
549 "Connection: close" . CRLF . 603 "Connection: close" . CRLF .