comparison 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
comparison
equal deleted inserted replaced
932:f9ab0aa6e14e 933:e94d9436a37f
1 #!/usr/bin/perl
2
3 # (C) Andrey Zelenkov
4 # (C) Nginx, Inc.
5
6 # Tests for realip_remote_port variable.
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 qw/ :DEFAULT http_end /;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http realip ipv6 unix/)
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 log_format port $realip_remote_port;
39
40 real_ip_header X-Forwarded-For;
41 set_real_ip_from 127.0.0.1/32;
42 set_real_ip_from ::1/128;
43
44 server {
45 listen [::1]:8081;
46 listen unix:%%TESTDIR%%/unix.sock;
47
48 location / {
49 add_header X-Port $realip_remote_port;
50 }
51 }
52
53 server {
54 listen 127.0.0.1:8080;
55 server_name localhost;
56
57 location / {
58 add_header X-Port $realip_remote_port;
59 }
60
61 location /log {
62 add_header X-Port $realip_remote_port;
63 access_log %%TESTDIR%%/port.log port;
64 }
65
66 location /inet6 {
67 proxy_pass http://[::1]:8081/;
68 }
69
70 location /unix {
71 proxy_pass http://unix:%%TESTDIR%%/unix.sock:/;
72 }
73 }
74 }
75
76 EOF
77
78 $t->write_file('index.html', '');
79 $t->write_file('log', '');
80 $t->try_run('no realip_remote_port')->plan(8);
81
82 ###############################################################################
83
84 my ($sp, $data) = sp_get('/log');
85 like($data, qr/X-Port: $sp/, 'realip port');
86
87 like(http_get('/inet6'), qr/X-Port: \d+/, 'realip port inet6');
88
89 unlike(http_get('/unix'), qr/X-Port/, 'realip port unix');
90
91 # real_ip_header extract port
92
93 like(http_xff('/', '127.0.0.1:9080'), qr/X-Port: 9080/, 'xff');
94 unlike(http_xff('/', '127.0.0.1'), qr/X-Port/, 'xff - no port');
95 like(http_xff('/inet6', '[::1]:9081'), qr/X-Port: 9081/, 'xff - inet6');
96 unlike(http_xff('/inet6', '::1'), qr/X-Port/, 'xff - no port');
97
98 # log
99
100 $t->stop();
101
102 my $log = $t->read_file('/port.log');
103 chomp $log;
104
105 is($sp, $log, 'realip port log');
106
107 ###############################################################################
108
109 sub http_xff {
110 my ($uri, $xff) = @_;
111 return http(<<EOF);
112 GET $uri HTTP/1.0
113 Host: localhost
114 X-Forwarded-For: $xff
115
116 EOF
117 }
118
119 sub sp_get {
120 my $s = http_get(shift, start => 1);
121 return ($s->sockport(), http_end($s));
122 }
123
124 ###############################################################################