|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Zhidao HONG |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for HTTP/2 proxy backend with cache support. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 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 | +my $t = Test::Nginx->new()->has(qw/http proxy cache http_v2/) |
| 26 | + ->plan(15); |
| 27 | + |
| 28 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | +
|
| 30 | +%%TEST_GLOBALS%% |
| 31 | +
|
| 32 | +daemon off; |
| 33 | +
|
| 34 | +events { |
| 35 | +} |
| 36 | +
|
| 37 | +http { |
| 38 | + %%TEST_GLOBALS_HTTP%% |
| 39 | +
|
| 40 | + proxy_cache_path %%TESTDIR%%/cache levels=1:2 |
| 41 | + keys_zone=NAME:1m; |
| 42 | +
|
| 43 | + server { |
| 44 | + listen 127.0.0.1:8080; |
| 45 | + server_name localhost; |
| 46 | +
|
| 47 | + location / { |
| 48 | + proxy_pass http://127.0.0.1:8081; |
| 49 | + proxy_http_version 2; |
| 50 | +
|
| 51 | + proxy_cache NAME; |
| 52 | +
|
| 53 | + proxy_cache_valid 200 302 2s; |
| 54 | + proxy_cache_valid 301 1d; |
| 55 | + proxy_cache_valid any 1m; |
| 56 | +
|
| 57 | + proxy_cache_min_uses 1; |
| 58 | +
|
| 59 | + proxy_cache_use_stale error timeout invalid_header http_500 |
| 60 | + http_404; |
| 61 | +
|
| 62 | + proxy_no_cache $arg_e; |
| 63 | +
|
| 64 | + add_header X-Cache-Status $upstream_cache_status; |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + server { |
| 69 | + listen 127.0.0.1:8081; |
| 70 | + server_name localhost; |
| 71 | +
|
| 72 | + http2 on; |
| 73 | +
|
| 74 | + location / { |
| 75 | + root %%TESTDIR%%; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +EOF |
| 81 | + |
| 82 | +$t->write_file('t.html', 'SEE-THIS'); |
| 83 | +$t->write_file('t2.html', 'SEE-THIS'); |
| 84 | +$t->write_file('empty.html', ''); |
| 85 | +$t->write_file('big.html', 'x' x 1024); |
| 86 | + |
| 87 | +$t->run(); |
| 88 | + |
| 89 | +############################################################################### |
| 90 | + |
| 91 | +like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request'); |
| 92 | + |
| 93 | +$t->write_file('t.html', 'NOOP'); |
| 94 | +like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request cached'); |
| 95 | + |
| 96 | +unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head request'); |
| 97 | +like(http_get('/t2.html'), qr/SEE-THIS/, 'get after head'); |
| 98 | +unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head after get'); |
| 99 | + |
| 100 | +like(http_head('/empty.html?head'), qr/MISS/, 'empty head first'); |
| 101 | +like(http_head('/empty.html?head'), qr/HIT/, 'empty head second'); |
| 102 | + |
| 103 | +like(http_get_range('/t.html', 'Range: bytes=4-'), qr/^THIS/m, 'cached range'); |
| 104 | +like(http_get_range('/t.html', 'Range: bytes=0-2,4-'), qr/^SEE.*^THIS/ms, |
| 105 | + 'cached multipart range'); |
| 106 | + |
| 107 | +like(http_get('/empty.html'), qr/MISS/, 'empty get first'); |
| 108 | +like(http_get('/empty.html'), qr/HIT/, 'empty get second'); |
| 109 | + |
| 110 | +select(undef, undef, undef, 3.1); |
| 111 | +unlink $t->testdir() . '/t.html'; |
| 112 | +like(http_get('/t.html'), qr/STALE/, 'non-empty get stale'); |
| 113 | + |
| 114 | +unlink $t->testdir() . '/empty.html'; |
| 115 | +like(http_get('/empty.html'), qr/STALE/, 'empty get stale'); |
| 116 | + |
| 117 | +# no client connection close with response on non-cacheable HEAD requests |
| 118 | + |
| 119 | +my $s = http(<<EOF, start => 1); |
| 120 | +HEAD /big.html?e=1 HTTP/1.1 |
| 121 | +Host: localhost |
| 122 | +
|
| 123 | +EOF |
| 124 | + |
| 125 | +my $r = http_get('/t.html', socket => $s); |
| 126 | + |
| 127 | +like($r, qr/Connection: keep-alive/, 'non-cacheable head - keepalive'); |
| 128 | +like($r, qr/SEE-THIS/, 'non-cacheable head - second'); |
| 129 | + |
| 130 | +############################################################################### |
| 131 | + |
| 132 | +sub http_get_range { |
| 133 | + my ($url, $extra) = @_; |
| 134 | + return http(<<EOF); |
| 135 | +GET $url HTTP/1.1 |
| 136 | +Host: localhost |
| 137 | +Connection: close |
| 138 | +$extra |
| 139 | +
|
| 140 | +EOF |
| 141 | +} |
| 142 | + |
| 143 | +############################################################################### |
0 commit comments