Maxim Dounin | a2167d2 | 2013-06-27 19:20:39 +0400 | [diff] [blame] | 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 | |
Sergey Kandaurov | 0b01518 | 2015-05-18 20:37:19 +0300 | [diff] [blame] | 28 | my $t = Test::Nginx->new()->has(qw/http scgi cache shmem/)->plan(10) |
Maxim Dounin | a2167d2 | 2013-06-27 19:20:39 +0400 | [diff] [blame] | 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'); |
Maxim Dounin | a2167d2 | 2013-06-27 19:20:39 +0400 | [diff] [blame] | 73 | like(http_get('/len/empty'), qr/HIT/, 'empty length cached'); |
Maxim Dounin | a2167d2 | 2013-06-27 19:20:39 +0400 | [diff] [blame] | 74 | |
| 75 | like(http_get('/nolen/empty'), qr/MISS/, 'empty no length'); |
| 76 | like(http_get('/nolen/empty'), qr/HIT/, 'empty no length cached'); |
| 77 | |
| 78 | like(http_get('/unfinished'), qr/MISS/, 'unfinished'); |
| 79 | like(http_get('/unfinished'), qr/MISS/, 'unfinished not cached'); |
| 80 | |
| 81 | ############################################################################### |
| 82 | |
| 83 | sub 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 | ############################################################################### |