Mercurial > hg > nginx-tests
comparison proxy_protocol2_port.t @ 1309:3eb1de7e3b41
Tests: proxy protocol v2 tests.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 27 Mar 2018 13:12:50 +0300 |
parents | |
children | 97c8280de681 |
comparison
equal
deleted
inserted
replaced
1308:58fdd9515f81 | 1309:3eb1de7e3b41 |
---|---|
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 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 realip/); | |
26 | |
27 plan(skip_all => 'no proxy protocol v2') unless $t->has_version('1.13.11'); | |
28 | |
29 $t->write_file_expand('nginx.conf', <<'EOF')->plan(8); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 http { | |
39 %%TEST_GLOBALS_HTTP%% | |
40 | |
41 log_format port $proxy_protocol_port; | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080 proxy_protocol; | |
45 server_name localhost; | |
46 | |
47 add_header X-PP-Port $proxy_protocol_port; | |
48 add_header X-Remote-Port $remote_port; | |
49 | |
50 location /pp { | |
51 real_ip_header proxy_protocol; | |
52 error_page 404 =200 /t; | |
53 | |
54 location /pp/real { | |
55 set_real_ip_from 127.0.0.1/32; | |
56 } | |
57 } | |
58 | |
59 location /log { | |
60 access_log %%TESTDIR%%/port.log port; | |
61 } | |
62 } | |
63 } | |
64 | |
65 EOF | |
66 | |
67 $t->write_file('t', 'SEE-THIS'); | |
68 $t->run(); | |
69 | |
70 ############################################################################### | |
71 | |
72 my $p = pack("N3C", 0x0D0A0D0A, 0x000D0A51, 0x5549540A, 0x21); | |
73 my $tcp4 = $p . pack("CnN2n2", 0x11, 12, 0xc0000201, 0xc0000202, 123, 5678); | |
74 my $tcp6 = $p . pack("CnNx8NNx8Nn2", 0x21, 36, | |
75 0x20010db8, 0x00000001, 0x20010db8, 0x00000002, 123, 5678); | |
76 my $unk = $p . pack("CnC4", 0x44, 4, 1, 2, 3, 4); | |
77 | |
78 # realip | |
79 | |
80 like(pp_get('/pp', $tcp4), qr/X-PP-Port: 123\x0d/, 'pp port tcp4'); | |
81 like(pp_get('/pp', $tcp6), qr/X-PP-Port: 123\x0d/, 'pp port tcp6'); | |
82 unlike(pp_get('/pp', $unk), qr/X-PP-Port/, 'pp port unknown'); | |
83 | |
84 # remote_port | |
85 | |
86 like(pp_get('/pp/real', $tcp4), qr/X-Remote-Port: 123\x0d/, 'remote port tcp4'); | |
87 unlike(pp_get('/pp', $tcp4), qr/X-Remote-Port: 123\x0d/, 'no remote port tcp4'); | |
88 like(pp_get('/pp/real', $tcp6), qr/X-Remote-Port: 123\x0d/, 'remote port tcp6'); | |
89 unlike(pp_get('/pp', $tcp6), qr/X-Remote-Port: 123\x0d/, 'no remote port tcp6'); | |
90 | |
91 # log | |
92 | |
93 pp_get('/log', $tcp4); | |
94 | |
95 $t->stop(); | |
96 | |
97 my $log = $t->read_file('/port.log'); | |
98 chomp $log; | |
99 | |
100 is($log, 123, 'pp port log'); | |
101 | |
102 ############################################################################### | |
103 | |
104 sub pp_get { | |
105 my ($url, $proxy) = @_; | |
106 return http($proxy . <<EOF); | |
107 GET $url HTTP/1.0 | |
108 Host: localhost | |
109 | |
110 EOF | |
111 } | |
112 | |
113 ############################################################################### |