Skip to content

Commit df1e8d9

Browse files
committed
Tests: add improved HTTP/2 proxy tests based on standard proxy tests.
1 parent 15fc40a commit df1e8d9

File tree

3 files changed

+362
-0
lines changed

3 files changed

+362
-0
lines changed

proxy_http2_cache_2.t

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.0;
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+
###############################################################################

proxy_http2_cache_convert_head_2.t

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Zhidao HONG
4+
# (C) Nginx, Inc.
5+
6+
# Tests for HTTP/2 proxy backend cache with proxy_cache_convert_head directive.
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/)->plan(8)
26+
->write_file_expand('nginx.conf', <<'EOF');
27+
28+
%%TEST_GLOBALS%%
29+
30+
daemon off;
31+
32+
events {
33+
}
34+
35+
http {
36+
%%TEST_GLOBALS_HTTP%%
37+
38+
proxy_cache_path %%TESTDIR%%/cache levels=1:2
39+
keys_zone=NAME:1m;
40+
41+
server {
42+
listen 127.0.0.1:8080;
43+
server_name localhost;
44+
45+
proxy_cache NAME;
46+
47+
proxy_cache_key $request_uri;
48+
49+
proxy_cache_valid 200 302 2s;
50+
51+
add_header X-Cache-Status $upstream_cache_status;
52+
53+
location / {
54+
proxy_pass http://127.0.0.1:8081/t.html;
55+
proxy_http_version 2.0;
56+
proxy_cache_convert_head off;
57+
58+
location /inner {
59+
proxy_pass http://127.0.0.1:8081/t.html;
60+
proxy_http_version 2.0;
61+
proxy_cache_convert_head on;
62+
}
63+
}
64+
65+
location /on {
66+
proxy_pass http://127.0.0.1:8081/t.html;
67+
proxy_http_version 2.0;
68+
proxy_cache_convert_head on;
69+
}
70+
}
71+
72+
server {
73+
listen 127.0.0.1:8081;
74+
server_name localhost;
75+
76+
http2 on;
77+
78+
location / {
79+
root %%TESTDIR%%;
80+
add_header X-Method $request_method;
81+
}
82+
}
83+
}
84+
85+
EOF
86+
87+
$t->write_file('t.html', 'SEE-THIS');
88+
$t->run();
89+
90+
###############################################################################
91+
92+
like(http_get('/'), qr/x-method: GET/i, 'get');
93+
like(http_head('/?2'), qr/x-method: HEAD/i, 'head');
94+
like(http_head('/?2'), qr/HIT/, 'head cached');
95+
unlike(http_get('/?2'), qr/SEE-THIS/, 'get after head');
96+
97+
like(http_get('/on'), qr/x-method: GET/i, 'on - get');
98+
like(http_head('/on?2'), qr/x-method: GET/i, 'on - head');
99+
100+
like(http_get('/inner'), qr/x-method: GET/i, 'inner - get');
101+
like(http_head('/inner?2'), qr/x-method: GET/i, 'inner - head');
102+
103+
###############################################################################

proxy_http2_store_2.t

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Zhidao HONG
4+
# (C) Nginx, Inc.
5+
6+
# Tests for HTTP/2 proxy backend with proxy_store functionality.
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();
26+
27+
$t->write_file_expand('nginx.conf', <<'EOF')->has(qw/http proxy ssi http_v2/)->plan(9);
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
33+
events {
34+
}
35+
36+
http {
37+
%%TEST_GLOBALS_HTTP%%
38+
39+
server {
40+
listen 127.0.0.1:8080;
41+
server_name localhost;
42+
43+
location /store- {
44+
proxy_pass http://127.0.0.1:8081/;
45+
proxy_http_version 2.0;
46+
proxy_store on;
47+
}
48+
location /store-string- {
49+
proxy_pass http://127.0.0.1:8081/;
50+
proxy_http_version 2.0;
51+
proxy_store %%TESTDIR%%$uri;
52+
}
53+
location /ssi.html {
54+
ssi on;
55+
}
56+
location /index-big.html {
57+
limit_rate 200k;
58+
}
59+
}
60+
61+
server {
62+
listen 127.0.0.1:8081;
63+
server_name localhost;
64+
65+
http2 on;
66+
67+
location / {
68+
root %%TESTDIR%%;
69+
}
70+
}
71+
}
72+
73+
EOF
74+
75+
$t->write_file('index.html', 'SEE-THIS');
76+
$t->write_file('index-nostore.html', 'SEE-THIS');
77+
$t->write_file('index-big.html', 'x' x (100 << 10));
78+
$t->write_file('ssi.html',
79+
'<!--#include virtual="/store-index-big.html?1" -->' .
80+
'<!--#include virtual="/store-index-big.html?2" -->'
81+
);
82+
$t->run();
83+
84+
###############################################################################
85+
86+
like(http_get('/store-index.html'), qr/SEE-THIS/, 'proxy request');
87+
ok(-e $t->testdir() . '/store-index.html', 'result stored');
88+
89+
like(http_get('/store-string-index.html'), qr/SEE-THIS/,
90+
'proxy string path request');
91+
ok(-e $t->testdir() . '/store-string-index.html', 'string path result stored');
92+
93+
like(http_head('/store-index-nostore.html'), qr/200 OK/, 'head request');
94+
ok(!-e $t->testdir() . '/store-index-nostore.html', 'result not stored');
95+
96+
ok(scalar @{[ glob $t->testdir() . '/proxy_temp/*' ]} == 0, 'no temp files');
97+
98+
http_get('/store-index-big.html', aborted => 1, sleep => 0.1);
99+
100+
select(undef, undef, undef, 0.5);
101+
select(undef, undef, undef, 2.5)
102+
if scalar @{[ glob $t->testdir() . '/proxy_temp/*' ]};
103+
104+
ok(scalar @{[ glob $t->testdir() . '/proxy_temp/*' ]} == 0,
105+
'no temp files after aborted request');
106+
107+
http_get('/ssi.html', aborted => 1, sleep => 0.1);
108+
109+
select(undef, undef, undef, 0.5);
110+
select(undef, undef, undef, 2.5)
111+
if scalar @{[ glob $t->testdir() . '/proxy_temp/*' ]};
112+
113+
ok(scalar @{[ glob $t->testdir() . '/proxy_temp/*' ]} == 0,
114+
'no temp files after aborted ssi');
115+
116+
###############################################################################

0 commit comments

Comments
 (0)