comparison scgi_cache.t @ 308:26147426718c

Tests: scgi cache tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 27 Jun 2013 19:20:39 +0400
parents
children e9de4da234c0
comparison
equal deleted inserted replaced
307:81c98592661f 308:26147426718c
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for scgi_cache.
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
25 eval { require SCGI; };
26 plan(skip_all => 'SCGI not installed') if $@;
27
28 my $t = Test::Nginx->new()->has(qw/http scgi cache/)->plan(10)
29 ->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 http {
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
59 EOF
60
61 $t->run_daemon(\&scgi_daemon);
62 $t->run();
63
64 ###############################################################################
65
66 like(http_get('/len'), qr/MISS/, 'length');
67 like(http_get('/len'), qr/HIT/, 'length cached');
68
69 like(http_get('/nolen'), qr/MISS/, 'no length');
70 like(http_get('/nolen'), qr/HIT/, 'no length cached');
71
72 like(http_get('/len/empty'), qr/MISS/, 'empty length');
73
74 TODO: {
75 local $TODO = 'not yet';
76
77 like(http_get('/len/empty'), qr/HIT/, 'empty length cached');
78 }
79
80 like(http_get('/nolen/empty'), qr/MISS/, 'empty no length');
81 like(http_get('/nolen/empty'), qr/HIT/, 'empty no length cached');
82
83 like(http_get('/unfinished'), qr/MISS/, 'unfinished');
84 like(http_get('/unfinished'), qr/MISS/, 'unfinished not cached');
85
86 ###############################################################################
87
88 sub scgi_daemon {
89 my $server = IO::Socket::INET->new(
90 Proto => 'tcp',
91 LocalHost => '127.0.0.1:8081',
92 Listen => 5,
93 Reuse => 1
94 )
95 or die "Can't create listening socket: $!\n";
96
97 my $scgi = SCGI->new($server, blocking => 1);
98 my %count;
99
100 while (my $request = $scgi->accept()) {
101 $request->read_env();
102
103 my $uri = $request->env->{REQUEST_URI} || '';
104 my $c = $request->connection();
105
106 $count{$uri} ||= 0;
107 $count{$uri}++;
108
109 if ($uri eq '/len') {
110 $c->print(
111 "Content-Length: 9" . CRLF .
112 "Content-Type: text/html" . CRLF .
113 "Cache-Control: max-age=300" . CRLF . CRLF .
114 "test body"
115 );
116
117 } elsif ($uri eq '/nolen') {
118 $c->print(
119 "Content-Type: text/html" . CRLF .
120 "Cache-Control: max-age=300" . CRLF . CRLF .
121 "test body"
122 );
123
124 } elsif ($uri eq '/len/empty') {
125 $c->print(
126 "Content-Length: 0" . CRLF .
127 "Content-Type: text/html" . CRLF .
128 "Cache-Control: max-age=300" . CRLF . CRLF
129 );
130
131 } elsif ($uri eq '/nolen/empty') {
132 $c->print(
133 "Content-Type: text/html" . CRLF .
134 "Cache-Control: max-age=300" . CRLF . CRLF
135 );
136
137 } elsif ($uri eq '/unfinished') {
138 $c->print(
139 "Content-Length: 10" . CRLF .
140 "Content-Type: text/html" . CRLF .
141 "Cache-Control: max-age=300" . CRLF . CRLF
142 );
143 }
144 }
145 }
146
147 ###############################################################################