Mercurial > hg > nginx-tests
comparison proxy_protocol_port.t @ 934:d5762cd27ad9
Tests: added tests for proxy_protocol_port variable.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Mon, 23 May 2016 19:09:03 +0300 |
parents | |
children | e9064d691790 |
comparison
equal
deleted
inserted
replaced
933:e94d9436a37f | 934:d5762cd27ad9 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for proxy_protocol_port variable. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use Socket qw/ CRLF /; | |
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 my $t = Test::Nginx->new()->has(qw/http ipv6 realip/) | |
28 ->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 http { | |
38 %%TEST_GLOBALS_HTTP%% | |
39 | |
40 log_format port $proxy_protocol_port; | |
41 | |
42 server { | |
43 listen 127.0.0.1:8080 proxy_protocol; | |
44 server_name localhost; | |
45 | |
46 add_header X-PP-Port $proxy_protocol_port; | |
47 add_header X-Remote-Port $remote_port; | |
48 | |
49 location /pp { | |
50 real_ip_header proxy_protocol; | |
51 error_page 404 =200 /t; | |
52 | |
53 location /pp/real { | |
54 set_real_ip_from 127.0.0.1/32; | |
55 } | |
56 } | |
57 | |
58 location /log { | |
59 access_log %%TESTDIR%%/port.log port; | |
60 } | |
61 } | |
62 } | |
63 | |
64 EOF | |
65 | |
66 $t->write_file('t', 'SEE-THIS'); | |
67 $t->try_run('no proxy_protocol_port')->plan(8); | |
68 | |
69 ############################################################################### | |
70 | |
71 my $tcp4 = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF; | |
72 my $tcp6 = 'PROXY TCP6 2001:Db8::1 2001:Db8::2 1234 5678' . CRLF; | |
73 my $unk = 'PROXY UNKNOWN 1 2 3 4 5 6' . CRLF; | |
74 | |
75 # realip | |
76 | |
77 like(pp_get('/pp', $tcp4), qr/X-PP-Port: 1234/, 'pp port tcp4'); | |
78 like(pp_get('/pp', $tcp6), qr/X-PP-Port: 1234/, 'pp port tcp6'); | |
79 unlike(pp_get('/pp', $unk), qr/X-PP-Port/, 'pp port unknown'); | |
80 | |
81 # remote_port | |
82 | |
83 like(pp_get('/pp/real', $tcp4), qr/X-Remote-Port: 1234/, 'remote port tcp4'); | |
84 unlike(pp_get('/pp', $tcp4), qr/X-Remote-Port: 1234/, 'no remote port tcp4'); | |
85 like(pp_get('/pp/real', $tcp6), qr/X-Remote-Port: 1234/, 'remote port tcp6'); | |
86 unlike(pp_get('/pp', $tcp6), qr/X-Remote-Port: 1234/, 'no remote port tcp6'); | |
87 | |
88 # log | |
89 | |
90 pp_get('/log', $tcp4); | |
91 | |
92 $t->stop(); | |
93 | |
94 my $log = $t->read_file('/port.log'); | |
95 chomp $log; | |
96 | |
97 is($log, 1234, 'pp port log'); | |
98 | |
99 ############################################################################### | |
100 | |
101 sub pp_get { | |
102 my ($url, $proxy) = @_; | |
103 return http($proxy . <<EOF); | |
104 GET $url HTTP/1.0 | |
105 Host: localhost | |
106 | |
107 EOF | |
108 } | |
109 | |
110 ############################################################################### |