rewrite.t 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for rewrite module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. BEGIN { use FindBin; chdir($FindBin::Bin); }
  9. use lib 'lib';
  10. use Test::Nginx;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(19)
  15. ->write_file_expand('nginx.conf', <<'EOF');
  16. %%TEST_GLOBALS%%
  17. master_process off;
  18. daemon off;
  19. events {
  20. }
  21. http {
  22. %%TEST_GLOBALS_HTTP%%
  23. server {
  24. listen 127.0.0.1:8080;
  25. server_name localhost;
  26. location / {
  27. rewrite ^ http://example.com/ redirect;
  28. }
  29. location /add {
  30. rewrite ^ http://example.com/?c=d redirect;
  31. }
  32. location /no {
  33. rewrite ^ http://example.com/?c=d? redirect;
  34. }
  35. location /return204 {
  36. return 204;
  37. }
  38. location /return200 {
  39. return 200;
  40. }
  41. location /return405 {
  42. return 405;
  43. }
  44. location /error404return405 {
  45. error_page 404 /return405;
  46. return 404;
  47. }
  48. location /error405return204 {
  49. error_page 405 /return204;
  50. return 405;
  51. }
  52. location /error405return200 {
  53. error_page 405 /return200;
  54. return 405;
  55. }
  56. location /return200text {
  57. return 200 "text";
  58. }
  59. location /return404text {
  60. return 404 "text";
  61. }
  62. location /return302text {
  63. return 302 "text";
  64. }
  65. location /error405return200text {
  66. error_page 405 /return200text;
  67. return 405;
  68. }
  69. location /error302return200text {
  70. error_page 302 /return200text;
  71. return 302 "text";
  72. }
  73. location /error405return302text {
  74. error_page 405 /return302text;
  75. return 405;
  76. }
  77. location /error405rewrite {
  78. error_page 405 /;
  79. return 405;
  80. }
  81. location /error405directory {
  82. error_page 405 /directory;
  83. return 405;
  84. }
  85. location /directory {
  86. }
  87. }
  88. }
  89. EOF
  90. mkdir($t->testdir() . '/directory');
  91. $t->run();
  92. ###############################################################################
  93. like(http_get('/'), qr!^Location: http://example.com/\x0d?$!ms, 'simple');
  94. like(http_get('/?a=b'), qr!^Location: http://example.com/\?a=b\x0d?$!ms,
  95. 'simple with args');
  96. like(http_get('/add'), qr!^Location: http://example.com/\?c=d\x0d?$!ms,
  97. 'add args');
  98. like(http_get('/add?a=b'), qr!^Location: http://example.com/\?c=d&a=b\x0d?$!ms,
  99. 'add args with args');
  100. like(http_get('/no?a=b'), qr!^Location: http://example.com/\?c=d\x0d?$!ms,
  101. 'no args with args');
  102. like(http_get('/return204'), qr!204 No Content!, 'return 204');
  103. like(http_get('/return200'), qr!200 OK!, 'return 200');
  104. like(http_get('/return405'), qr!HTTP/1.1 405.*body!ms, 'return 405');
  105. like(http_get('/error404return405'), qr!HTTP/1.1 404!, 'error 404 return 405');
  106. TODO: {
  107. local $TODO = 'not yet';
  108. # status code should be 405, and entity body is expected (vs. normal 204
  109. # replies which doesn't expect to have body); use HTTP/1.1 for test
  110. # to make problem clear
  111. my $r = http(<<EOF);
  112. GET /error405return204 HTTP/1.1
  113. Host: localhost
  114. Connection: close
  115. EOF
  116. like($r, qr/HTTP\/1.1 405.*(Content-Length|\x0d\0a0\x0d\x0a)/ms,
  117. 'error 405 return 204');
  118. # the same test, but with return 200. this doesn't have special
  119. # handling and returns builtin error page body (the same problem as
  120. # in /error405return200text below)
  121. like(http_get('/error405return200'), qr/HTTP\/1.1 405(?!.*body)/ms,
  122. 'error 405 return 200');
  123. }
  124. # tests involving return with two arguments, as introduced in
  125. # 0.8.42
  126. like(http_get('/return200text'), qr!text\z!, 'return 200 text');
  127. like(http_get('/return404text'), qr!text\z!, 'return 404 text');
  128. TODO: {
  129. local $TODO = 'not yet';
  130. like(http_get('/error405return200text'), qr!HTTP/1.1 405.*text\z!ms,
  131. 'error 405 to return 200 text');
  132. }
  133. # return 302 is somewhat special: it adds Location header instead of
  134. # body text. additionally it doesn't sent reply directly (as it's done for
  135. # other returns since 0.8.42) but instead returns NGX_HTTP_* code
  136. like(http_get('/return302text'), qr!HTTP/1.1 302.*Location: text!ms,
  137. 'return 302 text');
  138. TODO: {
  139. local $TODO = 'not yet';
  140. like(http_get('/error302return200text'),
  141. qr!HTTP/1.1 302.*Location: text.*text\z!ms,
  142. 'error 302 return 200 text');
  143. }
  144. TODO: {
  145. local $TODO = 'not yet';
  146. # in contrast to other return's this shouldn't preserve original status code
  147. # from error, and the same applies to "rewrite ... redirect" as an error
  148. # handler; both should in line with e.g. directory redirect as well
  149. like(http_get('/error405return302text'),
  150. qr!HTTP/1.1 302.*Location: text!ms,
  151. 'error 405 return 302 text');
  152. like(http_get('/error405rewrite'),
  153. qr!HTTP/1.1 302.*Location: http://example.com/!ms,
  154. 'error 405 rewrite redirect');
  155. }
  156. like(http_get('/error405directory'),
  157. qr!HTTP/1.1 301.*Location: http://!ms,
  158. 'error 405 directory redirect');
  159. ###############################################################################