fastcgi_cache.t 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Test for fastcgi backend with 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;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. eval { require FCGI; };
  15. plan(skip_all => 'FCGI not installed') if $@;
  16. my $t = Test::Nginx->new()->has(qw/http fastcgi cache/)->plan(5)
  17. ->write_file_expand('nginx.conf', <<'EOF');
  18. %%TEST_GLOBALS%%
  19. master_process off;
  20. daemon off;
  21. events {
  22. }
  23. http {
  24. %%TEST_GLOBALS_HTTP%%
  25. fastcgi_cache_path %%TESTDIR%%/cache levels=1:2
  26. keys_zone=NAME:10m;
  27. server {
  28. listen 127.0.0.1:8080;
  29. server_name localhost;
  30. location / {
  31. fastcgi_pass 127.0.0.1:8081;
  32. fastcgi_param REQUEST_URI $request_uri;
  33. fastcgi_cache NAME;
  34. fastcgi_cache_key $request_uri;
  35. fastcgi_cache_valid 302 1m;
  36. }
  37. }
  38. }
  39. EOF
  40. $t->run_daemon(\&fastcgi_daemon);
  41. $t->run();
  42. ###############################################################################
  43. like(http_get('/'), qr/SEE-THIS/, 'fastcgi request');
  44. like(http_get('/'), qr/SEE-THIS/, 'fastcgi request cached');
  45. unlike(http_head('/'), qr/SEE-THIS/, 'no data in cached HEAD');
  46. like(http_get('/stderr'), qr/SEE-THIS/, 'large stderr handled');
  47. like(http_get('/stderr'), qr/SEE-THIS/, 'large stderr cached');
  48. ###############################################################################
  49. sub fastcgi_daemon {
  50. my $socket = FCGI::OpenSocket('127.0.0.1:8081', 5);
  51. my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
  52. $socket);
  53. my $count;
  54. while( $request->Accept() >= 0 ) {
  55. $count++;
  56. if ($ENV{REQUEST_URI} eq '/stderr') {
  57. warn "sample stderr text" x 512;
  58. }
  59. print <<EOF;
  60. Location: http://127.0.0.1:8080/redirect
  61. Content-Type: text/html
  62. SEE-THIS
  63. $count
  64. EOF
  65. }
  66. FCGI::CloseSocket($socket);
  67. }
  68. ###############################################################################