Skip to content

Commit 1c8825a

Browse files
committed
Tests: add proxy_http2_next_upstream.t for HTTP/2 proxy next_upstream directive.
1 parent 5fed930 commit 1c8825a

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

proxy_http2_next_upstream.t

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Maxim Dounin
4+
# (C) Sergey Kandaurov
5+
# (C) Nginx, Inc.
6+
7+
# Tests for HTTP/2 proxy module, proxy_next_upstream directive.
8+
9+
###############################################################################
10+
11+
use warnings;
12+
use strict;
13+
14+
use Test::More;
15+
16+
BEGIN { use FindBin; chdir($FindBin::Bin); }
17+
18+
use lib 'lib';
19+
use Test::Nginx;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/http http_v2 proxy rewrite/)->plan(9);
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+
upstream u {
41+
server 127.0.0.1:8081 max_fails=2;
42+
server 127.0.0.1:8082;
43+
}
44+
45+
upstream u2 {
46+
server 127.0.0.1:8081;
47+
server 127.0.0.1:8082;
48+
}
49+
50+
server {
51+
listen 127.0.0.1:8080;
52+
server_name localhost;
53+
54+
location / {
55+
proxy_pass http://u;
56+
proxy_http_version 2.0;
57+
proxy_request_buffering off;
58+
proxy_set_header TE "trailers";
59+
proxy_pass_trailers on;
60+
proxy_next_upstream http_500 http_404 invalid_header;
61+
}
62+
63+
location /all/ {
64+
proxy_pass http://u2;
65+
proxy_http_version 2.0;
66+
proxy_request_buffering off;
67+
proxy_set_header TE "trailers";
68+
proxy_pass_trailers on;
69+
proxy_next_upstream http_500 http_404;
70+
error_page 404 /all/404;
71+
proxy_intercept_errors on;
72+
}
73+
74+
location /all/404 {
75+
return 200 "$upstream_addr\n";
76+
}
77+
}
78+
79+
server {
80+
listen 127.0.0.1:8081 http2;
81+
server_name localhost;
82+
83+
location / {
84+
return 404;
85+
}
86+
location /ok {
87+
return 200 "AND-THIS\n";
88+
}
89+
location /500 {
90+
return 500;
91+
}
92+
location /444 {
93+
return 444;
94+
}
95+
96+
location /all/ {
97+
return 404;
98+
}
99+
}
100+
101+
server {
102+
listen 127.0.0.1:8082 http2;
103+
server_name localhost;
104+
105+
location / {
106+
return 200 "TEST-OK-IF-YOU-SEE-THIS\n";
107+
}
108+
109+
location /all/ {
110+
return 404;
111+
}
112+
}
113+
}
114+
115+
EOF
116+
117+
# suppress deprecation warning
118+
119+
open OLDERR, ">&", \*STDERR; close STDERR;
120+
$t->run();
121+
open STDERR, ">&", \*OLDERR;
122+
123+
###############################################################################
124+
125+
my ($p1, $p2) = (port(8081), port(8082));
126+
127+
# check if both request fallback to a backend
128+
# which returns valid response
129+
130+
like(http_get('/'), qr/SEE-THIS/, 'proxy request');
131+
like(http_get('/'), qr/SEE-THIS/, 'second request');
132+
133+
# make sure backend isn't switched off after
134+
# proxy_next_upstream http_404
135+
136+
like(http_get('/ok') . http_get('/ok'), qr/AND-THIS/, 'not down');
137+
138+
# next upstream on invalid_header
139+
140+
like(http_get('/444'), qr/SEE-THIS/, 'request 444');
141+
like(http_get('/444'), qr/SEE-THIS/, 'request 444 second');
142+
143+
# next upstream on http_500
144+
145+
like(http_get('/500'), qr/SEE-THIS/, 'request 500');
146+
like(http_get('/500'), qr/SEE-THIS/, 'request 500 second');
147+
148+
# make sure backend switched off with http_500
149+
150+
unlike(http_get('/ok') . http_get('/ok'), qr/AND-THIS/, 'down after 500');
151+
152+
# make sure all backends are tried once
153+
154+
like(http_get('/all/rr'),
155+
qr/^127.0.0.1:($p1, 127.0.0.1:$p2|$p2, 127.0.0.1:$p1)$/mi,
156+
'all tried once');
157+
158+
###############################################################################

0 commit comments

Comments
 (0)