comparison stream_realip_hostname.t @ 1176:eaa92d810bbb

Tests: realip tests for 'unix:' and hostname in set_real_ip_from.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 16 May 2017 15:45:10 +0300
parents
children 97c8280de681
comparison
equal deleted inserted replaced
1175:717030fd5a94 1176:eaa92d810bbb
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for stream realip module, 'unix:' and hostname in set_real_ip_from.
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 use Test::Nginx::Stream qw/ stream /;
22
23 ###############################################################################
24
25 select STDERR; $| = 1;
26 select STDOUT; $| = 1;
27
28 my $t = Test::Nginx->new()->has(qw/stream stream_return stream_realip unix/)
29 ->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 stream {
39 server {
40 listen unix:%%TESTDIR%%/unix.sock proxy_protocol;
41 listen 127.0.0.1:8080;
42 listen 127.0.0.1:8082 proxy_protocol;
43 return $remote_addr;
44
45 set_real_ip_from unix:;
46 }
47
48 server {
49 listen 127.0.0.1:8081;
50 proxy_pass unix:%%TESTDIR%%/unix.sock;
51 }
52
53 server {
54 listen 127.0.0.1:8085 proxy_protocol;
55 listen unix:%%TESTDIR%%/unix2.sock proxy_protocol;
56 return $remote_addr;
57
58 set_real_ip_from localhost;
59 }
60
61 server {
62 listen 127.0.0.1:8083;
63 proxy_pass 127.0.0.1:8085;
64 }
65
66 server {
67 listen 127.0.0.1:8084;
68 proxy_pass unix:%%TESTDIR%%/unix2.sock;
69 }
70 }
71
72 EOF
73
74 $t->try_run('no stream realip hostnames support');
75
76 plan(skip_all => 'no 127.0.0.1 on host')
77 if http_get('/') ne '127.0.0.1';
78
79 $t->plan(4);
80
81 ###############################################################################
82
83 is(pp_get(8081, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
84 '192.0.2.1', 'realip unix');
85 isnt(pp_get(8082, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
86 '192.0.2.1', 'realip unix - no match');
87
88 is(pp_get(8083, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
89 '192.0.2.1', 'realip hostname');
90 isnt(pp_get(8084, "PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"),
91 '192.0.2.1', 'realip hostname - no match');
92
93 ###############################################################################
94
95 sub pp_get {
96 my ($port, $proxy) = @_;
97 stream(PeerPort => port($port))->io($proxy);
98 }
99
100 ###############################################################################