annotate realip.t @ 245:7189255c1b91

Tests: added real_ip_recursive test.
author Andrei Belov <defan@nginx.com>
date Mon, 14 Jan 2013 19:49:41 +0400
parents ea574aae77cb
children bf6b05f03458
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
1 #!/usr/bin/perl
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
2
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
3 # (C) Maxim Dounin
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
4
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
5 # Tests for nginx realip module.
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
6
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
7 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
8
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
9 use warnings;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
10 use strict;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
11
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
12 use Test::More;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
13
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16 use lib 'lib';
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17 use Test::Nginx;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
19 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
20
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
21 select STDERR; $| = 1;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
22 select STDOUT; $| = 1;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
23
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
24 my $t = Test::Nginx->new()->has(qw/http realip/);
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
25
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
26 $t->write_file_expand('nginx.conf', <<'EOF');
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
27
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28 %%TEST_GLOBALS%%
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
29
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
30 daemon off;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
31
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
32 events {
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
33 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
34
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
35 http {
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36 %%TEST_GLOBALS_HTTP%%
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
37
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
38 add_header X-IP $remote_addr;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
39 real_ip_header X-Forwarded-For;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
40
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
41 server {
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
42 listen 127.0.0.1:8080;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
43 server_name localhost;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
44
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
45 location /1 {
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
46 set_real_ip_from 127.0.0.1/32;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
47 set_real_ip_from 10.0.1.0/24;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
48 real_ip_recursive off;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
49 }
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
50
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
51 location /2 {
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
52 set_real_ip_from 127.0.0.1/32;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
53 set_real_ip_from 10.0.1.0/24;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
54 real_ip_recursive on;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
55 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
56 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
57 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
58
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 EOF
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61 $t->write_file('1', '');
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
62 $t->write_file('2', '');
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
63 $t->run();
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
65 plan(skip_all => 'no 127.0.0.1 on host')
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
66 if http_get('/1') !~ /X-IP: 127.0.0.1/m;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
68 $t->plan(4);
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
69
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
70 ###############################################################################
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
72 like(http_xff('/1', '192.0.2.1'), qr/^X-IP: 192.0.2.1/m, 'realip');
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
73 like(http_xff('/1', '10.0.0.1, 192.0.2.1'), qr/^X-IP: 192.0.2.1/m,
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
74 'realip multi');
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
75 like(http_xff('/1', '192.0.2.1, 10.0.1.1, 127.0.0.1'),
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
76 qr/^X-IP: 127.0.0.1/m, 'realip recursive off');
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
77 like(http_xff('/2', '192.0.2.1, 10.0.1.1, 127.0.0.1'),
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
78 qr/^X-IP: 192.0.2.1/m, 'realip recursive on');
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
79
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
80 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
82 sub http_xff {
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
83 my ($uri, $xff) = @_;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
84 return http(<<EOF);
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
85 GET $uri HTTP/1.0
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
86 Host: localhost
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
87 X-Forwarded-For: $xff
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
88
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
89 EOF
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
90 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
91
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
92 ###############################################################################