comparison realip_remote_port.t @ 1057:e19d41512e22

Tests: added tests for $realip_remote_port variable.
author Andrey Zelenkov <zelenkov@nginx.com>
date Fri, 14 Oct 2016 14:34:44 +0300
parents
children 766bcbb632ee
comparison
equal deleted inserted replaced
1056:1e41a0de0772 1057:e19d41512e22
1 #!/usr/bin/perl
2
3 # (C) Andrey Zelenkov
4 # (C) Nginx, Inc.
5
6 # Tests for nginx realip module, $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/)
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 set_real_ip_from 127.0.0.1/32;
39 real_ip_header X-Forwarded-For;
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 location / {
46 add_header X-IP $remote_addr;
47 add_header X-Real-Port $realip_remote_port;
48 }
49 }
50 }
51
52 EOF
53
54 $t->write_file('index.html', '');
55 $t->write_file('1', '');
56 $t->try_run('no realip_remote_port');
57
58 plan(skip_all => 'no 127.0.0.1 on host')
59 if http_get('/') !~ /X-IP: 127.0.0.1/m;
60
61 $t->plan(4);
62
63 ###############################################################################
64
65 my ($sp, $data) = http_sp_get('/1');
66 like($data, qr/X-Real-Port: $sp/, 'request');
67
68 ($sp, $data) = http_sp_get('/');
69 like($data, qr/X-Real-Port: $sp/, 'request redirect');
70
71 ($sp, $data) = http_sp_xff('/1', '127.0.0.1:123');
72 like($data, qr/X-Real-Port: $sp/, 'realip');
73
74 TODO: {
75 local $TODO = 'not yet' unless $t->has_version('1.11.5');
76
77 ($sp, $data) = http_sp_xff('/', '127.0.0.1:123');
78 like($data, qr/X-Real-Port: $sp/, 'realip redirect');
79
80 }
81
82 ###############################################################################
83
84 sub http_sp_get {
85 my $s = http_get(shift, start => 1);
86 return ($s->sockport(), http_end($s));
87 }
88
89 sub http_sp_xff {
90 my ($url, $xff) = @_;
91
92 my $s = http(<<EOF, start => 1);
93 GET $url HTTP/1.0
94 Host: localhost
95 X-Forwarded-For: $xff
96
97 EOF
98
99 return ($s->sockport(), http_end($s));
100 }
101
102 ###############################################################################