memcached_fake.t 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Test for memcached backend with fake daemon.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. use Socket qw/ CRLF /;
  9. BEGIN { use FindBin; chdir($FindBin::Bin); }
  10. use lib 'lib';
  11. use Test::Nginx;
  12. ###############################################################################
  13. select STDERR; $| = 1;
  14. select STDOUT; $| = 1;
  15. my $t = Test::Nginx->new()->has(qw/http rewrite memcached ssi/)->plan(3)
  16. ->write_file_expand('nginx.conf', <<'EOF');
  17. %%TEST_GLOBALS%%
  18. master_process off;
  19. daemon off;
  20. events {
  21. }
  22. http {
  23. %%TEST_GLOBALS_HTTP%%
  24. server {
  25. listen 127.0.0.1:8080;
  26. server_name localhost;
  27. location / {
  28. set $memcached_key $uri;
  29. memcached_pass 127.0.0.1:8081;
  30. }
  31. location /ssi {
  32. default_type text/html;
  33. ssi on;
  34. }
  35. }
  36. }
  37. EOF
  38. $t->write_file('ssi.html', '<!--#include virtual="/" set="blah" -->blah: <!--#echo var="blah" -->');
  39. $t->run_daemon(\&memcached_fake_daemon);
  40. $t->run();
  41. ###############################################################################
  42. like(http_get('/'), qr/SEE-THIS/, 'memcached split trailer');
  43. like(http_get('/ssi.html'), qr/SEE-THIS/, 'memcached ssi var');
  44. like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no error');
  45. ###############################################################################
  46. sub memcached_fake_daemon {
  47. my $server = IO::Socket::INET->new(
  48. Proto => 'tcp',
  49. LocalAddr => '127.0.0.1:8081',
  50. Listen => 5,
  51. Reuse => 1
  52. )
  53. or die "Can't create listening socket: $!\n";
  54. while (my $client = $server->accept()) {
  55. $client->autoflush(1);
  56. while (<$client>) {
  57. last if (/\x0d\x0a$/);
  58. }
  59. print $client 'VALUE / 0 8' . CRLF;
  60. print $client 'SEE-TH';
  61. select(undef, undef, undef, 0.1);
  62. print $client 'IS';
  63. select(undef, undef, undef, 0.1);
  64. print $client CRLF . 'EN';
  65. select(undef, undef, undef, 0.1);
  66. print $client 'D' . CRLF;
  67. close $client;
  68. }
  69. }
  70. ###############################################################################