blob: d231e418f31125266033516341d2675a4705e95a [file] [log] [blame]
Maxim Douninabf90c72010-02-16 15:03:09 +03001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Test for memcached backend with fake daemon.
6
7###############################################################################
8
9use warnings;
10use strict;
11
12use Test::More;
13use Socket qw/ CRLF /;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
19
20###############################################################################
21
22select STDERR; $| = 1;
23select STDOUT; $| = 1;
24
Maxim Dounin751d0312010-02-19 22:26:38 +030025my $t = Test::Nginx->new()->has(qw/http rewrite memcached ssi/)->plan(3)
Maxim Douninabf90c72010-02-16 15:03:09 +030026 ->write_file_expand('nginx.conf', <<'EOF');
27
28%%TEST_GLOBALS%%
29
Maxim Dounin35773f62013-01-24 02:17:36 +040030daemon off;
Maxim Douninabf90c72010-02-16 15:03:09 +030031
32events {
33}
34
35http {
36 %%TEST_GLOBALS_HTTP%%
37
38 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030039 listen 127.0.0.1:8080;
Maxim Douninabf90c72010-02-16 15:03:09 +030040 server_name localhost;
41
42 location / {
43 set $memcached_key $uri;
Andrey Zelenkove59bf362016-07-12 17:39:03 +030044 memcached_pass 127.0.0.1:8081;
Maxim Douninabf90c72010-02-16 15:03:09 +030045 }
Maxim Dounin751d0312010-02-19 22:26:38 +030046
47 location /ssi {
48 default_type text/html;
49 ssi on;
50 }
Maxim Douninabf90c72010-02-16 15:03:09 +030051 }
52}
53
54EOF
55
Sergey Kandaurov8d4e0112018-03-01 15:14:43 +030056$t->write_file('ssi.html',
57 '<!--#include virtual="/" set="blah" -->' .
58 'blah: <!--#echo var="blah" -->');
59
Maxim Douninabf90c72010-02-16 15:03:09 +030060$t->run_daemon(\&memcached_fake_daemon);
61$t->run();
62
Andrey Zelenkove59bf362016-07-12 17:39:03 +030063$t->waitforsocket('127.0.0.1:' . port(8081))
Valentin Bartenevc7801d52013-03-20 17:27:01 +040064 or die "Can't start fake memcached";
65
Maxim Douninabf90c72010-02-16 15:03:09 +030066###############################################################################
67
68like(http_get('/'), qr/SEE-THIS/, 'memcached split trailer');
69
Maxim Dounin751d0312010-02-19 22:26:38 +030070like(http_get('/ssi.html'), qr/SEE-THIS/, 'memcached ssi var');
71
Sergey Kandaurov72433042016-08-23 14:04:56 +030072like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors');
Maxim Douninabf90c72010-02-16 15:03:09 +030073
Maxim Douninabf90c72010-02-16 15:03:09 +030074###############################################################################
75
76sub memcached_fake_daemon {
77 my $server = IO::Socket::INET->new(
78 Proto => 'tcp',
Andrey Zelenkove59bf362016-07-12 17:39:03 +030079 LocalAddr => '127.0.0.1:' . port(8081),
Maxim Douninabf90c72010-02-16 15:03:09 +030080 Listen => 5,
81 Reuse => 1
82 )
83 or die "Can't create listening socket: $!\n";
84
Valentin Bartenevc7801d52013-03-20 17:27:01 +040085 local $SIG{PIPE} = 'IGNORE';
86
Maxim Douninabf90c72010-02-16 15:03:09 +030087 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 Dounin751d0312010-02-19 22:26:38 +030095 print $client 'SEE-TH';
96 select(undef, undef, undef, 0.1);
97 print $client 'IS';
Maxim Douninabf90c72010-02-16 15:03:09 +030098 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###############################################################################