1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
# vi:filetype=
use lib 'lib';
use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (2 * blocks() + 1);
$ENV{TEST_NGINX_HTML_DIR} = html_dir;
$ENV{TEST_NGINX_CLIENT_PORT} ||= server_port();
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location /main {
echo_subrequest GET /sub;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
hello
=== TEST 2: trailing echo
--- config
location /main {
echo_subrequest GET /sub;
echo after subrequest;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
hello
after subrequest
=== TEST 3: leading echo
--- config
location /main {
echo before subrequest;
echo_subrequest GET /sub;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
before subrequest
hello
=== TEST 4: leading & trailing echo
--- config
location /main {
echo before subrequest;
echo_subrequest GET /sub;
echo after subrequest;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
before subrequest
hello
after subrequest
=== TEST 5: multiple subrequests
--- config
location /main {
echo before sr 1;
echo_subrequest GET /sub;
echo after sr 1;
echo before sr 2;
echo_subrequest GET /sub;
echo after sr 2;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
before sr 1
hello
after sr 1
before sr 2
hello
after sr 2
=== TEST 6: timed multiple subrequests (blocking sleep)
--- config
location /main {
echo_reset_timer;
echo_subrequest GET /sub1;
echo_subrequest GET /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_blocking_sleep 0.02;
echo hello;
}
location /sub2 {
echo_blocking_sleep 0.01;
echo world;
}
--- request
GET /main
--- response_body_like
^hello
world
took 0\.0(?:2[5-9]|3[0-6]) sec for total\.$
=== TEST 7: timed multiple subrequests (non-blocking sleep)
--- config
location /main {
echo_reset_timer;
echo_subrequest GET /sub1;
echo_subrequest GET /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_sleep 0.02;
echo hello;
}
location /sub2 {
echo_sleep 0.01;
echo world;
}
--- request
GET /main
--- response_body_like
^hello
world
took 0\.0(?:2[5-9]|3[0-6]) sec for total\.$
=== TEST 8: location with args
--- config
location /main {
echo_subrequest GET /sub -q 'foo=Foo&bar=Bar';
}
location /sub {
echo $arg_foo $arg_bar;
}
--- request
GET /main
--- response_body
Foo Bar
=== TEST 9: chained subrequests
--- config
location /main {
echo 'pre main';
echo_subrequest GET /sub;
echo 'post main';
}
location /sub {
echo 'pre sub';
echo_subrequest GET /subsub;
echo 'post sub';
}
location /subsub {
echo 'subsub';
}
--- request
GET /main
--- response_body
pre main
pre sub
subsub
post sub
post main
=== TEST 10: chained subrequests using named locations
as of 0.8.20, ngx_http_subrequest still does not support
named location. sigh. this case is a TODO.
--- config
location /main {
echo 'pre main';
echo_subrequest GET @sub;
echo 'post main';
}
location @sub {
echo 'pre sub';
echo_subrequest GET @subsub;
echo 'post sub';
}
location @subsub {
echo 'subsub';
}
--- request
GET /main
--- response_body
pre main
pre sub
subsub
post sub
post main
--- SKIP
=== TEST 11: explicit flush in main request
--- config
location /main {
echo 'pre main';
echo_subrequest GET /sub;
echo 'post main';
echo_flush;
}
location /sub {
echo_sleep 0.02;
echo 'sub';
}
--- request
GET /main
--- response_body
pre main
sub
post main
=== TEST 12: DELETE subrequest
--- config
location /main {
echo_subrequest DELETE /sub;
}
location /sub {
echo "sub method: $echo_request_method";
echo "main method: $echo_client_request_method";
}
--- request
GET /main
--- response_body
sub method: DELETE
main method: GET
=== TEST 13: DELETE subrequest
--- config
location /main {
echo "main method: $echo_client_request_method";
echo_subrequest GET /proxy;
echo_subrequest DELETE /proxy;
}
location /proxy {
proxy_pass $scheme://127.0.0.1:$server_port/sub;
}
location /sub {
echo "sub method: $echo_request_method";
}
--- request
GET /main
--- response_body
main method: GET
sub method: GET
sub method: DELETE
=== TEST 14: POST subrequest with body
--- config
location /main {
echo_subrequest POST /sub -b 'hello, world';
}
location /sub {
echo "sub method: $echo_request_method";
# we don't need to call echo_read_client_body explicitly here
echo "sub body: $echo_request_body";
}
--- request
GET /main
--- response_body
sub method: POST
sub body: hello, world
=== TEST 15: POST subrequest with body (explicitly read the body)
--- config
location /main {
echo_subrequest POST /sub -b 'hello, world';
}
location /sub {
echo "sub method: $echo_request_method";
# we call echo_read_client_body explicitly here even
# though it's not necessary.
echo_read_request_body;
echo "sub body: $echo_request_body";
}
--- request
GET /main
--- response_body
sub method: POST
sub body: hello, world
=== TEST 16: POST subrequest with body (with proxy in the middle) and without read body explicitly
--- config
location /main {
echo_subrequest POST /proxy -b 'hello, world';
}
location /proxy {
proxy_pass $scheme://127.0.0.1:$server_port/sub;
}
location /sub {
echo "sub method: $echo_request_method.";
# we need to read body explicitly here...or $echo_request_body
# will evaluate to empty ("")
echo "sub body: $echo_request_body.";
}
--- request
GET /main
--- response_body
sub method: POST.
sub body: .
=== TEST 17: POST subrequest with body (with proxy in the middle) and read body explicitly
--- config
location /main {
echo_subrequest POST /proxy -b 'hello, world';
}
location /proxy {
proxy_pass $scheme://127.0.0.1:$server_port/sub;
}
location /sub {
echo "sub method: $echo_request_method.";
# we need to read body explicitly here...or $echo_request_body
# will evaluate to empty ("")
echo_read_request_body;
echo "sub body: $echo_request_body.";
}
--- request
GET /main
--- response_body
sub method: POST.
sub body: hello, world.
=== TEST 18: multiple subrequests
--- config
location /multi {
echo_subrequest POST '/sub' -q 'foo=Foo' -b 'hi';
echo_subrequest PUT '/sub' -q 'bar=Bar' -b 'hello';
}
location /sub {
echo "querystring: $query_string";
echo "method: $echo_request_method";
echo "body: $echo_request_body";
echo "content length: $http_content_length";
echo '///';
}
--- request
GET /multi
--- response_body
querystring: foo=Foo
method: POST
body: hi
content length: 2
///
querystring: bar=Bar
method: PUT
body: hello
content length: 5
///
=== TEST 19: unsafe uri
--- config
location /unsafe {
echo_subrequest GET '/../foo';
}
--- request
GET /unsafe
--- ignore_response
--- error_log
echo_subrequest sees unsafe uri: "/../foo"
--- no_error_log
[error]
=== TEST 20: querystring in url
--- config
location /main {
echo_subrequest GET /sub?foo=Foo&bar=Bar;
}
location /sub {
echo $arg_foo $arg_bar;
}
--- request
GET /main
--- response_body
Foo Bar
=== TEST 21: querystring in url *AND* an explicit querystring
--- config
location /main {
echo_subrequest GET /sub?foo=Foo&bar=Bar -q blah=Blah;
}
location /sub {
echo $arg_foo $arg_bar $arg_blah;
}
--- request
GET /main
--- response_body
Blah
=== TEST 22: let subrequest to read the main request's request body
--- SKIP
--- config
location /main {
echo_subrequest POST /sub;
}
location /sub {
echo_read_request_body;
echo_request_body;
}
--- request
POST /main
hello, body!
--- response_body chomp
hello, body!
=== TEST 23: deep nested echo_subrequest/echo_subrequest_async
--- config
location /main {
echo_subrequest GET /bar;
echo_subrequest_async GET /bar;
echo_subrequest_async GET /bar;
echo_subrequest GET /group;
echo_subrequest_async GET /group;
}
location /group {
echo_subrequest GET /bar;
echo_subrequest_async GET /bar;
}
location /bar {
echo $echo_incr;
}
--- request
GET /main
--- response_body
1
2
3
4
5
6
7
=== TEST 24: deep nested echo_subrequest/echo_subrequest_async
--- config
location /main {
echo_subrequest GET /bar?a;
echo_subrequest_async GET /bar?b;
echo_subrequest_async GET /bar?c;
echo_subrequest GET /group?a=d&b=e;
echo_subrequest_async GET /group?a=f&b=g;
}
location /group {
echo_subrequest GET /bar?$arg_a;
echo_subrequest_async GET /bar?$arg_b;
}
location /bar {
echo -n $query_string;
}
--- request
GET /main
--- response_body: abcdefg
--- timeout: 2
=== TEST 25: POST subrequest with file body (relative paths)
--- config
location /main {
echo_subrequest POST /sub -f html/blah.txt;
}
location /sub {
echo "sub method: $echo_request_method";
# we don't need to call echo_read_client_body explicitly here
echo_request_body;
}
--- user_files
>>> blah.txt
Hello, world
--- request
GET /main
--- response_body
sub method: POST
Hello, world
=== TEST 26: POST subrequest with file body (absolute paths)
--- config
location /main {
echo_subrequest POST /sub -f $TEST_NGINX_HTML_DIR/blah.txt;
}
location /sub {
echo "sub method: $echo_request_method";
# we don't need to call echo_read_client_body explicitly here
echo_request_body;
}
--- user_files
>>> blah.txt
Hello, world!
Haha
--- request
GET /main
--- response_body
sub method: POST
Hello, world!
Haha
=== TEST 27: POST subrequest with file body (file not found)
--- config
location /main {
echo_subrequest POST /sub -f html/blah/blah.txt;
}
location /sub {
echo "sub method: $echo_request_method";
# we don't need to call echo_read_client_body explicitly here
echo_request_body;
}
--- user_files
>>> blah.txt
Hello, world
--- request
GET /main
--- ignore_response
--- error_log eval
qr/open\(\) ".*?" failed/
--- no_error_log
[alert]
=== TEST 28: leading subrequest & echo_before_body
--- config
location /main {
echo_before_body hello;
echo_subrequest GET /foo;
}
location /foo {
echo world;
}
--- request
GET /main
--- response_body
hello
world
=== TEST 29: leading subrequest & xss
--- config
location /main {
default_type 'application/json';
xss_get on;
xss_callback_arg c;
echo_subrequest GET /foo;
}
location /foo {
echo -n world;
}
--- request
GET /main?c=hi
--- response_body chop
hi(world);
=== TEST 30: multiple leading subrequest & xss
--- config
location /main {
default_type 'application/json';
xss_get on;
xss_callback_arg c;
echo_subrequest GET /foo;
echo_subrequest GET /bar;
}
location /foo {
echo -n world;
}
location /bar {
echo -n ' people';
}
--- request
GET /main?c=hi
--- response_body chop
hi(world people);
=== TEST 31: sanity (HEAD)
--- config
location /main {
echo_subrequest GET /sub;
echo_subrequest GET /sub;
}
location /sub {
echo hello;
}
--- request
HEAD /main
--- response_body
=== TEST 32: POST subrequest to ngx_proxy
--- config
location /hello {
default_type text/plain;
echo_subrequest POST '/proxy' -q 'foo=Foo&bar=baz' -b 'request_body=test&test=3';
}
location /proxy {
proxy_pass http://127.0.0.1:$TEST_NGINX_CLIENT_PORT/sub;
#proxy_pass http://127.0.0.1:1113/sub;
}
location /sub {
echo_read_request_body;
echo "sub method: $echo_request_method";
# we don't need to call echo_read_client_body explicitly here
echo "sub body: $echo_request_body";
}
--- request
GET /hello
--- response_body
sub method: POST
sub body: request_body=test&test=3
=== TEST 33: HEAD subrequest
--- config
location /main {
echo_subrequest HEAD /sub;
echo_subrequest HEAD /sub;
}
location /sub {
echo hello;
}
--- request
GET /main
--- response_body
=== TEST 34: method name as an nginx variable (github issue #34)
--- config
location ~ ^/delay/(?<delay>[0-9.]+)/(?<originalURL>.*)$ {
# echo_blocking_sleep $delay;
echo_subrequest '$echo_request_method' '/$originalURL' -q '$args';
}
location /api {
echo "args: $args";
}
--- request
GET /delay/0.343/api/?a=b
--- response_body
args: a=b
--- no_error_log
[error]
|