blob: a0124d23b53969a0da048de83d9d35061fab6894 [file] [log] [blame]
Sergey Kandaurovb7944642021-02-08 17:09:23 +03001#!/usr/bin/perl
2
3# (C) Sergey Kandaurov
4# (C) Nginx, Inc.
5
6# Tests for haproxy protocol with unix socket.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
19use Test::Nginx::Stream;
20
21###############################################################################
22
23select STDERR; $| = 1;
24select STDOUT; $| = 1;
25
26my $t = Test::Nginx->new()
27 ->has(qw/http realip stream stream_realip stream_return unix/)
28 ->plan(5);
29
30$t->write_file_expand('nginx.conf', <<'EOF');
31
32%%TEST_GLOBALS%%
33
34daemon off;
35
36events {
37}
38
39http {
40 %%TEST_GLOBALS_HTTP%%
41
42 server {
43 listen unix:%%TESTDIR%%/unix.sock proxy_protocol;
44 server_name localhost;
45
46 add_header X-IP $remote_addr;
47 add_header X-PP $proxy_protocol_addr;
48 real_ip_header proxy_protocol;
49
50 location / { }
51 location /pp {
52 set_real_ip_from unix:;
53 error_page 404 =200 /t;
54 }
55 }
56}
57
58stream {
59 %%TEST_GLOBALS_STREAM%%
60
61 server {
62 listen unix:%%TESTDIR%%/unix1.sock proxy_protocol;
63 return $remote_addr:$proxy_protocol_addr;
64 }
65
66 server {
67 listen unix:%%TESTDIR%%/unix2.sock proxy_protocol;
68 return $remote_addr:$proxy_protocol_addr;
69
70 set_real_ip_from unix:;
71 }
72
73 server {
74 listen 127.0.0.1:8080;
75 proxy_pass unix:%%TESTDIR%%/unix.sock;
76
77 proxy_protocol on;
78 }
79
80 server {
81 listen 127.0.0.1:8081;
82 proxy_pass unix:%%TESTDIR%%/unix1.sock;
83
84 proxy_protocol on;
85 }
86
87 server {
88 listen 127.0.0.1:8082;
89 proxy_pass unix:%%TESTDIR%%/unix2.sock;
90
91 proxy_protocol on;
92 }
93}
94
95EOF
96
97$t->write_file('t', 'SEE-THIS');
98$t->run();
99
100###############################################################################
101
102my $r = http_get('/t');
103like($r, qr/X-IP: unix/, 'remote_addr');
104like($r, qr/X-PP: 127.0.0.1/, 'proxy_protocol_addr');
105
106$r = http_get('/pp');
107like($r, qr/X-IP: 127.0.0.1/, 'remote_addr realip');
108
109# listen proxy_protocol in stream
110
111is(get(8081), 'unix::127.0.0.1', 'stream proxy_protocol');
112is(get(8082), '127.0.0.1:127.0.0.1', 'stream proxy_protocol realip');
113
114###############################################################################
115
116sub get {
117 Test::Nginx::Stream->new(PeerPort => port(shift))->read();
118}
119
120###############################################################################