annotate realip_remote_port.t @ 933:e94d9436a37f

Tests: added tests for realip_remote_port variable.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 23 May 2016 18:54:18 +0300
parents
children e9064d691790
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
933
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
2
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
3 # (C) Andrey Zelenkov
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
5
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
6 # Tests for realip_remote_port variable.
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
7
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
8 ###############################################################################
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
9
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
10 use warnings;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
11 use strict;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
12
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
13 use Test::More;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
14
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
16
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
17 use lib 'lib';
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
18 use Test::Nginx qw/ :DEFAULT http_end /;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
19
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
20 ###############################################################################
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
21
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
22 select STDERR; $| = 1;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
23 select STDOUT; $| = 1;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
24
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
25 my $t = Test::Nginx->new()->has(qw/http realip ipv6 unix/)
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
26 ->write_file_expand('nginx.conf', <<'EOF');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
27
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
28 %%TEST_GLOBALS%%
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
29
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
30 daemon off;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
31
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
32 events {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
33 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
34
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
35 http {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
36 %%TEST_GLOBALS_HTTP%%
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
37
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
38 log_format port $realip_remote_port;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
39
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
40 real_ip_header X-Forwarded-For;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
41 set_real_ip_from 127.0.0.1/32;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
42 set_real_ip_from ::1/128;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
43
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
44 server {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
45 listen [::1]:8081;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
46 listen unix:%%TESTDIR%%/unix.sock;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
47
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
48 location / {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
49 add_header X-Port $realip_remote_port;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
50 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
51 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
52
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
53 server {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
54 listen 127.0.0.1:8080;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
55 server_name localhost;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
56
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
57 location / {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
58 add_header X-Port $realip_remote_port;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
59 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
60
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
61 location /log {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
62 add_header X-Port $realip_remote_port;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
63 access_log %%TESTDIR%%/port.log port;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
64 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
65
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
66 location /inet6 {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
67 proxy_pass http://[::1]:8081/;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
68 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
69
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
70 location /unix {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
71 proxy_pass http://unix:%%TESTDIR%%/unix.sock:/;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
72 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
73 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
74 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
75
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
76 EOF
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
77
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
78 $t->write_file('index.html', '');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
79 $t->write_file('log', '');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
80 $t->try_run('no realip_remote_port')->plan(8);
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
81
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
82 ###############################################################################
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
83
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
84 my ($sp, $data) = sp_get('/log');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
85 like($data, qr/X-Port: $sp/, 'realip port');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
86
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
87 like(http_get('/inet6'), qr/X-Port: \d+/, 'realip port inet6');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
88
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
89 unlike(http_get('/unix'), qr/X-Port/, 'realip port unix');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
90
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
91 # real_ip_header extract port
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
92
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
93 like(http_xff('/', '127.0.0.1:9080'), qr/X-Port: 9080/, 'xff');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
94 unlike(http_xff('/', '127.0.0.1'), qr/X-Port/, 'xff - no port');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
95 like(http_xff('/inet6', '[::1]:9081'), qr/X-Port: 9081/, 'xff - inet6');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
96 unlike(http_xff('/inet6', '::1'), qr/X-Port/, 'xff - no port');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
97
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
98 # log
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
99
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
100 $t->stop();
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
101
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
102 my $log = $t->read_file('/port.log');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
103 chomp $log;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
104
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
105 is($sp, $log, 'realip port log');
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
106
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
107 ###############################################################################
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
108
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
109 sub http_xff {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
110 my ($uri, $xff) = @_;
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
111 return http(<<EOF);
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
112 GET $uri HTTP/1.0
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
113 Host: localhost
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
114 X-Forwarded-For: $xff
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
115
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
116 EOF
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
117 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
118
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
119 sub sp_get {
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
120 my $s = http_get(shift, start => 1);
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
121 return ($s->sockport(), http_end($s));
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
122 }
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
123
e94d9436a37f Tests: added tests for realip_remote_port variable.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
124 ###############################################################################