proxy_cache.t 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for http proxy cache.
  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 qw/ :DEFAULT :gzip /;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. my $t = Test::Nginx->new()->has(qw/http proxy cache gzip/)->plan(12)
  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. proxy_cache_path %%TESTDIR%%/cache levels=1:2
  24. keys_zone=NAME:10m;
  25. server {
  26. listen 127.0.0.1:8080;
  27. server_name localhost;
  28. gzip on;
  29. gzip_min_length 0;
  30. location / {
  31. proxy_pass http://127.0.0.1:8081;
  32. proxy_cache NAME;
  33. proxy_cache_valid 200 302 1s;
  34. proxy_cache_valid 301 1d;
  35. proxy_cache_valid any 1m;
  36. proxy_cache_min_uses 1;
  37. proxy_cache_use_stale error timeout invalid_header http_500
  38. http_404;
  39. }
  40. location /fake/ {
  41. proxy_pass http://127.0.0.1:8082;
  42. proxy_cache NAME;
  43. }
  44. }
  45. server {
  46. listen 127.0.0.1:8081;
  47. server_name localhost;
  48. location / {
  49. }
  50. }
  51. }
  52. EOF
  53. $t->write_file('t.html', 'SEE-THIS');
  54. $t->write_file('t2.html', 'SEE-THIS');
  55. $t->write_file('empty.html', '');
  56. $t->run_daemon(\&http_fake_daemon);
  57. $t->run();
  58. ###############################################################################
  59. like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request');
  60. $t->write_file('t.html', 'NOOP');
  61. like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request cached');
  62. unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head request');
  63. like(http_get('/t2.html'), qr/SEE-THIS/, 'get after head');
  64. unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head after get');
  65. like(http_get_range('/t.html', 'Range: bytes=4-'), qr/^THIS/m, 'cached range');
  66. like(http_get_range('/t.html', 'Range: bytes=0-2,4-'), qr/^SEE.*^THIS/ms,
  67. 'cached multipart range');
  68. like(http_get('/empty.html'), qr/HTTP/, 'empty get first');
  69. like(http_get('/empty.html'), qr/HTTP/, 'empty get second');
  70. {
  71. local $TODO = 'not fixed yet';
  72. sleep(2);
  73. unlink $t->testdir() . '/t.html';
  74. like(http_gzip_request('/t.html'),
  75. qr/HTTP.*1c\x0d\x0a.{28}\x0d\x0a0\x0d\x0a\x0d\x0a\z/s,
  76. 'non-empty get stale');
  77. }
  78. {
  79. local $TODO = 'broken in 0.8.31';
  80. unlink $t->testdir() . '/empty.html';
  81. like(http_gzip_request('/empty.html'),
  82. qr/HTTP.*14\x0d\x0a.{20}\x0d\x0a0\x0d\x0a\x0d\x0a\z/s,
  83. 'empty get stale');
  84. }
  85. {
  86. local $TODO = 'patch pending';
  87. http_get('/fake/unfinished');
  88. like(http_get('/fake/unfinished'), qr/unfinished 2/, 'unfinished not cached');
  89. }
  90. ###############################################################################
  91. sub http_get_range {
  92. my ($url, $extra) = @_;
  93. return http(<<EOF);
  94. GET $url HTTP/1.1
  95. Host: localhost
  96. Connection: close
  97. $extra
  98. EOF
  99. }
  100. ###############################################################################
  101. sub http_fake_daemon {
  102. my $server = IO::Socket::INET->new(
  103. Proto => 'tcp',
  104. LocalAddr => '127.0.0.1:8082',
  105. Listen => 5,
  106. Reuse => 1
  107. )
  108. or die "Can't create listening socket: $!\n";
  109. my $num = 0;
  110. while (my $client = $server->accept()) {
  111. $client->autoflush(1);
  112. while (<$client>) {
  113. last if (/^\x0d?\x0a?$/);
  114. }
  115. $num++;
  116. print $client <<"EOF";
  117. HTTP/1.1 200 OK
  118. Content-Length: 100
  119. Cache-Control: max-age=300
  120. Connection: close
  121. unfinished $num
  122. EOF
  123. }
  124. }
  125. ###############################################################################