Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Maxim Dounin |
| 4 | |
| 5 | # Test for memcached backend with fake daemon. |
| 6 | |
| 7 | ############################################################################### |
| 8 | |
| 9 | use warnings; |
| 10 | use strict; |
| 11 | |
| 12 | use Test::More; |
| 13 | use Socket qw/ CRLF /; |
| 14 | |
| 15 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | |
| 17 | use lib 'lib'; |
| 18 | use Test::Nginx; |
| 19 | |
| 20 | ############################################################################### |
| 21 | |
| 22 | select STDERR; $| = 1; |
| 23 | select STDOUT; $| = 1; |
| 24 | |
Maxim Dounin | 751d031 | 2010-02-19 22:26:38 +0300 | [diff] [blame] | 25 | my $t = Test::Nginx->new()->has(qw/http rewrite memcached ssi/)->plan(3) |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 26 | ->write_file_expand('nginx.conf', <<'EOF'); |
| 27 | |
| 28 | %%TEST_GLOBALS%% |
| 29 | |
Maxim Dounin | 35773f6 | 2013-01-24 02:17:36 +0400 | [diff] [blame] | 30 | daemon off; |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 31 | |
| 32 | events { |
| 33 | } |
| 34 | |
| 35 | http { |
| 36 | %%TEST_GLOBALS_HTTP%% |
| 37 | |
| 38 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 39 | listen 127.0.0.1:8080; |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 40 | server_name localhost; |
| 41 | |
| 42 | location / { |
| 43 | set $memcached_key $uri; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 44 | memcached_pass 127.0.0.1:8081; |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 45 | } |
Maxim Dounin | 751d031 | 2010-02-19 22:26:38 +0300 | [diff] [blame] | 46 | |
| 47 | location /ssi { |
| 48 | default_type text/html; |
| 49 | ssi on; |
| 50 | } |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | EOF |
| 55 | |
Sergey Kandaurov | 8d4e011 | 2018-03-01 15:14:43 +0300 | [diff] [blame] | 56 | $t->write_file('ssi.html', |
| 57 | '<!--#include virtual="/" set="blah" -->' . |
| 58 | 'blah: <!--#echo var="blah" -->'); |
| 59 | |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 60 | $t->run_daemon(\&memcached_fake_daemon); |
| 61 | $t->run(); |
| 62 | |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 63 | $t->waitforsocket('127.0.0.1:' . port(8081)) |
Valentin Bartenev | c7801d5 | 2013-03-20 17:27:01 +0400 | [diff] [blame] | 64 | or die "Can't start fake memcached"; |
| 65 | |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 66 | ############################################################################### |
| 67 | |
| 68 | like(http_get('/'), qr/SEE-THIS/, 'memcached split trailer'); |
| 69 | |
Maxim Dounin | 751d031 | 2010-02-19 22:26:38 +0300 | [diff] [blame] | 70 | like(http_get('/ssi.html'), qr/SEE-THIS/, 'memcached ssi var'); |
| 71 | |
Sergey Kandaurov | 7243304 | 2016-08-23 14:04:56 +0300 | [diff] [blame] | 72 | like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors'); |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 73 | |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 74 | ############################################################################### |
| 75 | |
| 76 | sub memcached_fake_daemon { |
| 77 | my $server = IO::Socket::INET->new( |
| 78 | Proto => 'tcp', |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 79 | LocalAddr => '127.0.0.1:' . port(8081), |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 80 | Listen => 5, |
| 81 | Reuse => 1 |
| 82 | ) |
| 83 | or die "Can't create listening socket: $!\n"; |
| 84 | |
Valentin Bartenev | c7801d5 | 2013-03-20 17:27:01 +0400 | [diff] [blame] | 85 | local $SIG{PIPE} = 'IGNORE'; |
| 86 | |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 87 | while (my $client = $server->accept()) { |
| 88 | $client->autoflush(1); |
| 89 | |
| 90 | while (<$client>) { |
| 91 | last if (/\x0d\x0a$/); |
| 92 | } |
| 93 | |
| 94 | print $client 'VALUE / 0 8' . CRLF; |
Maxim Dounin | 751d031 | 2010-02-19 22:26:38 +0300 | [diff] [blame] | 95 | print $client 'SEE-TH'; |
| 96 | select(undef, undef, undef, 0.1); |
| 97 | print $client 'IS'; |
Maxim Dounin | abf90c7 | 2010-02-16 15:03:09 +0300 | [diff] [blame] | 98 | select(undef, undef, undef, 0.1); |
| 99 | print $client CRLF . 'EN'; |
| 100 | select(undef, undef, undef, 0.1); |
| 101 | print $client 'D' . CRLF; |
| 102 | close $client; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | ############################################################################### |