Mercurial > hg > nginx-tests
comparison proxy_implicit.t @ 1633:d57c7877b618
Tests: basic proxy tests with implicit upstream.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 02 Dec 2020 12:40:01 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1632:da1325cb1c39 | 1633:d57c7877b618 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for an upstream implicitly defined by proxy_pass. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use Socket qw/ SOCK_STREAM IPPROTO_TCP AF_INET6 /; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 eval { die if $Socket::VERSION < 1.96; }; | |
28 plan(skip_all => 'Socket too old for getaddrinfo') if $@; | |
29 | |
30 my $t = Test::Nginx->new()->has(qw/http proxy/); | |
31 | |
32 $t->write_file_expand('nginx.conf', <<'EOF'); | |
33 | |
34 %%TEST_GLOBALS%% | |
35 | |
36 daemon off; | |
37 | |
38 events { | |
39 } | |
40 | |
41 http { | |
42 %%TEST_GLOBALS_HTTP%% | |
43 | |
44 server { | |
45 listen 127.0.0.1:8080; | |
46 listen [::1]:%%PORT_8080%%; | |
47 server_name localhost; | |
48 | |
49 location / { | |
50 proxy_pass http://localhost:%%PORT_8080%%/stub; | |
51 proxy_next_upstream http_404; | |
52 add_header X-Addr $upstream_addr always; | |
53 } | |
54 | |
55 location /var { | |
56 proxy_pass http://$arg_b/stub; | |
57 proxy_next_upstream http_404; | |
58 add_header X-Addr $upstream_addr always; | |
59 } | |
60 | |
61 location /stub { } | |
62 } | |
63 } | |
64 | |
65 EOF | |
66 | |
67 $t->try_run('no inet6 support')->plan(3); | |
68 | |
69 ############################################################################### | |
70 | |
71 my $p = port(8080); | |
72 my @addrs = resolve('localhost'); | |
73 my $exp = qr/$addrs[0]:$p/ if @addrs == 1; | |
74 my $v1 = "$addrs[0]:$p", my $v2 = "$addrs[1]:$p" if @addrs == 2; | |
75 $exp = qr/\Q$v1, $v2\E|\Q$v2, $v1\E/ if @addrs == 2; | |
76 die "too many addresses in localhost" if @addrs > 2; | |
77 | |
78 like(http_get('/'), qr/Not Found/, 'implicit upstream'); | |
79 like(http_get('/'), $exp, 'implicit upstream all tried'); | |
80 like(http_get("/var?b=localhost:$p"), qr/Not Found/, | |
81 'implicit upstream by variable'); | |
82 | |
83 ############################################################################### | |
84 | |
85 sub resolve { | |
86 my ($name) = @_; | |
87 | |
88 my $ai_addrconfig = eval { Socket::AI_ADDRCONFIG() }; | |
89 my ($err, @res) = Socket::getaddrinfo($name, "", | |
90 { socktype => SOCK_STREAM, protocol => IPPROTO_TCP, | |
91 flags => $ai_addrconfig }); | |
92 die "Cannot getaddrinfo - $err" if $err; | |
93 | |
94 my @addrs; | |
95 foreach my $ai (@res) { | |
96 my ($err, $addr) = Socket::getnameinfo($ai->{addr}, | |
97 Socket::NI_NUMERICHOST(), Socket::NIx_NOSERV()); | |
98 die "Cannot getnameinfo - $err" if $err; | |
99 $addr = '[' . $addr . ']' if $ai->{family} == AF_INET6; | |
100 push @addrs, $addr; | |
101 } | |
102 return @addrs; | |
103 } | |
104 | |
105 ############################################################################### |