annotate realip_remote_addr.t @ 836:a9c4cebcfe69

Tests: adjusted http resolver test with resend. Don't bother with socket error to force resolver resend. Nginx doesn't handle well ECONNREFUSED send() error, which results in "500 Internal Server Error". It may deserve a separate test, though.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 29 Jan 2016 00:40:05 +0300
parents 4e6d21192037
children e9064d691790
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
786
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for nginx realip module, realip_remote_addr variable.
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 use lib 'lib';
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 use Test::Nginx;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 ###############################################################################
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 select STDERR; $| = 1;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23 select STDOUT; $| = 1;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 my $t = Test::Nginx->new()->has(qw/http realip/);
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 $t->write_file_expand('nginx.conf', <<'EOF');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 %%TEST_GLOBALS%%
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 daemon off;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 events {
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 http {
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 %%TEST_GLOBALS_HTTP%%
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 set_real_ip_from 127.0.0.1/32;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 real_ip_header X-Forwarded-For;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 server {
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 listen 127.0.0.1:8080;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 server_name localhost;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 location / {
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 add_header X-IP $remote_addr;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 add_header X-Real-IP $realip_remote_addr;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 EOF
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 $t->write_file('index.html', '');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 $t->write_file('1', '');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 $t->try_run('no realip_remote_addr');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 plan(skip_all => 'no 127.0.0.1 on host')
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 if http_get('/') !~ /X-IP: 127.0.0.1/m;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 $t->plan(4);
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64 ###############################################################################
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 like(http_get('/1'), qr/X-Real-IP: 127.0.0.1/m, 'request');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 like(http_get('/'), qr/X-Real-IP: 127.0.0.1/m, 'request redirect');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 like(http_xff('/1', '192.0.2.1'), qr/X-Real-IP: 127.0.0.1/m, 'realip');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 like(http_xff('/', '192.0.2.1'), qr/X-Real-IP: 127.0.0.1/m, 'realip redirect');
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 ###############################################################################
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 sub http_xff {
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 my ($uri, $xff) = @_;
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 return http(<<EOF);
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 GET $uri HTTP/1.0
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 Host: localhost
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 X-Forwarded-For: $xff
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 EOF
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 }
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83
4e6d21192037 Tests: realip module tests with realip_remote_addr variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 ###############################################################################