http_error_page.t 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for error_page directive.
  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(7)
  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 /redirect200 {
  27. error_page 404 =200 http://example.com/;
  28. return 404;
  29. }
  30. location /redirect497 {
  31. # 497 implies implicit status code change
  32. error_page 497 https://example.com/;
  33. return 497;
  34. }
  35. location /error302redirect {
  36. error_page 302 http://example.com/;
  37. return 302 "first";
  38. }
  39. location /error302return302text {
  40. error_page 302 /return302text;
  41. return 302 "first";
  42. }
  43. location /return302text {
  44. return 302 "http://example.com/";
  45. }
  46. location /error302rewrite {
  47. error_page 302 /rewrite;
  48. return 302 "first";
  49. }
  50. location /rewrite {
  51. rewrite ^ http://example.com/;
  52. }
  53. location /error302directory {
  54. error_page 302 /directory;
  55. return 302 "first";
  56. }
  57. location /directory {
  58. }
  59. location /error302auto {
  60. error_page 302 /auto;
  61. return 302 "first";
  62. }
  63. location /auto/ {
  64. proxy_pass http://127.0.0.1:8081;
  65. }
  66. }
  67. }
  68. EOF
  69. mkdir($t->testdir() . '/directory');
  70. $t->run();
  71. ###############################################################################
  72. # tests for error_page status code change for redirects. problems
  73. # introduced in 0.8.53 and fixed in 0.9.5.
  74. like(http_get('/redirect200'), qr!HTTP!, 'redirect 200');
  75. like(http_get('/redirect497'), qr!HTTP/1.1 302!, 'redirect 497');
  76. TODO: {
  77. local $TODO = 'not yet';
  78. # various tests to see if old location cleared if we happen to redirect
  79. # again in error_page 302
  80. like(http_get('/error302redirect'),
  81. qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
  82. 'error 302 redirect - old location cleared');
  83. like(http_get('/error302return302text'),
  84. qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
  85. 'error 302 return 302 text - old location cleared');
  86. like(http_get('/error302rewrite'),
  87. qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
  88. 'error 302 rewrite - old location cleared');
  89. like(http_get('/error302directory'),
  90. qr{HTTP/1.1 301(?!.*Location: first).*Location: http://}ms,
  91. 'error 302 directory redirect - old location cleared');
  92. like(http_get('/error302auto'),
  93. qr{HTTP/1.1 301(?!.*Location: first).*Location: http://}ms,
  94. 'error 302 auto redirect - old location cleared');
  95. }
  96. ###############################################################################