blob: ec33d0361f0b5de9ff6eaa69f511a0cf92201c40 [file] [log] [blame]
Maxim Dounina2167d22013-06-27 19:20:39 +04001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Tests for scgi_cache.
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
25eval { require SCGI; };
26plan(skip_all => 'SCGI not installed') if $@;
27
Sergey Kandaurov0b015182015-05-18 20:37:19 +030028my $t = Test::Nginx->new()->has(qw/http scgi cache shmem/)->plan(10)
Maxim Dounina2167d22013-06-27 19:20:39 +040029 ->write_file_expand('nginx.conf', <<'EOF');
30
31%%TEST_GLOBALS%%
32
33daemon off;
34
35events {
36}
37
38http {
39 %%TEST_GLOBALS_HTTP%%
40
41 scgi_cache_path %%TESTDIR%%/cache keys_zone=one:1m;
42 scgi_cache_key $request_uri;
43
44 add_header X-Cache-Status $upstream_cache_status;
45
46 server {
47 listen 127.0.0.1:8080;
48 server_name localhost;
49
50 location / {
51 scgi_pass 127.0.0.1:8081;
52 scgi_param SCGI 1;
53 scgi_param REQUEST_URI $uri;
54 scgi_cache one;
55 }
56 }
57}
58
59EOF
60
61$t->run_daemon(\&scgi_daemon);
62$t->run();
63
64###############################################################################
65
66like(http_get('/len'), qr/MISS/, 'length');
67like(http_get('/len'), qr/HIT/, 'length cached');
68
69like(http_get('/nolen'), qr/MISS/, 'no length');
70like(http_get('/nolen'), qr/HIT/, 'no length cached');
71
72like(http_get('/len/empty'), qr/MISS/, 'empty length');
Maxim Dounina2167d22013-06-27 19:20:39 +040073like(http_get('/len/empty'), qr/HIT/, 'empty length cached');
Maxim Dounina2167d22013-06-27 19:20:39 +040074
75like(http_get('/nolen/empty'), qr/MISS/, 'empty no length');
76like(http_get('/nolen/empty'), qr/HIT/, 'empty no length cached');
77
78like(http_get('/unfinished'), qr/MISS/, 'unfinished');
79like(http_get('/unfinished'), qr/MISS/, 'unfinished not cached');
80
81###############################################################################
82
83sub scgi_daemon {
84 my $server = IO::Socket::INET->new(
85 Proto => 'tcp',
86 LocalHost => '127.0.0.1:8081',
87 Listen => 5,
88 Reuse => 1
89 )
90 or die "Can't create listening socket: $!\n";
91
92 my $scgi = SCGI->new($server, blocking => 1);
93 my %count;
94
95 while (my $request = $scgi->accept()) {
96 $request->read_env();
97
98 my $uri = $request->env->{REQUEST_URI} || '';
99 my $c = $request->connection();
100
101 $count{$uri} ||= 0;
102 $count{$uri}++;
103
104 if ($uri eq '/len') {
105 $c->print(
106 "Content-Length: 9" . CRLF .
107 "Content-Type: text/html" . CRLF .
108 "Cache-Control: max-age=300" . CRLF . CRLF .
109 "test body"
110 );
111
112 } elsif ($uri eq '/nolen') {
113 $c->print(
114 "Content-Type: text/html" . CRLF .
115 "Cache-Control: max-age=300" . CRLF . CRLF .
116 "test body"
117 );
118
119 } elsif ($uri eq '/len/empty') {
120 $c->print(
121 "Content-Length: 0" . CRLF .
122 "Content-Type: text/html" . CRLF .
123 "Cache-Control: max-age=300" . CRLF . CRLF
124 );
125
126 } elsif ($uri eq '/nolen/empty') {
127 $c->print(
128 "Content-Type: text/html" . CRLF .
129 "Cache-Control: max-age=300" . CRLF . CRLF
130 );
131
132 } elsif ($uri eq '/unfinished') {
133 $c->print(
134 "Content-Length: 10" . CRLF .
135 "Content-Type: text/html" . CRLF .
136 "Cache-Control: max-age=300" . CRLF . CRLF
137 );
138 }
139 }
140}
141
142###############################################################################