Skip to content

Commit 7edf3d2

Browse files
Tests: added geo IPv6 tests.
1 parent 5775448 commit 7edf3d2

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

geo_ipv6.t

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Andrey Zelenkov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for geo module with IPv6.
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 geo ipv6/)
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+
geo $geo {
39+
::1/128 loopback;
40+
2001:0db8::/32 test;
41+
::/0 world;
42+
}
43+
44+
geo $geo_delete {
45+
::1/128 loopback;
46+
2001:0db8::/32 test;
47+
::/0 world;
48+
delete ::1/128;
49+
}
50+
51+
geo $geo_proxy {
52+
ranges;
53+
proxy ::1;
54+
default default;
55+
192.0.2.1-192.0.2.1 test;
56+
}
57+
58+
geo $arg_ip $geo_arg {
59+
default default;
60+
::1/128 loopback;
61+
192.0.2.0/24 test;
62+
}
63+
64+
server {
65+
listen 127.0.0.1:8080;
66+
server_name localhost;
67+
68+
location / {
69+
proxy_pass http://[::1]:%%PORT_8080%%/;
70+
}
71+
}
72+
73+
server {
74+
listen [::1]:%%PORT_8080%%;
75+
server_name localhost;
76+
77+
location / {
78+
add_header X-Geo $geo;
79+
add_header X-Del $geo_delete;
80+
add_header X-XFF $geo_proxy;
81+
add_header X-Arg $geo_arg;
82+
}
83+
84+
location /addr {
85+
add_header X-IP $remote_addr;
86+
}
87+
}
88+
}
89+
90+
EOF
91+
92+
$t->write_file('index.html', '');
93+
$t->write_file('addr', '');
94+
$t->try_run('no inet6 support');
95+
96+
plan(skip_all => 'no ::1 on host')
97+
if http_get('/addr') !~ /X-IP: ::1/m;
98+
99+
$t->plan(4);
100+
101+
###############################################################################
102+
103+
like(http_get('/'), qr/^X-Geo: loopback/m, 'geo ipv6');
104+
like(http_get('/'), qr/^X-Del: world/m, 'geo ipv6 delete');
105+
106+
like(http_xff('::ffff:192.0.2.1'), qr/^X-XFF: test/m, 'geo ipv6 ipv4-mapped');
107+
like(http_get('/?ip=::ffff:192.0.2.1'), qr/^X-Arg: test/m,
108+
'geo ipv6 ipv4-mapped from variable');
109+
110+
###############################################################################
111+
112+
sub http_xff {
113+
my ($xff) = @_;
114+
return http(<<EOF);
115+
GET / HTTP/1.0
116+
Host: localhost
117+
X-Forwarded-For: $xff
118+
119+
EOF
120+
}
121+
122+
###############################################################################

0 commit comments

Comments
 (0)